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