1 /* Shared library add-on to iptables to add destination-NAT support. */
8 #include <linux/netfilter_ipv4/ip_tables.h>
9 #include <linux/netfilter_ipv4/ip_nat_rule.h>
11 /* Dest NAT data consists of a multi-range, indicating where to map
15 struct ipt_entry_target t;
16 struct ip_nat_multi_range mr;
19 /* Function which prints out usage message. */
25 " --to-destination <ipaddr>[-<ipaddr>][:port-port]\n"
26 " Address to map destination to.\n"
27 " (You can use this more than once)\n\n",
31 static struct option opts[] = {
32 { "to-destination", 1, 0, '1' },
36 /* Initialize the target. */
38 init(struct ipt_entry_target *t, unsigned int *nfcache)
40 /* Can't cache this */
41 *nfcache |= NFC_UNKNOWN;
44 static struct ipt_natinfo *
45 append_range(struct ipt_natinfo *info, const struct ip_nat_range *range)
49 /* One rangesize already in struct ipt_natinfo */
50 size = IPT_ALIGN(sizeof(*info) + info->mr.rangesize * sizeof(*range));
52 info = realloc(info, size);
54 exit_error(OTHER_PROBLEM, "Out of memory\n");
56 info->t.u.target_size = size;
57 info->mr.range[info->mr.rangesize] = *range;
63 /* Ranges expected in network order. */
64 static struct ipt_entry_target *
65 parse_to(char *arg, int portok, struct ipt_natinfo *info)
67 struct ip_nat_range range;
71 memset(&range, 0, sizeof(range));
72 colon = strchr(arg, ':');
78 exit_error(PARAMETER_PROBLEM,
79 "Need TCP or UDP with port specification");
81 range.flags |= IP_NAT_RANGE_PROTO_SPECIFIED;
84 if (port == 0 || port > 65535)
85 exit_error(PARAMETER_PROBLEM,
86 "Port `%s' not valid\n", colon+1);
88 dash = strchr(colon, '-');
96 maxport = atoi(dash + 1);
97 if (maxport == 0 || maxport > 65535)
98 exit_error(PARAMETER_PROBLEM,
99 "Port `%s' not valid\n", dash+1);
101 /* People are stupid. */
102 exit_error(PARAMETER_PROBLEM,
103 "Port range `%s' funky\n", colon+1);
104 range.min.tcp.port = htons(port);
105 range.max.tcp.port = htons(maxport);
107 /* Starts with a colon? No IP info...*/
109 return &(append_range(info, &range)->t);
113 range.flags |= IP_NAT_RANGE_MAP_IPS;
114 dash = strchr(arg, '-');
115 if (colon && dash && dash > colon)
121 ip = dotted_to_addr(arg);
123 exit_error(PARAMETER_PROBLEM, "Bad IP address `%s'\n",
125 range.min_ip = ip->s_addr;
127 ip = dotted_to_addr(dash+1);
129 exit_error(PARAMETER_PROBLEM, "Bad IP address `%s'\n",
131 range.max_ip = ip->s_addr;
133 range.max_ip = range.min_ip;
135 return &(append_range(info, &range)->t);
138 /* Function which parses command options; returns true if it
141 parse(int c, char **argv, int invert, unsigned int *flags,
142 const struct ipt_entry *entry,
143 struct ipt_entry_target **target)
145 struct ipt_natinfo *info = (void *)*target;
148 if (entry->ip.proto == IPPROTO_TCP
149 || entry->ip.proto == IPPROTO_UDP)
156 if (check_inverse(optarg, &invert, NULL, 0))
157 exit_error(PARAMETER_PROBLEM,
158 "Unexpected `!' after --to-destination");
160 *target = parse_to(optarg, portok, info);
169 /* Final check; must have specfied --to-source. */
170 static void final_check(unsigned int flags)
173 exit_error(PARAMETER_PROBLEM,
174 "You must specify --to-destination");
177 static void print_range(const struct ip_nat_range *r)
179 if (r->flags & IP_NAT_RANGE_MAP_IPS) {
182 a.s_addr = r->min_ip;
183 printf("%s", addr_to_dotted(&a));
184 if (r->max_ip != r->min_ip) {
185 a.s_addr = r->max_ip;
186 printf("-%s", addr_to_dotted(&a));
189 if (r->flags & IP_NAT_RANGE_PROTO_SPECIFIED) {
191 printf("%hu", ntohs(r->min.tcp.port));
192 if (r->max.tcp.port != r->min.tcp.port)
193 printf("-%hu", ntohs(r->max.tcp.port));
197 /* Prints out the targinfo. */
199 print(const struct ipt_ip *ip,
200 const struct ipt_entry_target *target,
203 struct ipt_natinfo *info = (void *)target;
207 for (i = 0; i < info->mr.rangesize; i++) {
208 print_range(&info->mr.range[i]);
213 /* Saves the union ipt_targinfo in parsable form to stdout. */
215 save(const struct ipt_ip *ip, const struct ipt_entry_target *target)
217 struct ipt_natinfo *info = (void *)target;
220 for (i = 0; i < info->mr.rangesize; i++) {
221 printf("--to-destination ");
222 print_range(&info->mr.range[i]);
228 struct iptables_target dnat
232 IPT_ALIGN(sizeof(struct ip_nat_multi_range)),
233 IPT_ALIGN(sizeof(struct ip_nat_multi_range)),
245 register_target(&dnat);