libipt_SAME.c revision 05e0b01bd1cd4035893c33c7084164bd8fab37c8
1/* Shared library add-on to iptables to add simple non load-balancing SNAT support. */
2#include <stdio.h>
3#include <netdb.h>
4#include <string.h>
5#include <stdlib.h>
6#include <getopt.h>
7#include <iptables.h>
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>
11
12/* Function which prints out usage message. */
13static void
14help(void)
15{
16	printf(
17"SAME v%s 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"
22" --nodst\n"
23"				Don't use destination-ip in\n"
24"				           source selection\n",
25NETFILTER_VERSION);
26}
27
28static struct option opts[] = {
29	{ "to", 1, 0, '1' },
30	{ "nodst", 0, 0, '2'},
31	{ 0 }
32};
33
34/* Initialize the target. */
35static void
36init(struct ipt_entry_target *t, unsigned int *nfcache)
37{
38	struct ipt_same_info *mr = (struct ipt_same_info *)t->data;
39
40	/* Set default to 0 */
41	mr->rangesize = 0;
42	mr->info = 0;
43	mr->ipnum = 0;
44
45	/* Can't cache this */
46	*nfcache |= NFC_UNKNOWN;
47}
48
49/* Parses range of IPs */
50static void
51parse_to(char *arg, struct ip_nat_range *range)
52{
53	char *dash;
54	struct in_addr *ip;
55
56	range->flags |= IP_NAT_RANGE_MAP_IPS;
57	dash = strchr(arg, '-');
58
59	if (dash)
60		*dash = '\0';
61
62	ip = dotted_to_addr(arg);
63	if (!ip)
64		exit_error(PARAMETER_PROBLEM, "Bad IP address `%s'\n",
65			   arg);
66	range->min_ip = ip->s_addr;
67
68	if (dash) {
69		ip = dotted_to_addr(dash+1);
70		if (!ip)
71			exit_error(PARAMETER_PROBLEM, "Bad IP address `%s'\n",
72				   dash+1);
73	}
74	range->max_ip = ip->s_addr;
75	if (dash)
76		if (range->min_ip > range->max_ip)
77			exit_error(PARAMETER_PROBLEM, "Bad IP range `%s-%s'\n",
78				   arg, dash+1);
79}
80
81#define IPT_SAME_OPT_TO			0x01
82#define IPT_SAME_OPT_NODST		0x02
83
84/* Function which parses command options; returns true if it
85   ate an option */
86static int
87parse(int c, char **argv, int invert, unsigned int *flags,
88      const struct ipt_entry *entry,
89      struct ipt_entry_target **target)
90{
91	struct ipt_same_info *mr
92		= (struct ipt_same_info *)(*target)->data;
93
94	switch (c) {
95	case '1':
96		if (mr->rangesize == IPT_SAME_MAX_RANGE)
97			exit_error(PARAMETER_PROBLEM,
98				   "Too many ranges specified, maximum "
99				   "is %i ranges.\n",
100				   IPT_SAME_MAX_RANGE);
101		if (check_inverse(optarg, &invert))
102			exit_error(PARAMETER_PROBLEM,
103				   "Unexpected `!' after --to");
104
105		parse_to(optarg, &mr->range[mr->rangesize]);
106		mr->rangesize++;
107		*flags |= IPT_SAME_OPT_TO;
108		break;
109
110	case '2':
111		if (*flags & IPT_SAME_OPT_NODST)
112			exit_error(PARAMETER_PROBLEM,
113				   "Can't specify --nodst twice");
114
115		mr->info |= IPT_SAME_NODST;
116		*flags |= IPT_SAME_OPT_NODST;
117		break;
118
119	default:
120		return 0;
121	}
122
123	return 1;
124}
125
126/* Final check; need --to. */
127static void final_check(unsigned int flags)
128{
129	if (!(flags & IPT_SAME_OPT_TO))
130		exit_error(PARAMETER_PROBLEM,
131			   "SAME needs --to");
132}
133
134/* Prints out the targinfo. */
135static void
136print(const struct ipt_ip *ip,
137      const struct ipt_entry_target *target,
138      int numeric)
139{
140	int count;
141	struct ipt_same_info *mr
142		= (struct ipt_same_info *)target->data;
143
144	printf("same:");
145
146	for (count = 0; count < mr->rangesize; count++) {
147		struct ip_nat_range *r = &mr->range[count];
148		struct in_addr a;
149
150		a.s_addr = r->min_ip;
151
152		printf("%s", addr_to_dotted(&a));
153		a.s_addr = r->max_ip;
154
155		if (r->min_ip == r->max_ip)
156			printf(" ");
157		else
158			printf("-%s ", addr_to_dotted(&a));
159	}
160
161	if (mr->info & IPT_SAME_NODST)
162		printf("nodst ");
163}
164
165/* Saves the union ipt_targinfo in parsable form to stdout. */
166static void
167save(const struct ipt_ip *ip, const struct ipt_entry_target *target)
168{
169	int count;
170	struct ipt_same_info *mr
171		= (struct ipt_same_info *)target->data;
172
173	for (count = 0; count < mr->rangesize; count++) {
174		struct ip_nat_range *r = &mr->range[count];
175		struct in_addr a;
176
177		a.s_addr = r->min_ip;
178		printf("--to %s", addr_to_dotted(&a));
179		a.s_addr = r->max_ip;
180
181		if (r->min_ip == r->max_ip)
182			printf(" ");
183		else
184			printf("-%s ", addr_to_dotted(&a));
185	}
186
187	if (mr->info & IPT_SAME_NODST)
188		printf("--nodst ");
189}
190
191static
192struct iptables_target same
193= { NULL,
194    "SAME",
195    NETFILTER_VERSION,
196    IPT_ALIGN(sizeof(struct ipt_same_info)),
197    IPT_ALIGN(sizeof(struct ipt_same_info)),
198    &help,
199    &init,
200    &parse,
201    &final_check,
202    &print,
203    &save,
204    opts
205};
206
207void _init(void)
208{
209	register_target(&same);
210}
211