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_ipv4/ip_nat_rule.h>
10 #include <linux/netfilter_ipv4/ipt_SAME.h>
12 /* Function which prints out usage message. */
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"
24 " source selection\n",
28 static struct option opts[] = {
30 { "nodst", 0, 0, '2'},
34 /* Initialize the target. */
36 init(struct ipt_entry_target *t, unsigned int *nfcache)
38 struct ipt_same_info *mr = (struct ipt_same_info *)t->data;
40 /* Set default to 0 */
45 /* Can't cache this */
46 *nfcache |= NFC_UNKNOWN;
49 /* Parses range of IPs */
51 parse_to(char *arg, struct ip_nat_range *range)
56 range->flags |= IP_NAT_RANGE_MAP_IPS;
57 dash = strchr(arg, '-');
62 ip = dotted_to_addr(arg);
64 exit_error(PARAMETER_PROBLEM, "Bad IP address `%s'\n",
66 range->min_ip = ip->s_addr;
69 ip = dotted_to_addr(dash+1);
71 exit_error(PARAMETER_PROBLEM, "Bad IP address `%s'\n",
74 range->max_ip = ip->s_addr;
76 if (range->min_ip > range->max_ip)
77 exit_error(PARAMETER_PROBLEM, "Bad IP range `%s-%s'\n",
81 #define IPT_SAME_OPT_TO 0x01
82 #define IPT_SAME_OPT_NODST 0x02
84 /* Function which parses command options; returns true if it
87 parse(int c, char **argv, int invert, unsigned int *flags,
88 const struct ipt_entry *entry,
89 struct ipt_entry_target **target)
91 struct ipt_same_info *mr
92 = (struct ipt_same_info *)(*target)->data;
96 if (mr->rangesize == IPT_SAME_MAX_RANGE)
97 exit_error(PARAMETER_PROBLEM,
98 "Too many ranges specified, maximum "
101 if (check_inverse(optarg, &invert, NULL, 0))
102 exit_error(PARAMETER_PROBLEM,
103 "Unexpected `!' after --to");
105 parse_to(optarg, &mr->range[mr->rangesize]);
107 *flags |= IPT_SAME_OPT_TO;
111 if (*flags & IPT_SAME_OPT_NODST)
112 exit_error(PARAMETER_PROBLEM,
113 "Can't specify --nodst twice");
115 mr->info |= IPT_SAME_NODST;
116 *flags |= IPT_SAME_OPT_NODST;
126 /* Final check; need --to. */
127 static void final_check(unsigned int flags)
129 if (!(flags & IPT_SAME_OPT_TO))
130 exit_error(PARAMETER_PROBLEM,
134 /* Prints out the targinfo. */
136 print(const struct ipt_ip *ip,
137 const struct ipt_entry_target *target,
141 struct ipt_same_info *mr
142 = (struct ipt_same_info *)target->data;
146 for (count = 0; count < mr->rangesize; count++) {
147 struct ip_nat_range *r = &mr->range[count];
150 a.s_addr = r->min_ip;
152 printf("%s", addr_to_dotted(&a));
153 a.s_addr = r->max_ip;
155 if (r->min_ip == r->max_ip)
158 printf("-%s ", addr_to_dotted(&a));
161 if (mr->info & IPT_SAME_NODST)
165 /* Saves the union ipt_targinfo in parsable form to stdout. */
167 save(const struct ipt_ip *ip, const struct ipt_entry_target *target)
170 struct ipt_same_info *mr
171 = (struct ipt_same_info *)target->data;
173 for (count = 0; count < mr->rangesize; count++) {
174 struct ip_nat_range *r = &mr->range[count];
177 a.s_addr = r->min_ip;
178 printf("--to %s", addr_to_dotted(&a));
179 a.s_addr = r->max_ip;
181 if (r->min_ip == r->max_ip)
184 printf("-%s ", addr_to_dotted(&a));
187 if (mr->info & IPT_SAME_NODST)
192 struct iptables_target same
196 IPT_ALIGN(sizeof(struct ipt_same_info)),
197 IPT_ALIGN(sizeof(struct ipt_same_info)),
209 register_target(&same);