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