6 #include <iptables.h> /* get_kernel_version */
7 #include <limits.h> /* INT_MAX in ip_tables.h */
8 #include <linux/netfilter_ipv4/ip_tables.h>
9 #include <net/netfilter/nf_nat.h>
15 O_X_TO_DEST, /* hidden flag */
16 F_TO_DEST = 1 << O_TO_DEST,
17 F_RANDOM = 1 << O_RANDOM,
18 F_X_TO_DEST = 1 << O_X_TO_DEST,
21 /* Dest NAT data consists of a multi-range, indicating where to map
25 struct xt_entry_target t;
26 struct nf_nat_multi_range mr;
29 static void DNAT_help(void)
32 "DNAT target options:\n"
33 " --to-destination [<ipaddr>[-<ipaddr>]][:port[-port]]\n"
34 " Address to map destination to.\n"
35 "[--random] [--persistent]\n");
38 static const struct xt_option_entry DNAT_opts[] = {
39 {.name = "to-destination", .id = O_TO_DEST, .type = XTTYPE_STRING,
40 .flags = XTOPT_MAND | XTOPT_MULTI},
41 {.name = "random", .id = O_RANDOM, .type = XTTYPE_NONE},
42 {.name = "persistent", .id = O_PERSISTENT, .type = XTTYPE_NONE},
46 static struct ipt_natinfo *
47 append_range(struct ipt_natinfo *info, const struct nf_nat_range *range)
51 /* One rangesize already in struct ipt_natinfo */
52 size = XT_ALIGN(sizeof(*info) + info->mr.rangesize * sizeof(*range));
54 info = realloc(info, size);
56 xtables_error(OTHER_PROBLEM, "Out of memory\n");
58 info->t.u.target_size = size;
59 info->mr.range[info->mr.rangesize] = *range;
65 /* Ranges expected in network order. */
66 static struct xt_entry_target *
67 parse_to(const char *orig_arg, int portok, struct ipt_natinfo *info)
69 struct nf_nat_range range;
70 char *arg, *colon, *dash, *error;
71 const struct in_addr *ip;
73 arg = strdup(orig_arg);
75 xtables_error(RESOURCE_PROBLEM, "strdup");
76 memset(&range, 0, sizeof(range));
77 colon = strchr(arg, ':');
83 xtables_error(PARAMETER_PROBLEM,
84 "Need TCP, UDP, SCTP or DCCP with port specification");
86 range.flags |= IP_NAT_RANGE_PROTO_SPECIFIED;
89 if (port <= 0 || port > 65535)
90 xtables_error(PARAMETER_PROBLEM,
91 "Port `%s' not valid\n", colon+1);
93 error = strchr(colon+1, ':');
95 xtables_error(PARAMETER_PROBLEM,
96 "Invalid port:port syntax - use dash\n");
98 dash = strchr(colon, '-');
106 maxport = atoi(dash + 1);
107 if (maxport <= 0 || maxport > 65535)
108 xtables_error(PARAMETER_PROBLEM,
109 "Port `%s' not valid\n", dash+1);
111 /* People are stupid. */
112 xtables_error(PARAMETER_PROBLEM,
113 "Port range `%s' funky\n", colon+1);
114 range.min.tcp.port = htons(port);
115 range.max.tcp.port = htons(maxport);
117 /* Starts with a colon? No IP info...*/
120 return &(append_range(info, &range)->t);
125 range.flags |= IP_NAT_RANGE_MAP_IPS;
126 dash = strchr(arg, '-');
127 if (colon && dash && dash > colon)
133 ip = xtables_numeric_to_ipaddr(arg);
135 xtables_error(PARAMETER_PROBLEM, "Bad IP address \"%s\"\n",
137 range.min_ip = ip->s_addr;
139 ip = xtables_numeric_to_ipaddr(dash+1);
141 xtables_error(PARAMETER_PROBLEM, "Bad IP address \"%s\"\n",
143 range.max_ip = ip->s_addr;
145 range.max_ip = range.min_ip;
148 return &(append_range(info, &range)->t);
151 static void DNAT_parse(struct xt_option_call *cb)
153 const struct ipt_entry *entry = cb->xt_entry;
154 struct ipt_natinfo *info = (void *)(*cb->target);
157 if (entry->ip.proto == IPPROTO_TCP
158 || entry->ip.proto == IPPROTO_UDP
159 || entry->ip.proto == IPPROTO_SCTP
160 || entry->ip.proto == IPPROTO_DCCP
161 || entry->ip.proto == IPPROTO_ICMP)
166 xtables_option_parse(cb);
167 switch (cb->entry->id) {
169 if (cb->xflags & F_X_TO_DEST) {
171 get_kernel_version();
172 if (kernel_version > LINUX_VERSION(2, 6, 10))
173 xtables_error(PARAMETER_PROBLEM,
174 "DNAT: Multiple --to-destination not supported");
176 *cb->target = parse_to(cb->arg, portok, info);
177 cb->xflags |= F_X_TO_DEST;
180 info->mr.range[0].flags |= IP_NAT_RANGE_PERSISTENT;
185 static void DNAT_fcheck(struct xt_fcheck_call *cb)
187 static const unsigned int f = F_TO_DEST | F_RANDOM;
188 struct nf_nat_multi_range *mr = cb->data;
190 if ((cb->xflags & f) == f)
191 mr->range[0].flags |= IP_NAT_RANGE_PROTO_RANDOM;
194 static void print_range(const struct nf_nat_range *r)
196 if (r->flags & IP_NAT_RANGE_MAP_IPS) {
199 a.s_addr = r->min_ip;
200 printf("%s", xtables_ipaddr_to_numeric(&a));
201 if (r->max_ip != r->min_ip) {
202 a.s_addr = r->max_ip;
203 printf("-%s", xtables_ipaddr_to_numeric(&a));
206 if (r->flags & IP_NAT_RANGE_PROTO_SPECIFIED) {
208 printf("%hu", ntohs(r->min.tcp.port));
209 if (r->max.tcp.port != r->min.tcp.port)
210 printf("-%hu", ntohs(r->max.tcp.port));
214 static void DNAT_print(const void *ip, const struct xt_entry_target *target,
217 const struct ipt_natinfo *info = (const void *)target;
221 for (i = 0; i < info->mr.rangesize; i++) {
222 print_range(&info->mr.range[i]);
223 if (info->mr.range[i].flags & IP_NAT_RANGE_PROTO_RANDOM)
225 if (info->mr.range[i].flags & IP_NAT_RANGE_PERSISTENT)
226 printf(" persistent");
230 static void DNAT_save(const void *ip, const struct xt_entry_target *target)
232 const struct ipt_natinfo *info = (const void *)target;
235 for (i = 0; i < info->mr.rangesize; i++) {
236 printf(" --to-destination ");
237 print_range(&info->mr.range[i]);
238 if (info->mr.range[i].flags & IP_NAT_RANGE_PROTO_RANDOM)
240 if (info->mr.range[i].flags & IP_NAT_RANGE_PERSISTENT)
241 printf(" --persistent");
245 static struct xtables_target dnat_tg_reg = {
247 .version = XTABLES_VERSION,
248 .family = NFPROTO_IPV4,
249 .size = XT_ALIGN(sizeof(struct nf_nat_multi_range)),
250 .userspacesize = XT_ALIGN(sizeof(struct nf_nat_multi_range)),
252 .x6_parse = DNAT_parse,
253 .x6_fcheck = DNAT_fcheck,
256 .x6_options = DNAT_opts,
261 xtables_register_target(&dnat_tg_reg);