blob: e9c42a80e6edc2b55291bc7b27552c954529fa49 [file] [log] [blame]
Harald Welte18f1aff2001-03-25 19:03:23 +00001/* Shared library add-on to iptables to add simple non load-balancing SNAT support. */
Martin Josefssonf419f752001-02-19 21:55:27 +00002#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>
Harald Weltecf655eb2001-07-28 18:47:11 +000010#include <linux/netfilter_ipv4/ipt_SAME.h>
Martin Josefssonf419f752001-02-19 21:55:27 +000011
Martin Josefssonf419f752001-02-19 21:55:27 +000012/* Function which prints out usage message. */
13static void
14help(void)
15{
16 printf(
17"SAME v%s options:\n"
Harald Welte18f1aff2001-03-25 19:03:23 +000018" --to <ipaddr>-<ipaddr>\n"
Harald Weltecf655eb2001-07-28 18:47:11 +000019" Addresses to map source to.\n"
Harald Welte05e0b012001-08-26 08:18:25 +000020" May be specified more than\n"
21" once for multiple ranges.\n"
Harald Weltecf655eb2001-07-28 18:47:11 +000022" --nodst\n"
23" Don't use destination-ip in\n"
24" source selection\n",
Harald Welte80fe35d2002-05-29 13:08:15 +000025IPTABLES_VERSION);
Martin Josefssonf419f752001-02-19 21:55:27 +000026}
27
28static struct option opts[] = {
Harald Welte18f1aff2001-03-25 19:03:23 +000029 { "to", 1, 0, '1' },
Harald Weltecf655eb2001-07-28 18:47:11 +000030 { "nodst", 0, 0, '2'},
Martin Josefssonf419f752001-02-19 21:55:27 +000031 { 0 }
32};
33
34/* Initialize the target. */
35static void
36init(struct ipt_entry_target *t, unsigned int *nfcache)
37{
Harald Weltecf655eb2001-07-28 18:47:11 +000038 struct ipt_same_info *mr = (struct ipt_same_info *)t->data;
Martin Josefssonf419f752001-02-19 21:55:27 +000039
Harald Welte05e0b012001-08-26 08:18:25 +000040 /* Set default to 0 */
41 mr->rangesize = 0;
Harald Weltecf655eb2001-07-28 18:47:11 +000042 mr->info = 0;
Harald Welte05e0b012001-08-26 08:18:25 +000043 mr->ipnum = 0;
Harald Weltecf655eb2001-07-28 18:47:11 +000044
Martin Josefssonf419f752001-02-19 21:55:27 +000045 /* 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, '-');
Harald Welte05e0b012001-08-26 08:18:25 +000058
Martin Josefssonf419f752001-02-19 21:55:27 +000059 if (dash)
60 *dash = '\0';
Martin Josefssonf419f752001-02-19 21:55:27 +000061
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;
Harald Welte05e0b012001-08-26 08:18:25 +000067
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 }
Martin Josefssonf419f752001-02-19 21:55:27 +000074 range->max_ip = ip->s_addr;
Harald Welte05e0b012001-08-26 08:18:25 +000075 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);
Martin Josefssonf419f752001-02-19 21:55:27 +000079}
80
Harald Weltecf655eb2001-07-28 18:47:11 +000081#define IPT_SAME_OPT_TO 0x01
82#define IPT_SAME_OPT_NODST 0x02
83
Martin Josefssonf419f752001-02-19 21:55:27 +000084/* 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{
Harald Weltecf655eb2001-07-28 18:47:11 +000091 struct ipt_same_info *mr
92 = (struct ipt_same_info *)(*target)->data;
Martin Josefssonf419f752001-02-19 21:55:27 +000093
94 switch (c) {
95 case '1':
Harald Welte05e0b012001-08-26 08:18:25 +000096 if (mr->rangesize == IPT_SAME_MAX_RANGE)
Harald Weltecf655eb2001-07-28 18:47:11 +000097 exit_error(PARAMETER_PROBLEM,
Harald Welte05e0b012001-08-26 08:18:25 +000098 "Too many ranges specified, maximum "
99 "is %i ranges.\n",
100 IPT_SAME_MAX_RANGE);
Harald Welteb77f1da2002-03-14 11:35:58 +0000101 if (check_inverse(optarg, &invert, NULL, 0))
Martin Josefssonf419f752001-02-19 21:55:27 +0000102 exit_error(PARAMETER_PROBLEM,
Harald Welte18f1aff2001-03-25 19:03:23 +0000103 "Unexpected `!' after --to");
Martin Josefssonf419f752001-02-19 21:55:27 +0000104
Harald Welte05e0b012001-08-26 08:18:25 +0000105 parse_to(optarg, &mr->range[mr->rangesize]);
106 mr->rangesize++;
Harald Weltecf655eb2001-07-28 18:47:11 +0000107 *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
Martin Josefssonf419f752001-02-19 21:55:27 +0000119 default:
120 return 0;
121 }
Harald Weltecf655eb2001-07-28 18:47:11 +0000122
123 return 1;
Martin Josefssonf419f752001-02-19 21:55:27 +0000124}
125
Harald Welte18f1aff2001-03-25 19:03:23 +0000126/* Final check; need --to. */
Martin Josefssonf419f752001-02-19 21:55:27 +0000127static void final_check(unsigned int flags)
128{
Harald Weltecf655eb2001-07-28 18:47:11 +0000129 if (!(flags & IPT_SAME_OPT_TO))
Martin Josefssonf419f752001-02-19 21:55:27 +0000130 exit_error(PARAMETER_PROBLEM,
Harald Welte18f1aff2001-03-25 19:03:23 +0000131 "SAME needs --to");
Martin Josefssonf419f752001-02-19 21:55:27 +0000132}
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{
Harald Welte05e0b012001-08-26 08:18:25 +0000140 int count;
Harald Weltecf655eb2001-07-28 18:47:11 +0000141 struct ipt_same_info *mr
142 = (struct ipt_same_info *)target->data;
Harald Welte05e0b012001-08-26 08:18:25 +0000143
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;
Martin Josefssonf419f752001-02-19 21:55:27 +0000149
Harald Welte05e0b012001-08-26 08:18:25 +0000150 a.s_addr = r->min_ip;
Martin Josefssonf419f752001-02-19 21:55:27 +0000151
Harald Welte05e0b012001-08-26 08:18:25 +0000152 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 }
Harald Weltecf655eb2001-07-28 18:47:11 +0000160
161 if (mr->info & IPT_SAME_NODST)
162 printf("nodst ");
Martin Josefssonf419f752001-02-19 21:55:27 +0000163}
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{
Harald Welte05e0b012001-08-26 08:18:25 +0000169 int count;
Harald Weltecf655eb2001-07-28 18:47:11 +0000170 struct ipt_same_info *mr
171 = (struct ipt_same_info *)target->data;
Martin Josefssonf419f752001-02-19 21:55:27 +0000172
Harald Welte05e0b012001-08-26 08:18:25 +0000173 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 }
Harald Weltecf655eb2001-07-28 18:47:11 +0000186
187 if (mr->info & IPT_SAME_NODST)
188 printf("--nodst ");
Martin Josefssonf419f752001-02-19 21:55:27 +0000189}
190
Harald Welte3efb6ea2001-08-06 18:50:21 +0000191static
Martin Josefssonf419f752001-02-19 21:55:27 +0000192struct iptables_target same
193= { NULL,
194 "SAME",
Harald Welte80fe35d2002-05-29 13:08:15 +0000195 IPTABLES_VERSION,
Harald Weltecf655eb2001-07-28 18:47:11 +0000196 IPT_ALIGN(sizeof(struct ipt_same_info)),
197 IPT_ALIGN(sizeof(struct ipt_same_info)),
Martin Josefssonf419f752001-02-19 21:55:27 +0000198 &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}