blob: 944fe677652e917e96f2484c4370a5d795bb3b13 [file] [log] [blame]
Marc Bouchere6869a82000-03-20 06:03:29 +00001/* Shared library add-on to iptables to add source-NAT support. */
2#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>
Marc Bouchere6869a82000-03-20 06:03:29 +00008#include <iptables.h>
Jan Engelhardt4e418542009-02-21 03:46:37 +01009#include <limits.h> /* INT_MAX in ip_tables.h */
Marc Bouchere6869a82000-03-20 06:03:29 +000010#include <linux/netfilter_ipv4/ip_tables.h>
Jan Engelhardt978e27e2009-02-21 04:42:32 +010011#include <net/netfilter/nf_nat.h>
Marc Bouchere6869a82000-03-20 06:03:29 +000012
Eric Leblondae4b0b32007-02-24 15:11:33 +000013#define IPT_SNAT_OPT_SOURCE 0x01
Patrick McHardye656e262007-04-18 12:56:05 +000014#define IPT_SNAT_OPT_RANDOM 0x02
Eric Leblondae4b0b32007-02-24 15:11:33 +000015
Marc Bouchere6869a82000-03-20 06:03:29 +000016/* Source NAT data consists of a multi-range, indicating where to map
17 to. */
18struct ipt_natinfo
19{
Yasuyuki KOZAKAI193df8e2007-07-24 05:57:28 +000020 struct xt_entry_target t;
Jan Engelhardt978e27e2009-02-21 04:42:32 +010021 struct nf_nat_multi_range mr;
Marc Bouchere6869a82000-03-20 06:03:29 +000022};
23
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +000024static void SNAT_help(void)
Marc Bouchere6869a82000-03-20 06:03:29 +000025{
26 printf(
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020027"SNAT target options:\n"
Patrick McHardyef399a32007-05-29 11:24:45 +000028" --to-source <ipaddr>[-<ipaddr>][:port-port]\n"
Marc Bouchere6869a82000-03-20 06:03:29 +000029" Address to map source to.\n"
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020030"[--random]\n");
Marc Bouchere6869a82000-03-20 06:03:29 +000031}
32
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +000033static const struct option SNAT_opts[] = {
Patrick McHardy500f4832007-09-08 15:59:04 +000034 { "to-source", 1, NULL, '1' },
35 { "random", 0, NULL, '2' },
Max Kellermann9ee386a2008-01-29 13:48:05 +000036 { .name = NULL }
Marc Bouchere6869a82000-03-20 06:03:29 +000037};
38
Marc Bouchere6869a82000-03-20 06:03:29 +000039static struct ipt_natinfo *
Jan Engelhardt978e27e2009-02-21 04:42:32 +010040append_range(struct ipt_natinfo *info, const struct nf_nat_range *range)
Marc Bouchere6869a82000-03-20 06:03:29 +000041{
42 unsigned int size;
43
44 /* One rangesize already in struct ipt_natinfo */
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020045 size = XT_ALIGN(sizeof(*info) + info->mr.rangesize * sizeof(*range));
Marc Bouchere6869a82000-03-20 06:03:29 +000046
47 info = realloc(info, size);
48 if (!info)
Jan Engelhardt1829ed42009-02-21 03:29:44 +010049 xtables_error(OTHER_PROBLEM, "Out of memory\n");
Marc Bouchere6869a82000-03-20 06:03:29 +000050
Rusty Russell228e98d2000-04-27 10:28:06 +000051 info->t.u.target_size = size;
Marc Bouchere6869a82000-03-20 06:03:29 +000052 info->mr.range[info->mr.rangesize] = *range;
53 info->mr.rangesize++;
54
55 return info;
56}
57
58/* Ranges expected in network order. */
Yasuyuki KOZAKAI193df8e2007-07-24 05:57:28 +000059static struct xt_entry_target *
Marc Bouchere6869a82000-03-20 06:03:29 +000060parse_to(char *arg, int portok, struct ipt_natinfo *info)
61{
Jan Engelhardt978e27e2009-02-21 04:42:32 +010062 struct nf_nat_range range;
Harald Weltede5ba5d2005-02-01 15:14:15 +000063 char *colon, *dash, *error;
Jan Engelhardtbd943842008-01-20 13:38:08 +000064 const struct in_addr *ip;
Marc Bouchere6869a82000-03-20 06:03:29 +000065
66 memset(&range, 0, sizeof(range));
67 colon = strchr(arg, ':');
68
69 if (colon) {
70 int port;
71
72 if (!portok)
Jan Engelhardt1829ed42009-02-21 03:29:44 +010073 xtables_error(PARAMETER_PROBLEM,
Patrick McHardy5a942f92008-11-04 13:22:40 +010074 "Need TCP, UDP, SCTP or DCCP with port specification");
Marc Bouchere6869a82000-03-20 06:03:29 +000075
76 range.flags |= IP_NAT_RANGE_PROTO_SPECIFIED;
77
78 port = atoi(colon+1);
Yasuyuki KOZAKAIa3a9c0d2005-06-22 12:22:44 +000079 if (port <= 0 || port > 65535)
Jan Engelhardt1829ed42009-02-21 03:29:44 +010080 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +000081 "Port `%s' not valid\n", colon+1);
82
Harald Weltede5ba5d2005-02-01 15:14:15 +000083 error = strchr(colon+1, ':');
84 if (error)
Jan Engelhardt1829ed42009-02-21 03:29:44 +010085 xtables_error(PARAMETER_PROBLEM,
Harald Weltede5ba5d2005-02-01 15:14:15 +000086 "Invalid port:port syntax - use dash\n");
87
Marc Bouchere6869a82000-03-20 06:03:29 +000088 dash = strchr(colon, '-');
89 if (!dash) {
90 range.min.tcp.port
91 = range.max.tcp.port
92 = htons(port);
93 } else {
94 int maxport;
95
96 maxport = atoi(dash + 1);
Yasuyuki KOZAKAIa3a9c0d2005-06-22 12:22:44 +000097 if (maxport <= 0 || maxport > 65535)
Jan Engelhardt1829ed42009-02-21 03:29:44 +010098 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +000099 "Port `%s' not valid\n", dash+1);
100 if (maxport < port)
101 /* People are stupid. */
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100102 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +0000103 "Port range `%s' funky\n", colon+1);
104 range.min.tcp.port = htons(port);
105 range.max.tcp.port = htons(maxport);
106 }
107 /* Starts with a colon? No IP info...*/
108 if (colon == arg)
109 return &(append_range(info, &range)->t);
110 *colon = '\0';
111 }
112
113 range.flags |= IP_NAT_RANGE_MAP_IPS;
114 dash = strchr(arg, '-');
115 if (colon && dash && dash > colon)
116 dash = NULL;
117
118 if (dash)
119 *dash = '\0';
120
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +0100121 ip = xtables_numeric_to_ipaddr(arg);
Marc Bouchere6869a82000-03-20 06:03:29 +0000122 if (!ip)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100123 xtables_error(PARAMETER_PROBLEM, "Bad IP address \"%s\"\n",
Marc Bouchere6869a82000-03-20 06:03:29 +0000124 arg);
125 range.min_ip = ip->s_addr;
126 if (dash) {
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +0100127 ip = xtables_numeric_to_ipaddr(dash+1);
Marc Bouchere6869a82000-03-20 06:03:29 +0000128 if (!ip)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100129 xtables_error(PARAMETER_PROBLEM, "Bad IP address \"%s\"\n",
Marc Bouchere6869a82000-03-20 06:03:29 +0000130 dash+1);
131 range.max_ip = ip->s_addr;
132 } else
133 range.max_ip = range.min_ip;
134
135 return &(append_range(info, &range)->t);
136}
137
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000138static int SNAT_parse(int c, char **argv, int invert, unsigned int *flags,
139 const void *e, struct xt_entry_target **target)
Marc Bouchere6869a82000-03-20 06:03:29 +0000140{
Yasuyuki KOZAKAIac8b2712007-07-24 06:06:59 +0000141 const struct ipt_entry *entry = e;
Marc Bouchere6869a82000-03-20 06:03:29 +0000142 struct ipt_natinfo *info = (void *)*target;
143 int portok;
144
145 if (entry->ip.proto == IPPROTO_TCP
Patrick McHardy36d870c2005-07-22 06:39:45 +0000146 || entry->ip.proto == IPPROTO_UDP
Patrick McHardy5a942f92008-11-04 13:22:40 +0100147 || entry->ip.proto == IPPROTO_SCTP
148 || entry->ip.proto == IPPROTO_DCCP
Patrick McHardy36d870c2005-07-22 06:39:45 +0000149 || entry->ip.proto == IPPROTO_ICMP)
Marc Bouchere6869a82000-03-20 06:03:29 +0000150 portok = 1;
151 else
152 portok = 0;
153
154 switch (c) {
155 case '1':
Jan Engelhardt0f16c722009-01-30 04:55:38 +0100156 if (xtables_check_inverse(optarg, &invert, NULL, 0))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100157 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +0000158 "Unexpected `!' after --to-source");
159
Eric Leblondae4b0b32007-02-24 15:11:33 +0000160 if (*flags & IPT_SNAT_OPT_SOURCE) {
Phil Oester8cf65912005-09-19 15:00:33 +0000161 if (!kernel_version)
162 get_kernel_version();
163 if (kernel_version > LINUX_VERSION(2, 6, 10))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100164 xtables_error(PARAMETER_PROBLEM,
Phil Oester8cf65912005-09-19 15:00:33 +0000165 "Multiple --to-source not supported");
166 }
Marc Bouchere6869a82000-03-20 06:03:29 +0000167 *target = parse_to(optarg, portok, info);
Patrick McHardye656e262007-04-18 12:56:05 +0000168 /* WTF do we need this for?? */
Eric Leblondae4b0b32007-02-24 15:11:33 +0000169 if (*flags & IPT_SNAT_OPT_RANDOM)
Patrick McHardyef399a32007-05-29 11:24:45 +0000170 info->mr.range[0].flags |= IP_NAT_RANGE_PROTO_RANDOM;
171 *flags |= IPT_SNAT_OPT_SOURCE;
Marc Bouchere6869a82000-03-20 06:03:29 +0000172 return 1;
173
Eric Leblondae4b0b32007-02-24 15:11:33 +0000174 case '2':
175 if (*flags & IPT_SNAT_OPT_SOURCE) {
Patrick McHardyef399a32007-05-29 11:24:45 +0000176 info->mr.range[0].flags |= IP_NAT_RANGE_PROTO_RANDOM;
Eric Leblondae4b0b32007-02-24 15:11:33 +0000177 *flags |= IPT_SNAT_OPT_RANDOM;
178 } else
179 *flags |= IPT_SNAT_OPT_RANDOM;
180 return 1;
Eric Leblondae4b0b32007-02-24 15:11:33 +0000181
Marc Bouchere6869a82000-03-20 06:03:29 +0000182 default:
183 return 0;
184 }
185}
186
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000187static void SNAT_check(unsigned int flags)
Marc Bouchere6869a82000-03-20 06:03:29 +0000188{
Eric Leblondae4b0b32007-02-24 15:11:33 +0000189 if (!(flags & IPT_SNAT_OPT_SOURCE))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100190 xtables_error(PARAMETER_PROBLEM,
Marc Bouchere6869a82000-03-20 06:03:29 +0000191 "You must specify --to-source");
192}
193
Jan Engelhardt978e27e2009-02-21 04:42:32 +0100194static void print_range(const struct nf_nat_range *r)
Marc Bouchere6869a82000-03-20 06:03:29 +0000195{
196 if (r->flags & IP_NAT_RANGE_MAP_IPS) {
197 struct in_addr a;
198
199 a.s_addr = r->min_ip;
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100200 printf("%s", xtables_ipaddr_to_numeric(&a));
Marc Bouchere6869a82000-03-20 06:03:29 +0000201 if (r->max_ip != r->min_ip) {
202 a.s_addr = r->max_ip;
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100203 printf("-%s", xtables_ipaddr_to_numeric(&a));
Marc Bouchere6869a82000-03-20 06:03:29 +0000204 }
205 }
206 if (r->flags & IP_NAT_RANGE_PROTO_SPECIFIED) {
207 printf(":");
208 printf("%hu", ntohs(r->min.tcp.port));
209 if (r->max.tcp.port != r->min.tcp.port)
210 printf("-%hu", ntohs(r->max.tcp.port));
211 }
212}
213
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000214static void SNAT_print(const void *ip, const struct xt_entry_target *target,
215 int numeric)
Marc Bouchere6869a82000-03-20 06:03:29 +0000216{
217 struct ipt_natinfo *info = (void *)target;
218 unsigned int i = 0;
219
220 printf("to:");
221 for (i = 0; i < info->mr.rangesize; i++) {
222 print_range(&info->mr.range[i]);
223 printf(" ");
Patrick McHardy9c67def2007-04-18 14:00:11 +0000224 if (info->mr.range[i].flags & IP_NAT_RANGE_PROTO_RANDOM)
225 printf("random ");
Marc Bouchere6869a82000-03-20 06:03:29 +0000226 }
227}
228
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000229static void SNAT_save(const void *ip, const struct xt_entry_target *target)
Marc Bouchere6869a82000-03-20 06:03:29 +0000230{
231 struct ipt_natinfo *info = (void *)target;
232 unsigned int i = 0;
233
234 for (i = 0; i < info->mr.rangesize; i++) {
235 printf("--to-source ");
236 print_range(&info->mr.range[i]);
237 printf(" ");
Patrick McHardy9c67def2007-04-18 14:00:11 +0000238 if (info->mr.range[i].flags & IP_NAT_RANGE_PROTO_RANDOM)
239 printf("--random ");
Marc Bouchere6869a82000-03-20 06:03:29 +0000240 }
241}
242
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200243static struct xtables_target snat_tg_reg = {
Pablo Neira8caee8b2004-12-28 13:11:59 +0000244 .name = "SNAT",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200245 .version = XTABLES_VERSION,
Jan Engelhardt03d99482008-11-18 12:27:54 +0100246 .family = NFPROTO_IPV4,
Jan Engelhardt978e27e2009-02-21 04:42:32 +0100247 .size = XT_ALIGN(sizeof(struct nf_nat_multi_range)),
248 .userspacesize = XT_ALIGN(sizeof(struct nf_nat_multi_range)),
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000249 .help = SNAT_help,
250 .parse = SNAT_parse,
251 .final_check = SNAT_check,
252 .print = SNAT_print,
253 .save = SNAT_save,
254 .extra_opts = SNAT_opts,
Marc Bouchere6869a82000-03-20 06:03:29 +0000255};
256
257void _init(void)
258{
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200259 xtables_register_target(&snat_tg_reg);
Marc Bouchere6869a82000-03-20 06:03:29 +0000260}