libipt_SAME.c revision d09b6d591ca7d7d7575cb6aa20384c9830f777ab
1/* Shared library add-on to iptables to add simple non load-balancing SNAT support. */
2#include <stdbool.h>
3#include <stdio.h>
4#include <netdb.h>
5#include <string.h>
6#include <stdlib.h>
7#include <getopt.h>
8#include <xtables.h>
9#include <net/netfilter/nf_nat.h>
10/* For 64bit kernel / 32bit userspace */
11#include <linux/netfilter_ipv4/ipt_SAME.h>
12
13static void SAME_help(void)
14{
15	printf(
16"SAME target options:\n"
17" --to <ipaddr>-<ipaddr>\n"
18"				Addresses to map source to.\n"
19"				 May be specified more than\n"
20"				  once for multiple ranges.\n"
21" --nodst\n"
22"				Don't use destination-ip in\n"
23"				           source selection\n"
24" --random\n"
25"				Randomize source port\n");
26}
27
28static const struct option SAME_opts[] = {
29	{.name = "to",     .has_arg = true,  .val = '1'},
30	{.name = "nodst",  .has_arg = false, .val = '2'},
31	{.name = "random", .has_arg = false, .val = '3'},
32	XT_GETOPT_TABLEEND,
33};
34
35static void SAME_init(struct xt_entry_target *t)
36{
37	struct ipt_same_info *mr = (struct ipt_same_info *)t->data;
38
39	/* Set default to 0 */
40	mr->rangesize = 0;
41	mr->info = 0;
42	mr->ipnum = 0;
43
44}
45
46/* Parses range of IPs */
47static void
48parse_to(char *arg, struct nf_nat_range *range)
49{
50	char *dash;
51	const struct in_addr *ip;
52
53	range->flags |= IP_NAT_RANGE_MAP_IPS;
54	dash = strchr(arg, '-');
55
56	if (dash)
57		*dash = '\0';
58
59	ip = xtables_numeric_to_ipaddr(arg);
60	if (!ip)
61		xtables_error(PARAMETER_PROBLEM, "Bad IP address \"%s\"\n",
62			   arg);
63	range->min_ip = ip->s_addr;
64
65	if (dash) {
66		ip = xtables_numeric_to_ipaddr(dash+1);
67		if (!ip)
68			xtables_error(PARAMETER_PROBLEM, "Bad IP address \"%s\"\n",
69				   dash+1);
70	}
71	range->max_ip = ip->s_addr;
72	if (dash)
73		if (range->min_ip > range->max_ip)
74			xtables_error(PARAMETER_PROBLEM, "Bad IP range \"%s-%s\"\n",
75				   arg, dash+1);
76}
77
78#define IPT_SAME_OPT_TO			0x01
79#define IPT_SAME_OPT_NODST		0x02
80#define IPT_SAME_OPT_RANDOM		0x04
81
82static int SAME_parse(int c, char **argv, int invert, unsigned int *flags,
83                      const void *entry, struct xt_entry_target **target)
84{
85	struct ipt_same_info *mr
86		= (struct ipt_same_info *)(*target)->data;
87	unsigned int count;
88
89	switch (c) {
90	case '1':
91		if (mr->rangesize == IPT_SAME_MAX_RANGE)
92			xtables_error(PARAMETER_PROBLEM,
93				   "Too many ranges specified, maximum "
94				   "is %i ranges.\n",
95				   IPT_SAME_MAX_RANGE);
96		if (xtables_check_inverse(optarg, &invert, NULL, 0, argv))
97			xtables_error(PARAMETER_PROBLEM,
98				   "Unexpected `!' after --to");
99
100		parse_to(optarg, &mr->range[mr->rangesize]);
101		/* WTF do we need this for? */
102		if (*flags & IPT_SAME_OPT_RANDOM)
103			mr->range[mr->rangesize].flags
104				|= IP_NAT_RANGE_PROTO_RANDOM;
105		mr->rangesize++;
106		*flags |= IPT_SAME_OPT_TO;
107		break;
108
109	case '2':
110		if (*flags & IPT_SAME_OPT_NODST)
111			xtables_error(PARAMETER_PROBLEM,
112				   "Can't specify --nodst twice");
113
114		mr->info |= IPT_SAME_NODST;
115		*flags |= IPT_SAME_OPT_NODST;
116		break;
117
118	case '3':
119		*flags |= IPT_SAME_OPT_RANDOM;
120		for (count=0; count < mr->rangesize; count++)
121			mr->range[count].flags |= IP_NAT_RANGE_PROTO_RANDOM;
122		break;
123	}
124
125	return 1;
126}
127
128static void SAME_check(unsigned int flags)
129{
130	if (!(flags & IPT_SAME_OPT_TO))
131		xtables_error(PARAMETER_PROBLEM,
132			   "SAME needs --to");
133}
134
135static void SAME_print(const void *ip, const struct xt_entry_target *target,
136                       int numeric)
137{
138	unsigned int count;
139	const struct ipt_same_info *mr = (const void *)target->data;
140	int random_selection = 0;
141
142	printf("same:");
143
144	for (count = 0; count < mr->rangesize; count++) {
145		const struct nf_nat_range *r = &mr->range[count];
146		struct in_addr a;
147
148		a.s_addr = r->min_ip;
149
150		printf("%s", xtables_ipaddr_to_numeric(&a));
151		a.s_addr = r->max_ip;
152
153		if (r->min_ip == r->max_ip)
154			printf(" ");
155		else
156			printf("-%s ", xtables_ipaddr_to_numeric(&a));
157		if (r->flags & IP_NAT_RANGE_PROTO_RANDOM)
158			random_selection = 1;
159	}
160
161	if (mr->info & IPT_SAME_NODST)
162		printf("nodst ");
163
164	if (random_selection)
165		printf("random ");
166}
167
168static void SAME_save(const void *ip, const struct xt_entry_target *target)
169{
170	unsigned int count;
171	const struct ipt_same_info *mr = (const void *)target->data;
172	int random_selection = 0;
173
174	for (count = 0; count < mr->rangesize; count++) {
175		const struct nf_nat_range *r = &mr->range[count];
176		struct in_addr a;
177
178		a.s_addr = r->min_ip;
179		printf("--to %s", xtables_ipaddr_to_numeric(&a));
180		a.s_addr = r->max_ip;
181
182		if (r->min_ip == r->max_ip)
183			printf(" ");
184		else
185			printf("-%s ", xtables_ipaddr_to_numeric(&a));
186		if (r->flags & IP_NAT_RANGE_PROTO_RANDOM)
187			random_selection = 1;
188	}
189
190	if (mr->info & IPT_SAME_NODST)
191		printf("--nodst ");
192
193	if (random_selection)
194		printf("--random ");
195}
196
197static struct xtables_target same_tg_reg = {
198	.name		= "SAME",
199	.version	= XTABLES_VERSION,
200	.family		= NFPROTO_IPV4,
201	.size		= XT_ALIGN(sizeof(struct ipt_same_info)),
202	.userspacesize	= XT_ALIGN(sizeof(struct ipt_same_info)),
203	.help		= SAME_help,
204	.init		= SAME_init,
205	.parse		= SAME_parse,
206	.final_check	= SAME_check,
207	.print		= SAME_print,
208	.save		= SAME_save,
209	.extra_opts	= SAME_opts,
210};
211
212void _init(void)
213{
214	xtables_register_target(&same_tg_reg);
215}
216