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 /* For 64bit kernel / 32bit userspace */
11 #include "../include/linux/netfilter_ipv4/ipt_SAME.h"
13 /* Function which prints out usage message. */
19 " --to <ipaddr>-<ipaddr>\n"
20 " Addresses to map source to.\n"
21 " May be specified more than\n"
22 " once for multiple ranges.\n"
24 " Don't use destination-ip in\n"
25 " source selection\n",
29 static struct option opts
[] = {
31 { "nodst", 0, 0, '2'},
35 /* Initialize the target. */
37 init(struct ipt_entry_target
*t
, unsigned int *nfcache
)
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
)
55 range
->flags
|= IP_NAT_RANGE_MAP_IPS
;
56 dash
= strchr(arg
, '-');
61 ip
= dotted_to_addr(arg
);
63 exit_error(PARAMETER_PROBLEM
, "Bad IP address `%s'\n",
65 range
->min_ip
= ip
->s_addr
;
68 ip
= dotted_to_addr(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
83 /* Function which parses command options; returns true if it
86 parse(int c
, char **argv
, int invert
, unsigned int *flags
,
87 const struct ipt_entry
*entry
,
88 struct ipt_entry_target
**target
)
90 struct ipt_same_info
*mr
91 = (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
]);
106 *flags
|= IPT_SAME_OPT_TO
;
110 if (*flags
& IPT_SAME_OPT_NODST
)
111 exit_error(PARAMETER_PROBLEM
,
112 "Can't specify --nodst twice");
114 mr
->info
|= IPT_SAME_NODST
;
115 *flags
|= IPT_SAME_OPT_NODST
;
125 /* Final check; need --to. */
126 static void final_check(unsigned int flags
)
128 if (!(flags
& IPT_SAME_OPT_TO
))
129 exit_error(PARAMETER_PROBLEM
,
133 /* Prints out the targinfo. */
135 print(const struct ipt_ip
*ip
,
136 const struct ipt_entry_target
*target
,
140 struct ipt_same_info
*mr
141 = (struct ipt_same_info
*)target
->data
;
145 for (count
= 0; count
< mr
->rangesize
; count
++) {
146 struct ip_nat_range
*r
= &mr
->range
[count
];
149 a
.s_addr
= r
->min_ip
;
151 printf("%s", addr_to_dotted(&a
));
152 a
.s_addr
= r
->max_ip
;
154 if (r
->min_ip
== r
->max_ip
)
157 printf("-%s ", addr_to_dotted(&a
));
160 if (mr
->info
& IPT_SAME_NODST
)
164 /* Saves the union ipt_targinfo in parsable form to stdout. */
166 save(const struct ipt_ip
*ip
, const struct ipt_entry_target
*target
)
169 struct ipt_same_info
*mr
170 = (struct ipt_same_info
*)target
->data
;
172 for (count
= 0; count
< mr
->rangesize
; count
++) {
173 struct ip_nat_range
*r
= &mr
->range
[count
];
176 a
.s_addr
= r
->min_ip
;
177 printf("--to %s", addr_to_dotted(&a
));
178 a
.s_addr
= r
->max_ip
;
180 if (r
->min_ip
== r
->max_ip
)
183 printf("-%s ", addr_to_dotted(&a
));
186 if (mr
->info
& IPT_SAME_NODST
)
190 static struct iptables_target same
= {
193 .version
= IPTABLES_VERSION
,
194 .size
= IPT_ALIGN(sizeof(struct ipt_same_info
)),
195 .userspacesize
= IPT_ALIGN(sizeof(struct ipt_same_info
)),
199 .final_check
= &final_check
,
207 register_target(&same
);