1 /* Shared library add-on to iptables to add simple non load-balancing SNAT support. */
8 #include <linux/netfilter_ipv4/ip_tables.h>
9 #include <linux/netfilter/nf_nat.h>
10 /* For 64bit kernel / 32bit userspace */
11 #include "../include/linux/netfilter_ipv4/ipt_SAME.h"
13 /* Function which prints out usage message. */
14 static void SAME_help(void)
17 "SAME target options:\n"
18 " --to <ipaddr>-<ipaddr>\n"
19 " Addresses to map source to.\n"
20 " May be specified more than\n"
21 " once for multiple ranges.\n"
23 " Don't use destination-ip in\n"
26 " Randomize source port\n");
29 static const struct option SAME_opts[] = {
30 { "to", 1, NULL, '1' },
31 { "nodst", 0, NULL, '2'},
32 { "random", 0, NULL, '3' },
36 /* Initialize the target. */
37 static void SAME_init(struct xt_entry_target *t)
39 struct ipt_same_info *mr = (struct ipt_same_info *)t->data;
41 /* Set default to 0 */
48 /* Parses range of IPs */
50 parse_to(char *arg, struct ip_nat_range *range)
53 const struct in_addr *ip;
55 range->flags |= IP_NAT_RANGE_MAP_IPS;
56 dash = strchr(arg, '-');
61 ip = numeric_to_ipaddr(arg);
63 exit_error(PARAMETER_PROBLEM, "Bad IP address `%s'\n",
65 range->min_ip = ip->s_addr;
68 ip = numeric_to_ipaddr(dash+1);
70 exit_error(PARAMETER_PROBLEM, "Bad IP address `%s'\n",
73 range->max_ip = ip->s_addr;
75 if (range->min_ip > range->max_ip)
76 exit_error(PARAMETER_PROBLEM, "Bad IP range `%s-%s'\n",
80 #define IPT_SAME_OPT_TO 0x01
81 #define IPT_SAME_OPT_NODST 0x02
82 #define IPT_SAME_OPT_RANDOM 0x04
84 /* Function which parses command options; returns true if it
86 static int SAME_parse(int c, char **argv, int invert, unsigned int *flags,
87 const void *entry, struct xt_entry_target **target)
89 struct ipt_same_info *mr
90 = (struct ipt_same_info *)(*target)->data;
95 if (mr->rangesize == IPT_SAME_MAX_RANGE)
96 exit_error(PARAMETER_PROBLEM,
97 "Too many ranges specified, maximum "
100 if (check_inverse(optarg, &invert, NULL, 0))
101 exit_error(PARAMETER_PROBLEM,
102 "Unexpected `!' after --to");
104 parse_to(optarg, &mr->range[mr->rangesize]);
105 /* WTF do we need this for? */
106 if (*flags & IPT_SAME_OPT_RANDOM)
107 mr->range[mr->rangesize].flags
108 |= IP_NAT_RANGE_PROTO_RANDOM;
110 *flags |= IPT_SAME_OPT_TO;
114 if (*flags & IPT_SAME_OPT_NODST)
115 exit_error(PARAMETER_PROBLEM,
116 "Can't specify --nodst twice");
118 mr->info |= IPT_SAME_NODST;
119 *flags |= IPT_SAME_OPT_NODST;
123 *flags |= IPT_SAME_OPT_RANDOM;
124 for (count=0; count < mr->rangesize; count++)
125 mr->range[count].flags |= IP_NAT_RANGE_PROTO_RANDOM;
135 /* Final check; need --to. */
136 static void SAME_check(unsigned int flags)
138 if (!(flags & IPT_SAME_OPT_TO))
139 exit_error(PARAMETER_PROBLEM,
143 /* Prints out the targinfo. */
144 static void SAME_print(const void *ip, const struct xt_entry_target *target,
148 struct ipt_same_info *mr
149 = (struct ipt_same_info *)target->data;
150 int random_selection = 0;
154 for (count = 0; count < mr->rangesize; count++) {
155 struct ip_nat_range *r = &mr->range[count];
158 a.s_addr = r->min_ip;
160 printf("%s", ipaddr_to_numeric(&a));
161 a.s_addr = r->max_ip;
163 if (r->min_ip == r->max_ip)
166 printf("-%s ", ipaddr_to_numeric(&a));
167 if (r->flags & IP_NAT_RANGE_PROTO_RANDOM)
168 random_selection = 1;
171 if (mr->info & IPT_SAME_NODST)
174 if (random_selection)
178 /* Saves the union ipt_targinfo in parsable form to stdout. */
179 static void SAME_save(const void *ip, const struct xt_entry_target *target)
182 struct ipt_same_info *mr
183 = (struct ipt_same_info *)target->data;
184 int random_selection = 0;
186 for (count = 0; count < mr->rangesize; count++) {
187 struct ip_nat_range *r = &mr->range[count];
190 a.s_addr = r->min_ip;
191 printf("--to %s", ipaddr_to_numeric(&a));
192 a.s_addr = r->max_ip;
194 if (r->min_ip == r->max_ip)
197 printf("-%s ", ipaddr_to_numeric(&a));
198 if (r->flags & IP_NAT_RANGE_PROTO_RANDOM)
199 random_selection = 1;
202 if (mr->info & IPT_SAME_NODST)
205 if (random_selection)
209 static struct xtables_target same_tg_reg = {
211 .version = XTABLES_VERSION,
213 .size = XT_ALIGN(sizeof(struct ipt_same_info)),
214 .userspacesize = XT_ALIGN(sizeof(struct ipt_same_info)),
218 .final_check = SAME_check,
221 .extra_opts = SAME_opts,
226 xtables_register_target(&same_tg_reg);