Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 1 | /* Shared library add-on to iptables to add masquerade 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> |
| 9 | #include <linux/netfilter_ipv4/ip_nat_rule.h> |
| 10 | |
| 11 | /* Function which prints out usage message. */ |
| 12 | static void |
| 13 | help(void) |
| 14 | { |
| 15 | printf( |
| 16 | "MASQUERADE v%s options:\n" |
| 17 | " --to-ports <port>[-<port>]\n" |
| 18 | " Port (range) to map to.\n\n", |
| 19 | NETFILTER_VERSION); |
| 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 | |
| 36 | /* Can't cache this */ |
| 37 | *nfcache |= NFC_UNKNOWN; |
| 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 | |
| 49 | port = atoi(arg); |
| 50 | if (port == 0 || port > 65535) |
| 51 | exit_error(PARAMETER_PROBLEM, "Port `%s' not valid\n", arg); |
| 52 | |
| 53 | dash = strchr(arg, '-'); |
| 54 | if (!dash) { |
| 55 | mr->range[0].min.tcp.port |
| 56 | = mr->range[0].max.tcp.port |
Rusty Russell | f9b2e66 | 2000-04-19 11:24:02 +0000 | [diff] [blame] | 57 | = htons(port); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 58 | } else { |
| 59 | int maxport; |
| 60 | |
| 61 | maxport = atoi(dash + 1); |
| 62 | if (maxport == 0 || maxport > 65535) |
| 63 | exit_error(PARAMETER_PROBLEM, |
| 64 | "Port `%s' not valid\n", dash+1); |
| 65 | if (maxport < port) |
| 66 | /* People are stupid. Present reader excepted. */ |
| 67 | exit_error(PARAMETER_PROBLEM, |
| 68 | "Port range `%s' funky\n", arg); |
Rusty Russell | f9b2e66 | 2000-04-19 11:24:02 +0000 | [diff] [blame] | 69 | mr->range[0].min.tcp.port = htons(port); |
| 70 | mr->range[0].max.tcp.port = htons(maxport); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |
| 74 | /* Function which parses command options; returns true if it |
| 75 | ate an option */ |
| 76 | static int |
| 77 | parse(int c, char **argv, int invert, unsigned int *flags, |
| 78 | const struct ipt_entry *entry, |
| 79 | struct ipt_entry_target **target) |
| 80 | { |
| 81 | int portok; |
| 82 | struct ip_nat_multi_range *mr |
| 83 | = (struct ip_nat_multi_range *)(*target)->data; |
| 84 | |
| 85 | if (entry->ip.proto == IPPROTO_TCP |
| 86 | || entry->ip.proto == IPPROTO_UDP) |
| 87 | portok = 1; |
| 88 | else |
| 89 | portok = 0; |
| 90 | |
| 91 | switch (c) { |
| 92 | case '1': |
| 93 | if (!portok) |
| 94 | exit_error(PARAMETER_PROBLEM, |
| 95 | "Need TCP or UDP with port specification"); |
| 96 | |
| 97 | if (check_inverse(optarg, &invert)) |
| 98 | exit_error(PARAMETER_PROBLEM, |
| 99 | "Unexpected `!' after --to-ports"); |
| 100 | |
| 101 | parse_ports(optarg, mr); |
| 102 | return 1; |
| 103 | |
| 104 | default: |
| 105 | return 0; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | /* Final check; don't care. */ |
| 110 | static void final_check(unsigned int flags) |
| 111 | { |
| 112 | } |
| 113 | |
| 114 | /* Prints out the targinfo. */ |
| 115 | static void |
| 116 | print(const struct ipt_ip *ip, |
| 117 | const struct ipt_entry_target *target, |
| 118 | int numeric) |
| 119 | { |
| 120 | struct ip_nat_multi_range *mr |
| 121 | = (struct ip_nat_multi_range *)target->data; |
| 122 | struct ip_nat_range *r = &mr->range[0]; |
| 123 | |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 124 | if (r->flags & IP_NAT_RANGE_PROTO_SPECIFIED) { |
Rusty Russell | f9b2e66 | 2000-04-19 11:24:02 +0000 | [diff] [blame] | 125 | printf("masq ports: "); |
| 126 | printf("%hu", ntohs(r->min.tcp.port)); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 127 | if (r->max.tcp.port != r->min.tcp.port) |
Rusty Russell | f9b2e66 | 2000-04-19 11:24:02 +0000 | [diff] [blame] | 128 | printf("-%hu", ntohs(r->max.tcp.port)); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 129 | printf(" "); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | /* Saves the union ipt_targinfo in parsable form to stdout. */ |
| 134 | static void |
| 135 | save(const struct ipt_ip *ip, const struct ipt_entry_target *target) |
| 136 | { |
| 137 | struct ip_nat_multi_range *mr |
| 138 | = (struct ip_nat_multi_range *)target->data; |
| 139 | struct ip_nat_range *r = &mr->range[0]; |
| 140 | |
| 141 | if (r->flags & IP_NAT_RANGE_PROTO_SPECIFIED) { |
Rusty Russell | f9b2e66 | 2000-04-19 11:24:02 +0000 | [diff] [blame] | 142 | printf("%hu", ntohs(r->min.tcp.port)); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 143 | if (r->max.tcp.port != r->min.tcp.port) |
Rusty Russell | f9b2e66 | 2000-04-19 11:24:02 +0000 | [diff] [blame] | 144 | printf("-%hu", ntohs(r->max.tcp.port)); |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 145 | printf(" "); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | struct iptables_target masq |
| 150 | = { NULL, |
| 151 | "MASQUERADE", |
| 152 | NETFILTER_VERSION, |
Rusty Russell | 73f72f5 | 2000-07-03 10:17:57 +0000 | [diff] [blame] | 153 | IPT_ALIGN(sizeof(struct ip_nat_multi_range)), |
| 154 | IPT_ALIGN(sizeof(struct ip_nat_multi_range)), |
Marc Boucher | e6869a8 | 2000-03-20 06:03:29 +0000 | [diff] [blame] | 155 | &help, |
| 156 | &init, |
| 157 | &parse, |
| 158 | &final_check, |
| 159 | &print, |
| 160 | &save, |
| 161 | opts |
| 162 | }; |
| 163 | |
| 164 | void _init(void) |
| 165 | { |
| 166 | register_target(&masq); |
| 167 | } |