Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1 | /* Shared library add-on to iptables to add source-NAT support. */ |
| 2 | #include <stdio.h> |
| 3 | #include <netdb.h> |
| 4 | #include <string.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <getopt.h> |
Jan Engelhardt | 5d9678a | 2008-11-20 10:15:35 +0100 | [diff] [blame] | 7 | #include <xtables.h> |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 8 | #include <iptables.h> |
| 9 | #include <linux/netfilter_ipv4/ip_tables.h> |
Patrick McHardy | 40d5475 | 2007-04-18 07:00:36 +0000 | [diff] [blame] | 10 | #include <linux/netfilter/nf_nat.h> |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 11 | |
Eric Leblond | ae4b0b3 | 2007-02-24 15:11:33 +0000 | [diff] [blame] | 12 | #define IPT_SNAT_OPT_SOURCE 0x01 |
Patrick McHardy | e656e26 | 2007-04-18 12:56:05 +0000 | [diff] [blame] | 13 | #define IPT_SNAT_OPT_RANDOM 0x02 |
Eric Leblond | ae4b0b3 | 2007-02-24 15:11:33 +0000 | [diff] [blame] | 14 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 15 | /* Source NAT data consists of a multi-range, indicating where to map |
| 16 | to. */ |
| 17 | struct ipt_natinfo |
| 18 | { |
Yasuyuki KOZAKAI | 193df8e | 2007-07-24 05:57:28 +0000 | [diff] [blame] | 19 | struct xt_entry_target t; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 20 | struct ip_nat_multi_range mr; |
| 21 | }; |
| 22 | |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 23 | static void SNAT_help(void) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 24 | { |
| 25 | printf( |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 26 | "SNAT target options:\n" |
Patrick McHardy | ef399a3 | 2007-05-29 11:24:45 +0000 | [diff] [blame] | 27 | " --to-source <ipaddr>[-<ipaddr>][:port-port]\n" |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 28 | " Address to map source to.\n" |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 29 | "[--random]\n"); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 30 | } |
| 31 | |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 32 | static const struct option SNAT_opts[] = { |
Patrick McHardy | 500f483 | 2007-09-08 15:59:04 +0000 | [diff] [blame] | 33 | { "to-source", 1, NULL, '1' }, |
| 34 | { "random", 0, NULL, '2' }, |
Max Kellermann | 9ee386a | 2008-01-29 13:48:05 +0000 | [diff] [blame] | 35 | { .name = NULL } |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 36 | }; |
| 37 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 38 | static struct ipt_natinfo * |
| 39 | append_range(struct ipt_natinfo *info, const struct ip_nat_range *range) |
| 40 | { |
| 41 | unsigned int size; |
| 42 | |
| 43 | /* One rangesize already in struct ipt_natinfo */ |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 44 | size = XT_ALIGN(sizeof(*info) + info->mr.rangesize * sizeof(*range)); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 45 | |
| 46 | info = realloc(info, size); |
| 47 | if (!info) |
| 48 | exit_error(OTHER_PROBLEM, "Out of memory\n"); |
| 49 | |
Rusty Russell | 228e98d | 2000-04-27 10:28:06 +0000 | [diff] [blame] | 50 | info->t.u.target_size = size; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 51 | info->mr.range[info->mr.rangesize] = *range; |
| 52 | info->mr.rangesize++; |
| 53 | |
| 54 | return info; |
| 55 | } |
| 56 | |
| 57 | /* Ranges expected in network order. */ |
Yasuyuki KOZAKAI | 193df8e | 2007-07-24 05:57:28 +0000 | [diff] [blame] | 58 | static struct xt_entry_target * |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 59 | parse_to(char *arg, int portok, struct ipt_natinfo *info) |
| 60 | { |
| 61 | struct ip_nat_range range; |
Harald Welte | de5ba5d | 2005-02-01 15:14:15 +0000 | [diff] [blame] | 62 | char *colon, *dash, *error; |
Jan Engelhardt | bd94384 | 2008-01-20 13:38:08 +0000 | [diff] [blame] | 63 | const struct in_addr *ip; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 64 | |
| 65 | memset(&range, 0, sizeof(range)); |
| 66 | colon = strchr(arg, ':'); |
| 67 | |
| 68 | if (colon) { |
| 69 | int port; |
| 70 | |
| 71 | if (!portok) |
| 72 | exit_error(PARAMETER_PROBLEM, |
Patrick McHardy | 5a942f9 | 2008-11-04 13:22:40 +0100 | [diff] [blame] | 73 | "Need TCP, UDP, SCTP or DCCP with port specification"); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 74 | |
| 75 | range.flags |= IP_NAT_RANGE_PROTO_SPECIFIED; |
| 76 | |
| 77 | port = atoi(colon+1); |
Yasuyuki KOZAKAI | a3a9c0d | 2005-06-22 12:22:44 +0000 | [diff] [blame] | 78 | if (port <= 0 || port > 65535) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 79 | exit_error(PARAMETER_PROBLEM, |
| 80 | "Port `%s' not valid\n", colon+1); |
| 81 | |
Harald Welte | de5ba5d | 2005-02-01 15:14:15 +0000 | [diff] [blame] | 82 | error = strchr(colon+1, ':'); |
| 83 | if (error) |
| 84 | exit_error(PARAMETER_PROBLEM, |
| 85 | "Invalid port:port syntax - use dash\n"); |
| 86 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 87 | dash = strchr(colon, '-'); |
| 88 | if (!dash) { |
| 89 | range.min.tcp.port |
| 90 | = range.max.tcp.port |
| 91 | = htons(port); |
| 92 | } else { |
| 93 | int maxport; |
| 94 | |
| 95 | maxport = atoi(dash + 1); |
Yasuyuki KOZAKAI | a3a9c0d | 2005-06-22 12:22:44 +0000 | [diff] [blame] | 96 | if (maxport <= 0 || maxport > 65535) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 97 | exit_error(PARAMETER_PROBLEM, |
| 98 | "Port `%s' not valid\n", dash+1); |
| 99 | if (maxport < port) |
| 100 | /* People are stupid. */ |
| 101 | exit_error(PARAMETER_PROBLEM, |
| 102 | "Port range `%s' funky\n", colon+1); |
| 103 | range.min.tcp.port = htons(port); |
| 104 | range.max.tcp.port = htons(maxport); |
| 105 | } |
| 106 | /* Starts with a colon? No IP info...*/ |
| 107 | if (colon == arg) |
| 108 | return &(append_range(info, &range)->t); |
| 109 | *colon = '\0'; |
| 110 | } |
| 111 | |
| 112 | range.flags |= IP_NAT_RANGE_MAP_IPS; |
| 113 | dash = strchr(arg, '-'); |
| 114 | if (colon && dash && dash > colon) |
| 115 | dash = NULL; |
| 116 | |
| 117 | if (dash) |
| 118 | *dash = '\0'; |
| 119 | |
Jan Engelhardt | 1e01b0b | 2009-01-30 04:20:32 +0100 | [diff] [blame] | 120 | ip = xtables_numeric_to_ipaddr(arg); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 121 | if (!ip) |
| 122 | exit_error(PARAMETER_PROBLEM, "Bad IP address `%s'\n", |
| 123 | arg); |
| 124 | range.min_ip = ip->s_addr; |
| 125 | if (dash) { |
Jan Engelhardt | 1e01b0b | 2009-01-30 04:20:32 +0100 | [diff] [blame] | 126 | ip = xtables_numeric_to_ipaddr(dash+1); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 127 | if (!ip) |
| 128 | exit_error(PARAMETER_PROBLEM, "Bad IP address `%s'\n", |
| 129 | dash+1); |
| 130 | range.max_ip = ip->s_addr; |
| 131 | } else |
| 132 | range.max_ip = range.min_ip; |
| 133 | |
| 134 | return &(append_range(info, &range)->t); |
| 135 | } |
| 136 | |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 137 | static int SNAT_parse(int c, char **argv, int invert, unsigned int *flags, |
| 138 | const void *e, struct xt_entry_target **target) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 139 | { |
Yasuyuki KOZAKAI | ac8b271 | 2007-07-24 06:06:59 +0000 | [diff] [blame] | 140 | const struct ipt_entry *entry = e; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 141 | struct ipt_natinfo *info = (void *)*target; |
| 142 | int portok; |
| 143 | |
| 144 | if (entry->ip.proto == IPPROTO_TCP |
Patrick McHardy | 36d870c | 2005-07-22 06:39:45 +0000 | [diff] [blame] | 145 | || entry->ip.proto == IPPROTO_UDP |
Patrick McHardy | 5a942f9 | 2008-11-04 13:22:40 +0100 | [diff] [blame] | 146 | || entry->ip.proto == IPPROTO_SCTP |
| 147 | || entry->ip.proto == IPPROTO_DCCP |
Patrick McHardy | 36d870c | 2005-07-22 06:39:45 +0000 | [diff] [blame] | 148 | || entry->ip.proto == IPPROTO_ICMP) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 149 | portok = 1; |
| 150 | else |
| 151 | portok = 0; |
| 152 | |
| 153 | switch (c) { |
| 154 | case '1': |
Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 155 | if (check_inverse(optarg, &invert, NULL, 0)) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 156 | exit_error(PARAMETER_PROBLEM, |
| 157 | "Unexpected `!' after --to-source"); |
| 158 | |
Eric Leblond | ae4b0b3 | 2007-02-24 15:11:33 +0000 | [diff] [blame] | 159 | if (*flags & IPT_SNAT_OPT_SOURCE) { |
Phil Oester | 8cf6591 | 2005-09-19 15:00:33 +0000 | [diff] [blame] | 160 | if (!kernel_version) |
| 161 | get_kernel_version(); |
| 162 | if (kernel_version > LINUX_VERSION(2, 6, 10)) |
| 163 | exit_error(PARAMETER_PROBLEM, |
| 164 | "Multiple --to-source not supported"); |
| 165 | } |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 166 | *target = parse_to(optarg, portok, info); |
Patrick McHardy | e656e26 | 2007-04-18 12:56:05 +0000 | [diff] [blame] | 167 | /* WTF do we need this for?? */ |
Eric Leblond | ae4b0b3 | 2007-02-24 15:11:33 +0000 | [diff] [blame] | 168 | if (*flags & IPT_SNAT_OPT_RANDOM) |
Patrick McHardy | ef399a3 | 2007-05-29 11:24:45 +0000 | [diff] [blame] | 169 | info->mr.range[0].flags |= IP_NAT_RANGE_PROTO_RANDOM; |
| 170 | *flags |= IPT_SNAT_OPT_SOURCE; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 171 | return 1; |
| 172 | |
Eric Leblond | ae4b0b3 | 2007-02-24 15:11:33 +0000 | [diff] [blame] | 173 | case '2': |
| 174 | if (*flags & IPT_SNAT_OPT_SOURCE) { |
Patrick McHardy | ef399a3 | 2007-05-29 11:24:45 +0000 | [diff] [blame] | 175 | info->mr.range[0].flags |= IP_NAT_RANGE_PROTO_RANDOM; |
Eric Leblond | ae4b0b3 | 2007-02-24 15:11:33 +0000 | [diff] [blame] | 176 | *flags |= IPT_SNAT_OPT_RANDOM; |
| 177 | } else |
| 178 | *flags |= IPT_SNAT_OPT_RANDOM; |
| 179 | return 1; |
Eric Leblond | ae4b0b3 | 2007-02-24 15:11:33 +0000 | [diff] [blame] | 180 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 181 | default: |
| 182 | return 0; |
| 183 | } |
| 184 | } |
| 185 | |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 186 | static void SNAT_check(unsigned int flags) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 187 | { |
Eric Leblond | ae4b0b3 | 2007-02-24 15:11:33 +0000 | [diff] [blame] | 188 | if (!(flags & IPT_SNAT_OPT_SOURCE)) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 189 | exit_error(PARAMETER_PROBLEM, |
| 190 | "You must specify --to-source"); |
| 191 | } |
| 192 | |
| 193 | static void print_range(const struct ip_nat_range *r) |
| 194 | { |
| 195 | if (r->flags & IP_NAT_RANGE_MAP_IPS) { |
| 196 | struct in_addr a; |
| 197 | |
| 198 | a.s_addr = r->min_ip; |
Jan Engelhardt | e44ea7f | 2009-01-30 03:55:09 +0100 | [diff] [blame] | 199 | printf("%s", xtables_ipaddr_to_numeric(&a)); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 200 | if (r->max_ip != r->min_ip) { |
| 201 | a.s_addr = r->max_ip; |
Jan Engelhardt | e44ea7f | 2009-01-30 03:55:09 +0100 | [diff] [blame] | 202 | printf("-%s", xtables_ipaddr_to_numeric(&a)); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 203 | } |
| 204 | } |
| 205 | if (r->flags & IP_NAT_RANGE_PROTO_SPECIFIED) { |
| 206 | printf(":"); |
| 207 | printf("%hu", ntohs(r->min.tcp.port)); |
| 208 | if (r->max.tcp.port != r->min.tcp.port) |
| 209 | printf("-%hu", ntohs(r->max.tcp.port)); |
| 210 | } |
| 211 | } |
| 212 | |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 213 | static void SNAT_print(const void *ip, const struct xt_entry_target *target, |
| 214 | int numeric) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 215 | { |
| 216 | struct ipt_natinfo *info = (void *)target; |
| 217 | unsigned int i = 0; |
| 218 | |
| 219 | printf("to:"); |
| 220 | for (i = 0; i < info->mr.rangesize; i++) { |
| 221 | print_range(&info->mr.range[i]); |
| 222 | printf(" "); |
Patrick McHardy | 9c67def | 2007-04-18 14:00:11 +0000 | [diff] [blame] | 223 | if (info->mr.range[i].flags & IP_NAT_RANGE_PROTO_RANDOM) |
| 224 | printf("random "); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 228 | static void SNAT_save(const void *ip, const struct xt_entry_target *target) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 229 | { |
| 230 | struct ipt_natinfo *info = (void *)target; |
| 231 | unsigned int i = 0; |
| 232 | |
| 233 | for (i = 0; i < info->mr.rangesize; i++) { |
| 234 | printf("--to-source "); |
| 235 | print_range(&info->mr.range[i]); |
| 236 | printf(" "); |
Patrick McHardy | 9c67def | 2007-04-18 14:00:11 +0000 | [diff] [blame] | 237 | if (info->mr.range[i].flags & IP_NAT_RANGE_PROTO_RANDOM) |
| 238 | printf("--random "); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 242 | static struct xtables_target snat_tg_reg = { |
Pablo Neira | 8caee8b | 2004-12-28 13:11:59 +0000 | [diff] [blame] | 243 | .name = "SNAT", |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 244 | .version = XTABLES_VERSION, |
Jan Engelhardt | 03d9948 | 2008-11-18 12:27:54 +0100 | [diff] [blame] | 245 | .family = NFPROTO_IPV4, |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 246 | .size = XT_ALIGN(sizeof(struct ip_nat_multi_range)), |
| 247 | .userspacesize = XT_ALIGN(sizeof(struct ip_nat_multi_range)), |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 248 | .help = SNAT_help, |
| 249 | .parse = SNAT_parse, |
| 250 | .final_check = SNAT_check, |
| 251 | .print = SNAT_print, |
| 252 | .save = SNAT_save, |
| 253 | .extra_opts = SNAT_opts, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 254 | }; |
| 255 | |
| 256 | void _init(void) |
| 257 | { |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 258 | xtables_register_target(&snat_tg_reg); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 259 | } |