Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1 | /* Shared library add-on to iptables to add redirect support. */ |
| 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> |
Patrick McHardy | 40d5475 | 2007-04-18 07:00:36 +0000 | [diff] [blame] | 9 | #include <linux/netfilter/nf_nat.h> |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 10 | |
Patrick McHardy | ef399a3 | 2007-05-29 11:24:45 +0000 | [diff] [blame] | 11 | #define IPT_REDIRECT_OPT_DEST 0x01 |
| 12 | #define IPT_REDIRECT_OPT_RANDOM 0x02 |
| 13 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 14 | /* Function which prints out usage message. */ |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 15 | static void REDIRECT_help(void) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 16 | { |
| 17 | printf( |
| 18 | "REDIRECT v%s options:\n" |
| 19 | " --to-ports <port>[-<port>]\n" |
| 20 | " Port (range) to map to.\n\n", |
Harald Welte | 80fe35d | 2002-05-29 13:08:15 +0000 | [diff] [blame] | 21 | IPTABLES_VERSION); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 22 | } |
| 23 | |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 24 | static const struct option REDIRECT_opts[] = { |
Patrick McHardy | 500f483 | 2007-09-08 15:59:04 +0000 | [diff] [blame] | 25 | { "to-ports", 1, NULL, '1' }, |
Tom Eastep | 75b4b20 | 2007-11-15 13:06:58 +0000 | [diff] [blame] | 26 | { "random", 0, NULL, '2' }, |
Max Kellermann | 9ee386a | 2008-01-29 13:48:05 +0000 | [diff] [blame^] | 27 | { .name = NULL } |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 28 | }; |
| 29 | |
| 30 | /* Initialize the target. */ |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 31 | static void REDIRECT_init(struct xt_entry_target *t) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 32 | { |
| 33 | struct ip_nat_multi_range *mr = (struct ip_nat_multi_range *)t->data; |
| 34 | |
| 35 | /* Actually, it's 0, but it's ignored at the moment. */ |
| 36 | mr->rangesize = 1; |
| 37 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | /* Parses ports */ |
| 41 | static void |
| 42 | parse_ports(const char *arg, struct ip_nat_multi_range *mr) |
| 43 | { |
| 44 | const char *dash; |
| 45 | int port; |
| 46 | |
| 47 | mr->range[0].flags |= IP_NAT_RANGE_PROTO_SPECIFIED; |
| 48 | |
Phil Oester | 3836fcc | 2006-06-20 13:45:38 +0000 | [diff] [blame] | 49 | if (strchr(arg, '.')) |
| 50 | exit_error(PARAMETER_PROBLEM, "IP address not permitted\n"); |
| 51 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 52 | port = atoi(arg); |
| 53 | if (port == 0 || port > 65535) |
| 54 | exit_error(PARAMETER_PROBLEM, "Port `%s' not valid\n", arg); |
| 55 | |
| 56 | dash = strchr(arg, '-'); |
| 57 | if (!dash) { |
| 58 | mr->range[0].min.tcp.port |
| 59 | = mr->range[0].max.tcp.port |
Rusty Russell | f9b2e66 | 2000-04-19 11:24:02 +0000 | [diff] [blame] | 60 | = htons(port); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 61 | } else { |
| 62 | int maxport; |
| 63 | |
| 64 | maxport = atoi(dash + 1); |
| 65 | if (maxport == 0 || maxport > 65535) |
| 66 | exit_error(PARAMETER_PROBLEM, |
| 67 | "Port `%s' not valid\n", dash+1); |
| 68 | if (maxport < port) |
| 69 | /* People are stupid. */ |
| 70 | exit_error(PARAMETER_PROBLEM, |
| 71 | "Port range `%s' funky\n", arg); |
Rusty Russell | f9b2e66 | 2000-04-19 11:24:02 +0000 | [diff] [blame] | 72 | mr->range[0].min.tcp.port = htons(port); |
| 73 | mr->range[0].max.tcp.port = htons(maxport); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
| 77 | /* Function which parses command options; returns true if it |
| 78 | ate an option */ |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 79 | static int REDIRECT_parse(int c, char **argv, int invert, unsigned int *flags, |
| 80 | const void *e, struct xt_entry_target **target) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 81 | { |
Yasuyuki KOZAKAI | ac8b271 | 2007-07-24 06:06:59 +0000 | [diff] [blame] | 82 | const struct ipt_entry *entry = e; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 83 | struct ip_nat_multi_range *mr |
| 84 | = (struct ip_nat_multi_range *)(*target)->data; |
| 85 | int portok; |
| 86 | |
| 87 | if (entry->ip.proto == IPPROTO_TCP |
Patrick McHardy | 36d870c | 2005-07-22 06:39:45 +0000 | [diff] [blame] | 88 | || entry->ip.proto == IPPROTO_UDP |
| 89 | || entry->ip.proto == IPPROTO_ICMP) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 90 | portok = 1; |
| 91 | else |
| 92 | portok = 0; |
| 93 | |
| 94 | switch (c) { |
| 95 | case '1': |
| 96 | if (!portok) |
| 97 | exit_error(PARAMETER_PROBLEM, |
| 98 | "Need TCP or UDP with port specification"); |
| 99 | |
Harald Welte | b77f1da | 2002-03-14 11:35:58 +0000 | [diff] [blame] | 100 | if (check_inverse(optarg, &invert, NULL, 0)) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 101 | exit_error(PARAMETER_PROBLEM, |
| 102 | "Unexpected `!' after --to-ports"); |
| 103 | |
| 104 | parse_ports(optarg, mr); |
Patrick McHardy | ef399a3 | 2007-05-29 11:24:45 +0000 | [diff] [blame] | 105 | if (*flags & IPT_REDIRECT_OPT_RANDOM) |
| 106 | mr->range[0].flags |= IP_NAT_RANGE_PROTO_RANDOM; |
| 107 | *flags |= IPT_REDIRECT_OPT_DEST; |
| 108 | return 1; |
| 109 | |
| 110 | case '2': |
| 111 | if (*flags & IPT_REDIRECT_OPT_DEST) { |
| 112 | mr->range[0].flags |= IP_NAT_RANGE_PROTO_RANDOM; |
| 113 | *flags |= IPT_REDIRECT_OPT_RANDOM; |
| 114 | } else |
| 115 | *flags |= IPT_REDIRECT_OPT_RANDOM; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 116 | return 1; |
| 117 | |
| 118 | default: |
| 119 | return 0; |
| 120 | } |
| 121 | } |
| 122 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 123 | /* Prints out the targinfo. */ |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 124 | static void REDIRECT_print(const void *ip, const struct xt_entry_target *target, |
| 125 | int numeric) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 126 | { |
| 127 | struct ip_nat_multi_range *mr |
| 128 | = (struct ip_nat_multi_range *)target->data; |
| 129 | struct ip_nat_range *r = &mr->range[0]; |
| 130 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 131 | if (r->flags & IP_NAT_RANGE_PROTO_SPECIFIED) { |
Rusty Russell | 849779c | 2000-04-23 15:51:51 +0000 | [diff] [blame] | 132 | printf("redir ports "); |
Rusty Russell | f9b2e66 | 2000-04-19 11:24:02 +0000 | [diff] [blame] | 133 | printf("%hu", ntohs(r->min.tcp.port)); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 134 | if (r->max.tcp.port != r->min.tcp.port) |
Rusty Russell | f9b2e66 | 2000-04-19 11:24:02 +0000 | [diff] [blame] | 135 | printf("-%hu", ntohs(r->max.tcp.port)); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 136 | printf(" "); |
Patrick McHardy | ef399a3 | 2007-05-29 11:24:45 +0000 | [diff] [blame] | 137 | if (mr->range[0].flags & IP_NAT_RANGE_PROTO_RANDOM) |
| 138 | printf("random "); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
| 142 | /* Saves the union ipt_targinfo in parsable form to stdout. */ |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 143 | static void REDIRECT_save(const void *ip, const struct xt_entry_target *target) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 144 | { |
| 145 | struct ip_nat_multi_range *mr |
| 146 | = (struct ip_nat_multi_range *)target->data; |
| 147 | struct ip_nat_range *r = &mr->range[0]; |
| 148 | |
| 149 | if (r->flags & IP_NAT_RANGE_PROTO_SPECIFIED) { |
Harald Welte | 963bdcc | 2001-03-16 20:37:10 +0000 | [diff] [blame] | 150 | printf("--to-ports "); |
Rusty Russell | f9b2e66 | 2000-04-19 11:24:02 +0000 | [diff] [blame] | 151 | printf("%hu", ntohs(r->min.tcp.port)); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 152 | if (r->max.tcp.port != r->min.tcp.port) |
Rusty Russell | f9b2e66 | 2000-04-19 11:24:02 +0000 | [diff] [blame] | 153 | printf("-%hu", ntohs(r->max.tcp.port)); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 154 | printf(" "); |
Patrick McHardy | ef399a3 | 2007-05-29 11:24:45 +0000 | [diff] [blame] | 155 | if (mr->range[0].flags & IP_NAT_RANGE_PROTO_RANDOM) |
| 156 | printf("--random "); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 160 | static struct iptables_target redirect_target = { |
Pablo Neira | 8caee8b | 2004-12-28 13:11:59 +0000 | [diff] [blame] | 161 | .name = "REDIRECT", |
| 162 | .version = IPTABLES_VERSION, |
| 163 | .size = IPT_ALIGN(sizeof(struct ip_nat_multi_range)), |
| 164 | .userspacesize = IPT_ALIGN(sizeof(struct ip_nat_multi_range)), |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 165 | .help = REDIRECT_help, |
| 166 | .init = REDIRECT_init, |
| 167 | .parse = REDIRECT_parse, |
| 168 | .print = REDIRECT_print, |
| 169 | .save = REDIRECT_save, |
| 170 | .extra_opts = REDIRECT_opts, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 171 | }; |
| 172 | |
| 173 | void _init(void) |
| 174 | { |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 175 | register_target(&redirect_target); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 176 | } |