blob: 007ebc35849e1375743bfeda845b1a5103984d80 [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>
Jan Engelhardt5d9678a2008-11-20 10:15:35 +01007#include <xtables.h>
Martin Josefssonf419f752001-02-19 21:55:27 +00008#include <linux/netfilter_ipv4/ip_tables.h>
Patrick McHardy40d54752007-04-18 07:00:36 +00009#include <linux/netfilter/nf_nat.h>
Martin Josefsson1eb00812004-05-26 15:58:07 +000010/* For 64bit kernel / 32bit userspace */
Jan Engelhardta2a7f2b2008-09-01 14:20:13 +020011#include <linux/netfilter_ipv4/ipt_SAME.h>
Martin Josefssonf419f752001-02-19 21:55:27 +000012
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +000013static void SAME_help(void)
Martin Josefssonf419f752001-02-19 21:55:27 +000014{
15 printf(
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020016"SAME target options:\n"
Harald Welte18f1aff2001-03-25 19:03:23 +000017" --to <ipaddr>-<ipaddr>\n"
Harald Weltecf655eb2001-07-28 18:47:11 +000018" Addresses to map source to.\n"
Harald Welte05e0b012001-08-26 08:18:25 +000019" May be specified more than\n"
20" once for multiple ranges.\n"
Harald Weltecf655eb2001-07-28 18:47:11 +000021" --nodst\n"
22" Don't use destination-ip in\n"
Eric Leblondae4b0b32007-02-24 15:11:33 +000023" source selection\n"
Eric Leblondae4b0b32007-02-24 15:11:33 +000024" --random\n"
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020025" Randomize source port\n");
Martin Josefssonf419f752001-02-19 21:55:27 +000026}
27
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +000028static const struct option SAME_opts[] = {
Patrick McHardy500f4832007-09-08 15:59:04 +000029 { "to", 1, NULL, '1' },
30 { "nodst", 0, NULL, '2'},
31 { "random", 0, NULL, '3' },
Max Kellermann9ee386a2008-01-29 13:48:05 +000032 { .name = NULL }
Martin Josefssonf419f752001-02-19 21:55:27 +000033};
34
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +000035static void SAME_init(struct xt_entry_target *t)
Martin Josefssonf419f752001-02-19 21:55:27 +000036{
Harald Weltecf655eb2001-07-28 18:47:11 +000037 struct ipt_same_info *mr = (struct ipt_same_info *)t->data;
Martin Josefssonf419f752001-02-19 21:55:27 +000038
Harald Welte05e0b012001-08-26 08:18:25 +000039 /* Set default to 0 */
40 mr->rangesize = 0;
Harald Weltecf655eb2001-07-28 18:47:11 +000041 mr->info = 0;
Harald Welte05e0b012001-08-26 08:18:25 +000042 mr->ipnum = 0;
Harald Weltecf655eb2001-07-28 18:47:11 +000043
Martin Josefssonf419f752001-02-19 21:55:27 +000044}
45
46/* Parses range of IPs */
47static void
48parse_to(char *arg, struct ip_nat_range *range)
49{
50 char *dash;
Jan Engelhardtbd943842008-01-20 13:38:08 +000051 const struct in_addr *ip;
Martin Josefssonf419f752001-02-19 21:55:27 +000052
53 range->flags |= IP_NAT_RANGE_MAP_IPS;
54 dash = strchr(arg, '-');
Harald Welte05e0b012001-08-26 08:18:25 +000055
Martin Josefssonf419f752001-02-19 21:55:27 +000056 if (dash)
57 *dash = '\0';
Martin Josefssonf419f752001-02-19 21:55:27 +000058
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +010059 ip = xtables_numeric_to_ipaddr(arg);
Martin Josefssonf419f752001-02-19 21:55:27 +000060 if (!ip)
61 exit_error(PARAMETER_PROBLEM, "Bad IP address `%s'\n",
62 arg);
63 range->min_ip = ip->s_addr;
Harald Welte05e0b012001-08-26 08:18:25 +000064
65 if (dash) {
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +010066 ip = xtables_numeric_to_ipaddr(dash+1);
Harald Welte05e0b012001-08-26 08:18:25 +000067 if (!ip)
68 exit_error(PARAMETER_PROBLEM, "Bad IP address `%s'\n",
69 dash+1);
70 }
Martin Josefssonf419f752001-02-19 21:55:27 +000071 range->max_ip = ip->s_addr;
Harald Welte05e0b012001-08-26 08:18:25 +000072 if (dash)
73 if (range->min_ip > range->max_ip)
74 exit_error(PARAMETER_PROBLEM, "Bad IP range `%s-%s'\n",
75 arg, dash+1);
Martin Josefssonf419f752001-02-19 21:55:27 +000076}
77
Harald Weltecf655eb2001-07-28 18:47:11 +000078#define IPT_SAME_OPT_TO 0x01
79#define IPT_SAME_OPT_NODST 0x02
Patrick McHardye656e262007-04-18 12:56:05 +000080#define IPT_SAME_OPT_RANDOM 0x04
Harald Weltecf655eb2001-07-28 18:47:11 +000081
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +000082static int SAME_parse(int c, char **argv, int invert, unsigned int *flags,
83 const void *entry, struct xt_entry_target **target)
Martin Josefssonf419f752001-02-19 21:55:27 +000084{
Harald Weltecf655eb2001-07-28 18:47:11 +000085 struct ipt_same_info *mr
86 = (struct ipt_same_info *)(*target)->data;
Jan Engelhardt7a236f42008-03-03 12:30:41 +010087 unsigned int count;
Martin Josefssonf419f752001-02-19 21:55:27 +000088
89 switch (c) {
90 case '1':
Harald Welte05e0b012001-08-26 08:18:25 +000091 if (mr->rangesize == IPT_SAME_MAX_RANGE)
Harald Weltecf655eb2001-07-28 18:47:11 +000092 exit_error(PARAMETER_PROBLEM,
Harald Welte05e0b012001-08-26 08:18:25 +000093 "Too many ranges specified, maximum "
94 "is %i ranges.\n",
95 IPT_SAME_MAX_RANGE);
Jan Engelhardt0f16c722009-01-30 04:55:38 +010096 if (xtables_check_inverse(optarg, &invert, NULL, 0))
Martin Josefssonf419f752001-02-19 21:55:27 +000097 exit_error(PARAMETER_PROBLEM,
Harald Welte18f1aff2001-03-25 19:03:23 +000098 "Unexpected `!' after --to");
Martin Josefssonf419f752001-02-19 21:55:27 +000099
Harald Welte05e0b012001-08-26 08:18:25 +0000100 parse_to(optarg, &mr->range[mr->rangesize]);
Patrick McHardye656e262007-04-18 12:56:05 +0000101 /* WTF do we need this for? */
Eric Leblondae4b0b32007-02-24 15:11:33 +0000102 if (*flags & IPT_SAME_OPT_RANDOM)
103 mr->range[mr->rangesize].flags
104 |= IP_NAT_RANGE_PROTO_RANDOM;
Harald Welte05e0b012001-08-26 08:18:25 +0000105 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;
Eric Leblondae4b0b32007-02-24 15:11:33 +0000117
Eric Leblondae4b0b32007-02-24 15:11:33 +0000118 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;
Patrick McHardye656e262007-04-18 12:56:05 +0000123
Martin Josefssonf419f752001-02-19 21:55:27 +0000124 default:
125 return 0;
126 }
Harald Weltecf655eb2001-07-28 18:47:11 +0000127
128 return 1;
Martin Josefssonf419f752001-02-19 21:55:27 +0000129}
130
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000131static void SAME_check(unsigned int flags)
Martin Josefssonf419f752001-02-19 21:55:27 +0000132{
Harald Weltecf655eb2001-07-28 18:47:11 +0000133 if (!(flags & IPT_SAME_OPT_TO))
Martin Josefssonf419f752001-02-19 21:55:27 +0000134 exit_error(PARAMETER_PROBLEM,
Harald Welte18f1aff2001-03-25 19:03:23 +0000135 "SAME needs --to");
Martin Josefssonf419f752001-02-19 21:55:27 +0000136}
137
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000138static void SAME_print(const void *ip, const struct xt_entry_target *target,
139 int numeric)
Martin Josefssonf419f752001-02-19 21:55:27 +0000140{
Jan Engelhardt7a236f42008-03-03 12:30:41 +0100141 unsigned int count;
Harald Weltecf655eb2001-07-28 18:47:11 +0000142 struct ipt_same_info *mr
143 = (struct ipt_same_info *)target->data;
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100144 int random_selection = 0;
Harald Welte05e0b012001-08-26 08:18:25 +0000145
146 printf("same:");
147
148 for (count = 0; count < mr->rangesize; count++) {
149 struct ip_nat_range *r = &mr->range[count];
150 struct in_addr a;
Martin Josefssonf419f752001-02-19 21:55:27 +0000151
Harald Welte05e0b012001-08-26 08:18:25 +0000152 a.s_addr = r->min_ip;
Martin Josefssonf419f752001-02-19 21:55:27 +0000153
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100154 printf("%s", xtables_ipaddr_to_numeric(&a));
Harald Welte05e0b012001-08-26 08:18:25 +0000155 a.s_addr = r->max_ip;
156
157 if (r->min_ip == r->max_ip)
158 printf(" ");
159 else
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100160 printf("-%s ", xtables_ipaddr_to_numeric(&a));
Eric Leblondae4b0b32007-02-24 15:11:33 +0000161 if (r->flags & IP_NAT_RANGE_PROTO_RANDOM)
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100162 random_selection = 1;
Harald Welte05e0b012001-08-26 08:18:25 +0000163 }
Harald Weltecf655eb2001-07-28 18:47:11 +0000164
165 if (mr->info & IPT_SAME_NODST)
166 printf("nodst ");
Eric Leblondae4b0b32007-02-24 15:11:33 +0000167
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100168 if (random_selection)
Eric Leblondae4b0b32007-02-24 15:11:33 +0000169 printf("random ");
Martin Josefssonf419f752001-02-19 21:55:27 +0000170}
171
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000172static void SAME_save(const void *ip, const struct xt_entry_target *target)
Martin Josefssonf419f752001-02-19 21:55:27 +0000173{
Jan Engelhardt7a236f42008-03-03 12:30:41 +0100174 unsigned int count;
Harald Weltecf655eb2001-07-28 18:47:11 +0000175 struct ipt_same_info *mr
176 = (struct ipt_same_info *)target->data;
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100177 int random_selection = 0;
Martin Josefssonf419f752001-02-19 21:55:27 +0000178
Harald Welte05e0b012001-08-26 08:18:25 +0000179 for (count = 0; count < mr->rangesize; count++) {
180 struct ip_nat_range *r = &mr->range[count];
181 struct in_addr a;
182
183 a.s_addr = r->min_ip;
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100184 printf("--to %s", xtables_ipaddr_to_numeric(&a));
Harald Welte05e0b012001-08-26 08:18:25 +0000185 a.s_addr = r->max_ip;
186
187 if (r->min_ip == r->max_ip)
188 printf(" ");
189 else
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100190 printf("-%s ", xtables_ipaddr_to_numeric(&a));
Patrick McHardy9c67def2007-04-18 14:00:11 +0000191 if (r->flags & IP_NAT_RANGE_PROTO_RANDOM)
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100192 random_selection = 1;
Harald Welte05e0b012001-08-26 08:18:25 +0000193 }
Harald Weltecf655eb2001-07-28 18:47:11 +0000194
195 if (mr->info & IPT_SAME_NODST)
196 printf("--nodst ");
Patrick McHardy9c67def2007-04-18 14:00:11 +0000197
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100198 if (random_selection)
Patrick McHardy9c67def2007-04-18 14:00:11 +0000199 printf("--random ");
Martin Josefssonf419f752001-02-19 21:55:27 +0000200}
201
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200202static struct xtables_target same_tg_reg = {
Pablo Neira8caee8b2004-12-28 13:11:59 +0000203 .name = "SAME",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200204 .version = XTABLES_VERSION,
Jan Engelhardt03d99482008-11-18 12:27:54 +0100205 .family = NFPROTO_IPV4,
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200206 .size = XT_ALIGN(sizeof(struct ipt_same_info)),
207 .userspacesize = XT_ALIGN(sizeof(struct ipt_same_info)),
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000208 .help = SAME_help,
209 .init = SAME_init,
210 .parse = SAME_parse,
211 .final_check = SAME_check,
212 .print = SAME_print,
213 .save = SAME_save,
214 .extra_opts = SAME_opts,
Martin Josefssonf419f752001-02-19 21:55:27 +0000215};
216
217void _init(void)
218{
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200219 xtables_register_target(&same_tg_reg);
Martin Josefssonf419f752001-02-19 21:55:27 +0000220}