blob: 68f745c8ebff14eeae06a4f8d10508c740d27ab1 [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>
10
11#define BREAKUP_IP(x) (x)>>24, ((x)>>16) & 0xFF, ((x)>>8) & 0xFF, (x) & 0xFF
12
13/* Function which prints out usage message. */
14static void
15help(void)
16{
17 printf(
18"SAME v%s options:\n"
Harald Welte18f1aff2001-03-25 19:03:23 +000019" --to <ipaddr>-<ipaddr>\n"
Martin Josefssonf419f752001-02-19 21:55:27 +000020" Addresses to map source to.\n",
21NETFILTER_VERSION);
22}
23
24static struct option opts[] = {
Harald Welte18f1aff2001-03-25 19:03:23 +000025 { "to", 1, 0, '1' },
Martin Josefssonf419f752001-02-19 21:55:27 +000026 { 0 }
27};
28
29/* Initialize the target. */
30static void
31init(struct ipt_entry_target *t, unsigned int *nfcache)
32{
33 struct ip_nat_multi_range *mr = (struct ip_nat_multi_range *)t->data;
34
35 /* Actually, it's 0, but it's ignored at the moment. */
36 mr->rangesize = 1;
37
38 /* Can't cache this */
39 *nfcache |= NFC_UNKNOWN;
40}
41
42/* Parses range of IPs */
43static void
44parse_to(char *arg, struct ip_nat_range *range)
45{
46 char *dash;
47 struct in_addr *ip;
48
49 range->flags |= IP_NAT_RANGE_MAP_IPS;
50 dash = strchr(arg, '-');
51 if (dash)
52 *dash = '\0';
53 else
54 exit_error(PARAMETER_PROBLEM, "Bad IP range `%s'\n", arg);
55
56 ip = dotted_to_addr(arg);
57 if (!ip)
58 exit_error(PARAMETER_PROBLEM, "Bad IP address `%s'\n",
59 arg);
60 range->min_ip = ip->s_addr;
61 ip = dotted_to_addr(dash+1);
62 if (!ip)
63 exit_error(PARAMETER_PROBLEM, "Bad IP address `%s'\n",
64 dash+1);
65 range->max_ip = ip->s_addr;
66}
67
68/* Function which parses command options; returns true if it
69 ate an option */
70static int
71parse(int c, char **argv, int invert, unsigned int *flags,
72 const struct ipt_entry *entry,
73 struct ipt_entry_target **target)
74{
75 struct ip_nat_multi_range *mr
76 = (struct ip_nat_multi_range *)(*target)->data;
77
78 switch (c) {
79 case '1':
80 if (check_inverse(optarg, &invert))
81 exit_error(PARAMETER_PROBLEM,
Harald Welte18f1aff2001-03-25 19:03:23 +000082 "Unexpected `!' after --to");
Martin Josefssonf419f752001-02-19 21:55:27 +000083
84 parse_to(optarg, &mr->range[0]);
85 *flags = 1;
86 return 1;
87
88 default:
89 return 0;
90 }
91}
92
Harald Welte18f1aff2001-03-25 19:03:23 +000093/* Final check; need --to. */
Martin Josefssonf419f752001-02-19 21:55:27 +000094static void final_check(unsigned int flags)
95{
96 if (!flags)
97 exit_error(PARAMETER_PROBLEM,
Harald Welte18f1aff2001-03-25 19:03:23 +000098 "SAME needs --to");
Martin Josefssonf419f752001-02-19 21:55:27 +000099}
100
101/* Prints out the targinfo. */
102static void
103print(const struct ipt_ip *ip,
104 const struct ipt_entry_target *target,
105 int numeric)
106{
107 struct ip_nat_multi_range *mr
108 = (struct ip_nat_multi_range *)target->data;
109 struct ip_nat_range *r = &mr->range[0];
110 struct in_addr a;
111
112 a.s_addr = r->min_ip;
113
114 printf("same %s", addr_to_dotted(&a));
115 a.s_addr = r->max_ip;
116 printf("-%s ", addr_to_dotted(&a));
117}
118
119/* Saves the union ipt_targinfo in parsable form to stdout. */
120static void
121save(const struct ipt_ip *ip, const struct ipt_entry_target *target)
122{
123 struct ip_nat_multi_range *mr
124 = (struct ip_nat_multi_range *)target->data;
125 struct ip_nat_range *r = &mr->range[0];
126 struct in_addr a;
127
128 a.s_addr = r->min_ip;
Harald Welte18f1aff2001-03-25 19:03:23 +0000129 printf("--to %s", addr_to_dotted(&a));
Martin Josefssonf419f752001-02-19 21:55:27 +0000130 a.s_addr = r->max_ip;
131 printf("-%s ", addr_to_dotted(&a));
132}
133
134struct iptables_target same
135= { NULL,
136 "SAME",
137 NETFILTER_VERSION,
138 IPT_ALIGN(sizeof(struct ip_nat_multi_range)),
139 IPT_ALIGN(sizeof(struct ip_nat_multi_range)),
140 &help,
141 &init,
142 &parse,
143 &final_check,
144 &print,
145 &save,
146 opts
147};
148
149void _init(void)
150{
151 register_target(&same);
152}