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 | |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 14 | static void REDIRECT_help(void) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 15 | { |
| 16 | printf( |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 17 | "REDIRECT target options:\n" |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 18 | " --to-ports <port>[-<port>]\n" |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 19 | " Port (range) to map to.\n"); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 20 | } |
| 21 | |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 22 | static const struct option REDIRECT_opts[] = { |
Patrick McHardy | 500f483 | 2007-09-08 15:59:04 +0000 | [diff] [blame] | 23 | { "to-ports", 1, NULL, '1' }, |
Tom Eastep | 75b4b20 | 2007-11-15 13:06:58 +0000 | [diff] [blame] | 24 | { "random", 0, NULL, '2' }, |
Max Kellermann | 9ee386a | 2008-01-29 13:48:05 +0000 | [diff] [blame] | 25 | { .name = NULL } |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 26 | }; |
| 27 | |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 28 | static void REDIRECT_init(struct xt_entry_target *t) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 29 | { |
| 30 | struct ip_nat_multi_range *mr = (struct ip_nat_multi_range *)t->data; |
| 31 | |
| 32 | /* Actually, it's 0, but it's ignored at the moment. */ |
| 33 | mr->rangesize = 1; |
| 34 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | /* Parses ports */ |
| 38 | static void |
| 39 | parse_ports(const char *arg, struct ip_nat_multi_range *mr) |
| 40 | { |
| 41 | const char *dash; |
| 42 | int port; |
| 43 | |
| 44 | mr->range[0].flags |= IP_NAT_RANGE_PROTO_SPECIFIED; |
| 45 | |
Phil Oester | 3836fcc | 2006-06-20 13:45:38 +0000 | [diff] [blame] | 46 | if (strchr(arg, '.')) |
| 47 | exit_error(PARAMETER_PROBLEM, "IP address not permitted\n"); |
| 48 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 49 | port = atoi(arg); |
Kristof Provost | ccecd38 | 2008-05-26 00:55:34 +0200 | [diff] [blame] | 50 | if (port == 0) |
| 51 | port = service_to_port(arg, NULL); |
| 52 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 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 | |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 77 | static int REDIRECT_parse(int c, char **argv, int invert, unsigned int *flags, |
| 78 | const void *e, struct xt_entry_target **target) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 79 | { |
Yasuyuki KOZAKAI | ac8b271 | 2007-07-24 06:06:59 +0000 | [diff] [blame] | 80 | const struct ipt_entry *entry = e; |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 81 | struct ip_nat_multi_range *mr |
| 82 | = (struct ip_nat_multi_range *)(*target)->data; |
| 83 | int portok; |
| 84 | |
| 85 | if (entry->ip.proto == IPPROTO_TCP |
Patrick McHardy | 36d870c | 2005-07-22 06:39:45 +0000 | [diff] [blame] | 86 | || entry->ip.proto == IPPROTO_UDP |
Patrick McHardy | 5a942f9 | 2008-11-04 13:22:40 +0100 | [diff] [blame^] | 87 | || entry->ip.proto == IPPROTO_SCTP |
| 88 | || entry->ip.proto == IPPROTO_DCCP |
Patrick McHardy | 36d870c | 2005-07-22 06:39:45 +0000 | [diff] [blame] | 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, |
Patrick McHardy | 5a942f9 | 2008-11-04 13:22:40 +0100 | [diff] [blame^] | 98 | "Need TCP, UDP, SCTP or DCCP with port specification"); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 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 | |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 123 | static void REDIRECT_print(const void *ip, const struct xt_entry_target *target, |
| 124 | int numeric) |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 125 | { |
| 126 | struct ip_nat_multi_range *mr |
| 127 | = (struct ip_nat_multi_range *)target->data; |
| 128 | struct ip_nat_range *r = &mr->range[0]; |
| 129 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 130 | if (r->flags & IP_NAT_RANGE_PROTO_SPECIFIED) { |
Rusty Russell | 849779c | 2000-04-23 15:51:51 +0000 | [diff] [blame] | 131 | printf("redir ports "); |
Rusty Russell | f9b2e66 | 2000-04-19 11:24:02 +0000 | [diff] [blame] | 132 | printf("%hu", ntohs(r->min.tcp.port)); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 133 | if (r->max.tcp.port != r->min.tcp.port) |
Rusty Russell | f9b2e66 | 2000-04-19 11:24:02 +0000 | [diff] [blame] | 134 | printf("-%hu", ntohs(r->max.tcp.port)); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 135 | printf(" "); |
Patrick McHardy | ef399a3 | 2007-05-29 11:24:45 +0000 | [diff] [blame] | 136 | if (mr->range[0].flags & IP_NAT_RANGE_PROTO_RANDOM) |
| 137 | printf("random "); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 141 | 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] | 142 | { |
| 143 | struct ip_nat_multi_range *mr |
| 144 | = (struct ip_nat_multi_range *)target->data; |
| 145 | struct ip_nat_range *r = &mr->range[0]; |
| 146 | |
| 147 | if (r->flags & IP_NAT_RANGE_PROTO_SPECIFIED) { |
Harald Welte | 963bdcc | 2001-03-16 20:37:10 +0000 | [diff] [blame] | 148 | printf("--to-ports "); |
Rusty Russell | f9b2e66 | 2000-04-19 11:24:02 +0000 | [diff] [blame] | 149 | printf("%hu", ntohs(r->min.tcp.port)); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 150 | if (r->max.tcp.port != r->min.tcp.port) |
Rusty Russell | f9b2e66 | 2000-04-19 11:24:02 +0000 | [diff] [blame] | 151 | printf("-%hu", ntohs(r->max.tcp.port)); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 152 | printf(" "); |
Patrick McHardy | ef399a3 | 2007-05-29 11:24:45 +0000 | [diff] [blame] | 153 | if (mr->range[0].flags & IP_NAT_RANGE_PROTO_RANDOM) |
| 154 | printf("--random "); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 158 | static struct xtables_target redirect_tg_reg = { |
Pablo Neira | 8caee8b | 2004-12-28 13:11:59 +0000 | [diff] [blame] | 159 | .name = "REDIRECT", |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 160 | .version = XTABLES_VERSION, |
| 161 | .family = PF_INET, |
| 162 | .size = XT_ALIGN(sizeof(struct ip_nat_multi_range)), |
| 163 | .userspacesize = XT_ALIGN(sizeof(struct ip_nat_multi_range)), |
Jan Engelhardt | 1d5b63d | 2007-10-04 16:29:00 +0000 | [diff] [blame] | 164 | .help = REDIRECT_help, |
| 165 | .init = REDIRECT_init, |
| 166 | .parse = REDIRECT_parse, |
| 167 | .print = REDIRECT_print, |
| 168 | .save = REDIRECT_save, |
| 169 | .extra_opts = REDIRECT_opts, |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 170 | }; |
| 171 | |
| 172 | void _init(void) |
| 173 | { |
Jan Engelhardt | 8b7c64d | 2008-04-15 11:48:25 +0200 | [diff] [blame] | 174 | xtables_register_target(&redirect_tg_reg); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 175 | } |