| Harald Welte | 18f1aff | 2001-03-25 19:03:23 +0000 | [diff] [blame] | 1 | /* Shared library add-on to iptables to add simple non load-balancing SNAT support. */ |
| Martin Josefsson | f419f75 | 2001-02-19 21:55:27 +0000 | [diff] [blame] | 2 | #include <stdio.h> |
| 3 | #include <netdb.h> |
| 4 | #include <string.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <getopt.h> |
| 7 | #include <iptables.h> |
| 8 | #include <linux/netfilter_ipv4/ip_tables.h> |
| 9 | #include <linux/netfilter_ipv4/ip_nat_rule.h> |
| Harald Welte | cf655eb | 2001-07-28 18:47:11 +0000 | [diff] [blame] | 10 | #include <linux/netfilter_ipv4/ipt_SAME.h> |
| Martin Josefsson | f419f75 | 2001-02-19 21:55:27 +0000 | [diff] [blame] | 11 | |
| Martin Josefsson | f419f75 | 2001-02-19 21:55:27 +0000 | [diff] [blame] | 12 | /* Function which prints out usage message. */ |
| 13 | static void |
| 14 | help(void) |
| 15 | { |
| 16 | printf( |
| 17 | "SAME v%s options:\n" |
| Harald Welte | 18f1aff | 2001-03-25 19:03:23 +0000 | [diff] [blame] | 18 | " --to <ipaddr>-<ipaddr>\n" |
| Harald Welte | cf655eb | 2001-07-28 18:47:11 +0000 | [diff] [blame] | 19 | " Addresses to map source to.\n" |
| Harald Welte | 05e0b01 | 2001-08-26 08:18:25 +0000 | [diff] [blame] | 20 | " May be specified more than\n" |
| 21 | " once for multiple ranges.\n" |
| Harald Welte | cf655eb | 2001-07-28 18:47:11 +0000 | [diff] [blame] | 22 | " --nodst\n" |
| 23 | " Don't use destination-ip in\n" |
| 24 | " source selection\n", |
| Harald Welte | 80fe35d | 2002-05-29 13:08:15 +0000 | [diff] [blame] | 25 | IPTABLES_VERSION); |
| Martin Josefsson | f419f75 | 2001-02-19 21:55:27 +0000 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | static struct option opts[] = { |
| Harald Welte | 18f1aff | 2001-03-25 19:03:23 +0000 | [diff] [blame] | 29 | { "to", 1, 0, '1' }, |
| Harald Welte | cf655eb | 2001-07-28 18:47:11 +0000 | [diff] [blame] | 30 | { "nodst", 0, 0, '2'}, |
| Martin Josefsson | f419f75 | 2001-02-19 21:55:27 +0000 | [diff] [blame] | 31 | { 0 } |
| 32 | }; |
| 33 | |
| 34 | /* Initialize the target. */ |
| 35 | static void |
| 36 | init(struct ipt_entry_target *t, unsigned int *nfcache) |
| 37 | { |
| Harald Welte | cf655eb | 2001-07-28 18:47:11 +0000 | [diff] [blame] | 38 | struct ipt_same_info *mr = (struct ipt_same_info *)t->data; |
| Martin Josefsson | f419f75 | 2001-02-19 21:55:27 +0000 | [diff] [blame] | 39 | |
| Harald Welte | 05e0b01 | 2001-08-26 08:18:25 +0000 | [diff] [blame] | 40 | /* Set default to 0 */ |
| 41 | mr->rangesize = 0; |
| Harald Welte | cf655eb | 2001-07-28 18:47:11 +0000 | [diff] [blame] | 42 | mr->info = 0; |
| Harald Welte | 05e0b01 | 2001-08-26 08:18:25 +0000 | [diff] [blame] | 43 | mr->ipnum = 0; |
| Harald Welte | cf655eb | 2001-07-28 18:47:11 +0000 | [diff] [blame] | 44 | |
| Martin Josefsson | f419f75 | 2001-02-19 21:55:27 +0000 | [diff] [blame] | 45 | /* Can't cache this */ |
| 46 | *nfcache |= NFC_UNKNOWN; |
| 47 | } |
| 48 | |
| 49 | /* Parses range of IPs */ |
| 50 | static void |
| 51 | parse_to(char *arg, struct ip_nat_range *range) |
| 52 | { |
| 53 | char *dash; |
| 54 | struct in_addr *ip; |
| 55 | |
| 56 | range->flags |= IP_NAT_RANGE_MAP_IPS; |
| 57 | dash = strchr(arg, '-'); |
| Harald Welte | 05e0b01 | 2001-08-26 08:18:25 +0000 | [diff] [blame] | 58 | |
| Martin Josefsson | f419f75 | 2001-02-19 21:55:27 +0000 | [diff] [blame] | 59 | if (dash) |
| 60 | *dash = '\0'; |
| Martin Josefsson | f419f75 | 2001-02-19 21:55:27 +0000 | [diff] [blame] | 61 | |
| 62 | ip = dotted_to_addr(arg); |
| 63 | if (!ip) |
| 64 | exit_error(PARAMETER_PROBLEM, "Bad IP address `%s'\n", |
| 65 | arg); |
| 66 | range->min_ip = ip->s_addr; |
| Harald Welte | 05e0b01 | 2001-08-26 08:18:25 +0000 | [diff] [blame] | 67 | |
| 68 | if (dash) { |
| 69 | ip = dotted_to_addr(dash+1); |
| 70 | if (!ip) |
| 71 | exit_error(PARAMETER_PROBLEM, "Bad IP address `%s'\n", |
| 72 | dash+1); |
| 73 | } |
| Martin Josefsson | f419f75 | 2001-02-19 21:55:27 +0000 | [diff] [blame] | 74 | range->max_ip = ip->s_addr; |
| Harald Welte | 05e0b01 | 2001-08-26 08:18:25 +0000 | [diff] [blame] | 75 | if (dash) |
| 76 | if (range->min_ip > range->max_ip) |
| 77 | exit_error(PARAMETER_PROBLEM, "Bad IP range `%s-%s'\n", |
| 78 | arg, dash+1); |
| Martin Josefsson | f419f75 | 2001-02-19 21:55:27 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| Harald Welte | cf655eb | 2001-07-28 18:47:11 +0000 | [diff] [blame] | 81 | #define IPT_SAME_OPT_TO 0x01 |
| 82 | #define IPT_SAME_OPT_NODST 0x02 |
| 83 | |
| Martin Josefsson | f419f75 | 2001-02-19 21:55:27 +0000 | [diff] [blame] | 84 | /* Function which parses command options; returns true if it |
| 85 | ate an option */ |
| 86 | static int |
| 87 | parse(int c, char **argv, int invert, unsigned int *flags, |
| 88 | const struct ipt_entry *entry, |
| 89 | struct ipt_entry_target **target) |
| 90 | { |
| Harald Welte | cf655eb | 2001-07-28 18:47:11 +0000 | [diff] [blame] | 91 | struct ipt_same_info *mr |
| 92 | = (struct ipt_same_info *)(*target)->data; |
| Martin Josefsson | f419f75 | 2001-02-19 21:55:27 +0000 | [diff] [blame] | 93 | |
| 94 | switch (c) { |
| 95 | case '1': |
| Harald Welte | 05e0b01 | 2001-08-26 08:18:25 +0000 | [diff] [blame] | 96 | if (mr->rangesize == IPT_SAME_MAX_RANGE) |
| Harald Welte | cf655eb | 2001-07-28 18:47:11 +0000 | [diff] [blame] | 97 | exit_error(PARAMETER_PROBLEM, |
| Harald Welte | 05e0b01 | 2001-08-26 08:18:25 +0000 | [diff] [blame] | 98 | "Too many ranges specified, maximum " |
| 99 | "is %i ranges.\n", |
| 100 | IPT_SAME_MAX_RANGE); |
| Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 101 | if (check_inverse(optarg, &invert, NULL, 0)) |
| Martin Josefsson | f419f75 | 2001-02-19 21:55:27 +0000 | [diff] [blame] | 102 | exit_error(PARAMETER_PROBLEM, |
| Harald Welte | 18f1aff | 2001-03-25 19:03:23 +0000 | [diff] [blame] | 103 | "Unexpected `!' after --to"); |
| Martin Josefsson | f419f75 | 2001-02-19 21:55:27 +0000 | [diff] [blame] | 104 | |
| Harald Welte | 05e0b01 | 2001-08-26 08:18:25 +0000 | [diff] [blame] | 105 | parse_to(optarg, &mr->range[mr->rangesize]); |
| 106 | mr->rangesize++; |
| Harald Welte | cf655eb | 2001-07-28 18:47:11 +0000 | [diff] [blame] | 107 | *flags |= IPT_SAME_OPT_TO; |
| 108 | break; |
| 109 | |
| 110 | case '2': |
| 111 | if (*flags & IPT_SAME_OPT_NODST) |
| 112 | exit_error(PARAMETER_PROBLEM, |
| 113 | "Can't specify --nodst twice"); |
| 114 | |
| 115 | mr->info |= IPT_SAME_NODST; |
| 116 | *flags |= IPT_SAME_OPT_NODST; |
| 117 | break; |
| 118 | |
| Martin Josefsson | f419f75 | 2001-02-19 21:55:27 +0000 | [diff] [blame] | 119 | default: |
| 120 | return 0; |
| 121 | } |
| Harald Welte | cf655eb | 2001-07-28 18:47:11 +0000 | [diff] [blame] | 122 | |
| 123 | return 1; |
| Martin Josefsson | f419f75 | 2001-02-19 21:55:27 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| Harald Welte | 18f1aff | 2001-03-25 19:03:23 +0000 | [diff] [blame] | 126 | /* Final check; need --to. */ |
| Martin Josefsson | f419f75 | 2001-02-19 21:55:27 +0000 | [diff] [blame] | 127 | static void final_check(unsigned int flags) |
| 128 | { |
| Harald Welte | cf655eb | 2001-07-28 18:47:11 +0000 | [diff] [blame] | 129 | if (!(flags & IPT_SAME_OPT_TO)) |
| Martin Josefsson | f419f75 | 2001-02-19 21:55:27 +0000 | [diff] [blame] | 130 | exit_error(PARAMETER_PROBLEM, |
| Harald Welte | 18f1aff | 2001-03-25 19:03:23 +0000 | [diff] [blame] | 131 | "SAME needs --to"); |
| Martin Josefsson | f419f75 | 2001-02-19 21:55:27 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | /* Prints out the targinfo. */ |
| 135 | static void |
| 136 | print(const struct ipt_ip *ip, |
| 137 | const struct ipt_entry_target *target, |
| 138 | int numeric) |
| 139 | { |
| Harald Welte | 05e0b01 | 2001-08-26 08:18:25 +0000 | [diff] [blame] | 140 | int count; |
| Harald Welte | cf655eb | 2001-07-28 18:47:11 +0000 | [diff] [blame] | 141 | struct ipt_same_info *mr |
| 142 | = (struct ipt_same_info *)target->data; |
| Harald Welte | 05e0b01 | 2001-08-26 08:18:25 +0000 | [diff] [blame] | 143 | |
| 144 | printf("same:"); |
| 145 | |
| 146 | for (count = 0; count < mr->rangesize; count++) { |
| 147 | struct ip_nat_range *r = &mr->range[count]; |
| 148 | struct in_addr a; |
| Martin Josefsson | f419f75 | 2001-02-19 21:55:27 +0000 | [diff] [blame] | 149 | |
| Harald Welte | 05e0b01 | 2001-08-26 08:18:25 +0000 | [diff] [blame] | 150 | a.s_addr = r->min_ip; |
| Martin Josefsson | f419f75 | 2001-02-19 21:55:27 +0000 | [diff] [blame] | 151 | |
| Harald Welte | 05e0b01 | 2001-08-26 08:18:25 +0000 | [diff] [blame] | 152 | printf("%s", addr_to_dotted(&a)); |
| 153 | a.s_addr = r->max_ip; |
| 154 | |
| 155 | if (r->min_ip == r->max_ip) |
| 156 | printf(" "); |
| 157 | else |
| 158 | printf("-%s ", addr_to_dotted(&a)); |
| 159 | } |
| Harald Welte | cf655eb | 2001-07-28 18:47:11 +0000 | [diff] [blame] | 160 | |
| 161 | if (mr->info & IPT_SAME_NODST) |
| 162 | printf("nodst "); |
| Martin Josefsson | f419f75 | 2001-02-19 21:55:27 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | /* Saves the union ipt_targinfo in parsable form to stdout. */ |
| 166 | static void |
| 167 | save(const struct ipt_ip *ip, const struct ipt_entry_target *target) |
| 168 | { |
| Harald Welte | 05e0b01 | 2001-08-26 08:18:25 +0000 | [diff] [blame] | 169 | int count; |
| Harald Welte | cf655eb | 2001-07-28 18:47:11 +0000 | [diff] [blame] | 170 | struct ipt_same_info *mr |
| 171 | = (struct ipt_same_info *)target->data; |
| Martin Josefsson | f419f75 | 2001-02-19 21:55:27 +0000 | [diff] [blame] | 172 | |
| Harald Welte | 05e0b01 | 2001-08-26 08:18:25 +0000 | [diff] [blame] | 173 | for (count = 0; count < mr->rangesize; count++) { |
| 174 | struct ip_nat_range *r = &mr->range[count]; |
| 175 | struct in_addr a; |
| 176 | |
| 177 | a.s_addr = r->min_ip; |
| 178 | printf("--to %s", addr_to_dotted(&a)); |
| 179 | a.s_addr = r->max_ip; |
| 180 | |
| 181 | if (r->min_ip == r->max_ip) |
| 182 | printf(" "); |
| 183 | else |
| 184 | printf("-%s ", addr_to_dotted(&a)); |
| 185 | } |
| Harald Welte | cf655eb | 2001-07-28 18:47:11 +0000 | [diff] [blame] | 186 | |
| 187 | if (mr->info & IPT_SAME_NODST) |
| 188 | printf("--nodst "); |
| Martin Josefsson | f419f75 | 2001-02-19 21:55:27 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| Harald Welte | 3efb6ea | 2001-08-06 18:50:21 +0000 | [diff] [blame] | 191 | static |
| Martin Josefsson | f419f75 | 2001-02-19 21:55:27 +0000 | [diff] [blame] | 192 | struct iptables_target same |
| 193 | = { NULL, |
| 194 | "SAME", |
| Harald Welte | 80fe35d | 2002-05-29 13:08:15 +0000 | [diff] [blame] | 195 | IPTABLES_VERSION, |
| Harald Welte | cf655eb | 2001-07-28 18:47:11 +0000 | [diff] [blame] | 196 | IPT_ALIGN(sizeof(struct ipt_same_info)), |
| 197 | IPT_ALIGN(sizeof(struct ipt_same_info)), |
| Martin Josefsson | f419f75 | 2001-02-19 21:55:27 +0000 | [diff] [blame] | 198 | &help, |
| 199 | &init, |
| 200 | &parse, |
| 201 | &final_check, |
| 202 | &print, |
| 203 | &save, |
| 204 | opts |
| 205 | }; |
| 206 | |
| 207 | void _init(void) |
| 208 | { |
| 209 | register_target(&same); |
| 210 | } |