blob: da0e5f4d42dcb7a37437b2836c8e37904904cc45 [file] [log] [blame]
Svenning Soerensenb2f9cb72001-05-03 01:15:09 +00001/* Shared library add-on to iptables to add static NAT support.
2 Author: Svenning Soerensen <svenning@post5.tele.dk>
3*/
4
5#include <stdio.h>
6#include <netdb.h>
7#include <string.h>
8#include <stdlib.h>
9#include <getopt.h>
10#include <iptables.h>
11#include <linux/netfilter_ipv4/ip_tables.h>
Patrick McHardy40d54752007-04-18 07:00:36 +000012#include <linux/netfilter/nf_nat.h>
Svenning Soerensenb2f9cb72001-05-03 01:15:09 +000013
14#define MODULENAME "NETMAP"
15
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +000016static const struct option NETMAP_opts[] = {
Patrick McHardy500f4832007-09-08 15:59:04 +000017 { "to", 1, NULL, '1' },
Max Kellermann9ee386a2008-01-29 13:48:05 +000018 { .name = NULL }
Svenning Soerensenb2f9cb72001-05-03 01:15:09 +000019};
20
21/* Function which prints out usage message. */
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +000022static void NETMAP_help(void)
Svenning Soerensenb2f9cb72001-05-03 01:15:09 +000023{
24 printf(MODULENAME" v%s options:\n"
25 " --%s address[/mask]\n"
26 " Network address to map to.\n\n",
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +000027 IPTABLES_VERSION, NETMAP_opts[0].name);
Svenning Soerensenb2f9cb72001-05-03 01:15:09 +000028}
29
30static u_int32_t
31bits2netmask(int bits)
32{
33 u_int32_t netmask, bm;
34
35 if (bits >= 32 || bits < 0)
36 return(~0);
37 for (netmask = 0, bm = 0x80000000; bits; bits--, bm >>= 1)
38 netmask |= bm;
39 return htonl(netmask);
40}
41
42static int
43netmask2bits(u_int32_t netmask)
44{
45 u_int32_t bm;
46 int bits;
47
48 netmask = ntohl(netmask);
49 for (bits = 0, bm = 0x80000000; netmask & bm; netmask <<= 1)
50 bits++;
51 if (netmask)
52 return -1; /* holes in netmask */
53 return bits;
54}
55
56/* Initialize the target. */
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +000057static void NETMAP_init(struct xt_entry_target *t)
Svenning Soerensenb2f9cb72001-05-03 01:15:09 +000058{
59 struct ip_nat_multi_range *mr = (struct ip_nat_multi_range *)t->data;
60
61 /* Actually, it's 0, but it's ignored at the moment. */
62 mr->rangesize = 1;
63
Svenning Soerensenb2f9cb72001-05-03 01:15:09 +000064}
65
66/* Parses network address */
67static void
68parse_to(char *arg, struct ip_nat_range *range)
69{
70 char *slash;
Jan Engelhardtbd943842008-01-20 13:38:08 +000071 const struct in_addr *ip;
Svenning Soerensenb2f9cb72001-05-03 01:15:09 +000072 u_int32_t netmask;
Harald Welteb4719762001-07-23 02:14:22 +000073 unsigned int bits;
Svenning Soerensenb2f9cb72001-05-03 01:15:09 +000074
75 range->flags |= IP_NAT_RANGE_MAP_IPS;
76 slash = strchr(arg, '/');
77 if (slash)
78 *slash = '\0';
79
Jan Engelhardtbd943842008-01-20 13:38:08 +000080 ip = numeric_to_ipaddr(arg);
Svenning Soerensenb2f9cb72001-05-03 01:15:09 +000081 if (!ip)
82 exit_error(PARAMETER_PROBLEM, "Bad IP address `%s'\n",
83 arg);
84 range->min_ip = ip->s_addr;
85 if (slash) {
86 if (strchr(slash+1, '.')) {
Jan Engelhardtbd943842008-01-20 13:38:08 +000087 ip = numeric_to_ipmask(slash+1);
Svenning Soerensenb2f9cb72001-05-03 01:15:09 +000088 if (!ip)
89 exit_error(PARAMETER_PROBLEM, "Bad netmask `%s'\n",
90 slash+1);
91 netmask = ip->s_addr;
92 }
93 else {
Harald Welteb4719762001-07-23 02:14:22 +000094 if (string_to_number(slash+1, 0, 32, &bits) == -1)
Svenning Soerensenb2f9cb72001-05-03 01:15:09 +000095 exit_error(PARAMETER_PROBLEM, "Bad netmask `%s'\n",
96 slash+1);
97 netmask = bits2netmask(bits);
98 }
99 /* Don't allow /0 (/1 is probably insane, too) */
100 if (netmask == 0)
101 exit_error(PARAMETER_PROBLEM, "Netmask needed\n");
102 }
103 else
104 netmask = ~0;
105
106 if (range->min_ip & ~netmask) {
107 if (slash)
108 *slash = '/';
109 exit_error(PARAMETER_PROBLEM, "Bad network address `%s'\n",
110 arg);
111 }
112 range->max_ip = range->min_ip | ~netmask;
113}
114
115/* Function which parses command options; returns true if it
116 ate an option */
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000117static int NETMAP_parse(int c, char **argv, int invert, unsigned int *flags,
118 const void *entry, struct xt_entry_target **target)
Svenning Soerensenb2f9cb72001-05-03 01:15:09 +0000119{
120 struct ip_nat_multi_range *mr
121 = (struct ip_nat_multi_range *)(*target)->data;
122
123 switch (c) {
124 case '1':
Harald Welteb77f1da2002-03-14 11:35:58 +0000125 if (check_inverse(optarg, &invert, NULL, 0))
Svenning Soerensenb2f9cb72001-05-03 01:15:09 +0000126 exit_error(PARAMETER_PROBLEM,
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000127 "Unexpected `!' after --%s", NETMAP_opts[0].name);
Svenning Soerensenb2f9cb72001-05-03 01:15:09 +0000128
129 parse_to(optarg, &mr->range[0]);
130 *flags = 1;
131 return 1;
132
133 default:
134 return 0;
135 }
136}
137
138/* Final check; need --to */
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000139static void NETMAP_check(unsigned int flags)
Svenning Soerensenb2f9cb72001-05-03 01:15:09 +0000140{
141 if (!flags)
142 exit_error(PARAMETER_PROBLEM,
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000143 MODULENAME" needs --%s", NETMAP_opts[0].name);
Svenning Soerensenb2f9cb72001-05-03 01:15:09 +0000144}
145
146/* Prints out the targinfo. */
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000147static void NETMAP_print(const void *ip, const struct xt_entry_target *target,
148 int numeric)
Svenning Soerensenb2f9cb72001-05-03 01:15:09 +0000149{
150 struct ip_nat_multi_range *mr
151 = (struct ip_nat_multi_range *)target->data;
152 struct ip_nat_range *r = &mr->range[0];
153 struct in_addr a;
154 int bits;
155
156 a.s_addr = r->min_ip;
Jan Engelhardt08b16162008-01-20 13:36:08 +0000157 printf("%s", ipaddr_to_numeric(&a));
Svenning Soerensenb2f9cb72001-05-03 01:15:09 +0000158 a.s_addr = ~(r->min_ip ^ r->max_ip);
159 bits = netmask2bits(a.s_addr);
160 if (bits < 0)
Jan Engelhardt08b16162008-01-20 13:36:08 +0000161 printf("/%s", ipaddr_to_numeric(&a));
Svenning Soerensenb2f9cb72001-05-03 01:15:09 +0000162 else
163 printf("/%d", bits);
164}
165
166/* Saves the targinfo in parsable form to stdout. */
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000167static void NETMAP_save(const void *ip, const struct xt_entry_target *target)
Svenning Soerensenb2f9cb72001-05-03 01:15:09 +0000168{
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000169 printf("--%s ", NETMAP_opts[0].name);
170 NETMAP_print(ip, target, 0);
Svenning Soerensenb2f9cb72001-05-03 01:15:09 +0000171}
172
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000173static struct iptables_target netmap_target = {
Pablo Neira8caee8b2004-12-28 13:11:59 +0000174 .name = MODULENAME,
175 .version = IPTABLES_VERSION,
176 .size = IPT_ALIGN(sizeof(struct ip_nat_multi_range)),
177 .userspacesize = IPT_ALIGN(sizeof(struct ip_nat_multi_range)),
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000178 .help = NETMAP_help,
179 .init = NETMAP_init,
180 .parse = NETMAP_parse,
181 .final_check = NETMAP_check,
182 .print = NETMAP_print,
183 .save = NETMAP_save,
184 .extra_opts = NETMAP_opts,
Svenning Soerensenb2f9cb72001-05-03 01:15:09 +0000185};
186
187void _init(void)
188{
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000189 register_target(&netmap_target);
Svenning Soerensenb2f9cb72001-05-03 01:15:09 +0000190}
191