blob: 4eda2237723768b3ab617e5fe3baa385e1d0edf2 [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>
Martin Josefsson1eb00812004-05-26 15:58:07 +000010/* For 64bit kernel / 32bit userspace */
11#include "../include/linux/netfilter_ipv4/ipt_SAME.h"
Martin Josefssonf419f752001-02-19 21:55:27 +000012
Martin Josefssonf419f752001-02-19 21:55:27 +000013/* 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"
Harald Weltecf655eb2001-07-28 18:47:11 +000020" Addresses to map source to.\n"
Harald Welte05e0b012001-08-26 08:18:25 +000021" May be specified more than\n"
22" once for multiple ranges.\n"
Harald Weltecf655eb2001-07-28 18:47:11 +000023" --nodst\n"
24" Don't use destination-ip in\n"
25" source selection\n",
Harald Welte80fe35d2002-05-29 13:08:15 +000026IPTABLES_VERSION);
Martin Josefssonf419f752001-02-19 21:55:27 +000027}
28
29static struct option opts[] = {
Harald Welte18f1aff2001-03-25 19:03:23 +000030 { "to", 1, 0, '1' },
Harald Weltecf655eb2001-07-28 18:47:11 +000031 { "nodst", 0, 0, '2'},
Martin Josefssonf419f752001-02-19 21:55:27 +000032 { 0 }
33};
34
35/* Initialize the target. */
36static void
37init(struct ipt_entry_target *t, unsigned int *nfcache)
38{
Harald Weltecf655eb2001-07-28 18:47:11 +000039 struct ipt_same_info *mr = (struct ipt_same_info *)t->data;
Martin Josefssonf419f752001-02-19 21:55:27 +000040
Harald Welte05e0b012001-08-26 08:18:25 +000041 /* Set default to 0 */
42 mr->rangesize = 0;
Harald Weltecf655eb2001-07-28 18:47:11 +000043 mr->info = 0;
Harald Welte05e0b012001-08-26 08:18:25 +000044 mr->ipnum = 0;
Harald Weltecf655eb2001-07-28 18:47:11 +000045
Martin Josefssonf419f752001-02-19 21:55:27 +000046}
47
48/* Parses range of IPs */
49static void
50parse_to(char *arg, struct ip_nat_range *range)
51{
52 char *dash;
53 struct in_addr *ip;
54
55 range->flags |= IP_NAT_RANGE_MAP_IPS;
56 dash = strchr(arg, '-');
Harald Welte05e0b012001-08-26 08:18:25 +000057
Martin Josefssonf419f752001-02-19 21:55:27 +000058 if (dash)
59 *dash = '\0';
Martin Josefssonf419f752001-02-19 21:55:27 +000060
61 ip = dotted_to_addr(arg);
62 if (!ip)
63 exit_error(PARAMETER_PROBLEM, "Bad IP address `%s'\n",
64 arg);
65 range->min_ip = ip->s_addr;
Harald Welte05e0b012001-08-26 08:18:25 +000066
67 if (dash) {
68 ip = dotted_to_addr(dash+1);
69 if (!ip)
70 exit_error(PARAMETER_PROBLEM, "Bad IP address `%s'\n",
71 dash+1);
72 }
Martin Josefssonf419f752001-02-19 21:55:27 +000073 range->max_ip = ip->s_addr;
Harald Welte05e0b012001-08-26 08:18:25 +000074 if (dash)
75 if (range->min_ip > range->max_ip)
76 exit_error(PARAMETER_PROBLEM, "Bad IP range `%s-%s'\n",
77 arg, dash+1);
Martin Josefssonf419f752001-02-19 21:55:27 +000078}
79
Harald Weltecf655eb2001-07-28 18:47:11 +000080#define IPT_SAME_OPT_TO 0x01
81#define IPT_SAME_OPT_NODST 0x02
82
Martin Josefssonf419f752001-02-19 21:55:27 +000083/* Function which parses command options; returns true if it
84 ate an option */
85static int
86parse(int c, char **argv, int invert, unsigned int *flags,
87 const struct ipt_entry *entry,
88 struct ipt_entry_target **target)
89{
Harald Weltecf655eb2001-07-28 18:47:11 +000090 struct ipt_same_info *mr
91 = (struct ipt_same_info *)(*target)->data;
Martin Josefssonf419f752001-02-19 21:55:27 +000092
93 switch (c) {
94 case '1':
Harald Welte05e0b012001-08-26 08:18:25 +000095 if (mr->rangesize == IPT_SAME_MAX_RANGE)
Harald Weltecf655eb2001-07-28 18:47:11 +000096 exit_error(PARAMETER_PROBLEM,
Harald Welte05e0b012001-08-26 08:18:25 +000097 "Too many ranges specified, maximum "
98 "is %i ranges.\n",
99 IPT_SAME_MAX_RANGE);
Harald Welteb77f1da2002-03-14 11:35:58 +0000100 if (check_inverse(optarg, &invert, NULL, 0))
Martin Josefssonf419f752001-02-19 21:55:27 +0000101 exit_error(PARAMETER_PROBLEM,
Harald Welte18f1aff2001-03-25 19:03:23 +0000102 "Unexpected `!' after --to");
Martin Josefssonf419f752001-02-19 21:55:27 +0000103
Harald Welte05e0b012001-08-26 08:18:25 +0000104 parse_to(optarg, &mr->range[mr->rangesize]);
105 mr->rangesize++;
Harald Weltecf655eb2001-07-28 18:47:11 +0000106 *flags |= IPT_SAME_OPT_TO;
107 break;
108
109 case '2':
110 if (*flags & IPT_SAME_OPT_NODST)
111 exit_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
Martin Josefssonf419f752001-02-19 21:55:27 +0000118 default:
119 return 0;
120 }
Harald Weltecf655eb2001-07-28 18:47:11 +0000121
122 return 1;
Martin Josefssonf419f752001-02-19 21:55:27 +0000123}
124
Harald Welte18f1aff2001-03-25 19:03:23 +0000125/* Final check; need --to. */
Martin Josefssonf419f752001-02-19 21:55:27 +0000126static void final_check(unsigned int flags)
127{
Harald Weltecf655eb2001-07-28 18:47:11 +0000128 if (!(flags & IPT_SAME_OPT_TO))
Martin Josefssonf419f752001-02-19 21:55:27 +0000129 exit_error(PARAMETER_PROBLEM,
Harald Welte18f1aff2001-03-25 19:03:23 +0000130 "SAME needs --to");
Martin Josefssonf419f752001-02-19 21:55:27 +0000131}
132
133/* Prints out the targinfo. */
134static void
135print(const struct ipt_ip *ip,
136 const struct ipt_entry_target *target,
137 int numeric)
138{
Harald Welte05e0b012001-08-26 08:18:25 +0000139 int count;
Harald Weltecf655eb2001-07-28 18:47:11 +0000140 struct ipt_same_info *mr
141 = (struct ipt_same_info *)target->data;
Harald Welte05e0b012001-08-26 08:18:25 +0000142
143 printf("same:");
144
145 for (count = 0; count < mr->rangesize; count++) {
146 struct ip_nat_range *r = &mr->range[count];
147 struct in_addr a;
Martin Josefssonf419f752001-02-19 21:55:27 +0000148
Harald Welte05e0b012001-08-26 08:18:25 +0000149 a.s_addr = r->min_ip;
Martin Josefssonf419f752001-02-19 21:55:27 +0000150
Harald Welte05e0b012001-08-26 08:18:25 +0000151 printf("%s", addr_to_dotted(&a));
152 a.s_addr = r->max_ip;
153
154 if (r->min_ip == r->max_ip)
155 printf(" ");
156 else
157 printf("-%s ", addr_to_dotted(&a));
158 }
Harald Weltecf655eb2001-07-28 18:47:11 +0000159
160 if (mr->info & IPT_SAME_NODST)
161 printf("nodst ");
Martin Josefssonf419f752001-02-19 21:55:27 +0000162}
163
164/* Saves the union ipt_targinfo in parsable form to stdout. */
165static void
166save(const struct ipt_ip *ip, const struct ipt_entry_target *target)
167{
Harald Welte05e0b012001-08-26 08:18:25 +0000168 int count;
Harald Weltecf655eb2001-07-28 18:47:11 +0000169 struct ipt_same_info *mr
170 = (struct ipt_same_info *)target->data;
Martin Josefssonf419f752001-02-19 21:55:27 +0000171
Harald Welte05e0b012001-08-26 08:18:25 +0000172 for (count = 0; count < mr->rangesize; count++) {
173 struct ip_nat_range *r = &mr->range[count];
174 struct in_addr a;
175
176 a.s_addr = r->min_ip;
177 printf("--to %s", addr_to_dotted(&a));
178 a.s_addr = r->max_ip;
179
180 if (r->min_ip == r->max_ip)
181 printf(" ");
182 else
183 printf("-%s ", addr_to_dotted(&a));
184 }
Harald Weltecf655eb2001-07-28 18:47:11 +0000185
186 if (mr->info & IPT_SAME_NODST)
187 printf("--nodst ");
Martin Josefssonf419f752001-02-19 21:55:27 +0000188}
189
Pablo Neira8caee8b2004-12-28 13:11:59 +0000190static struct iptables_target same = {
191 .next = NULL,
192 .name = "SAME",
193 .version = IPTABLES_VERSION,
194 .size = IPT_ALIGN(sizeof(struct ipt_same_info)),
195 .userspacesize = IPT_ALIGN(sizeof(struct ipt_same_info)),
196 .help = &help,
197 .init = &init,
198 .parse = &parse,
199 .final_check = &final_check,
200 .print = &print,
201 .save = &save,
202 .extra_opts = opts
Martin Josefssonf419f752001-02-19 21:55:27 +0000203};
204
205void _init(void)
206{
207 register_target(&same);
208}