blob: 0e85dec126f537991855f0e031d4091f54c91ccd [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*/
Jan Engelhardt32b8e612010-07-23 21:16:14 +02004#include <stdbool.h>
Svenning Soerensenb2f9cb72001-05-03 01:15:09 +00005#include <stdio.h>
6#include <netdb.h>
7#include <string.h>
8#include <stdlib.h>
9#include <getopt.h>
Jan Engelhardt5d9678a2008-11-20 10:15:35 +010010#include <xtables.h>
Jan Engelhardt978e27e2009-02-21 04:42:32 +010011#include <net/netfilter/nf_nat.h>
Svenning Soerensenb2f9cb72001-05-03 01:15:09 +000012
13#define MODULENAME "NETMAP"
14
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +000015static const struct option NETMAP_opts[] = {
Jan Engelhardt32b8e612010-07-23 21:16:14 +020016 {.name = "to", .has_arg = true, .val = '1'},
17 XT_GETOPT_TABLEEND,
Svenning Soerensenb2f9cb72001-05-03 01:15:09 +000018};
19
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +000020static void NETMAP_help(void)
Svenning Soerensenb2f9cb72001-05-03 01:15:09 +000021{
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020022 printf(MODULENAME" target options:\n"
Svenning Soerensenb2f9cb72001-05-03 01:15:09 +000023 " --%s address[/mask]\n"
24 " Network address to map to.\n\n",
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +020025 NETMAP_opts[0].name);
Svenning Soerensenb2f9cb72001-05-03 01:15:09 +000026}
27
Jan Engelhardt7ac40522011-01-07 12:34:04 +010028static uint32_t
Svenning Soerensenb2f9cb72001-05-03 01:15:09 +000029bits2netmask(int bits)
30{
Jan Engelhardt7ac40522011-01-07 12:34:04 +010031 uint32_t netmask, bm;
Svenning Soerensenb2f9cb72001-05-03 01:15:09 +000032
33 if (bits >= 32 || bits < 0)
34 return(~0);
35 for (netmask = 0, bm = 0x80000000; bits; bits--, bm >>= 1)
36 netmask |= bm;
37 return htonl(netmask);
38}
39
40static int
Jan Engelhardt7ac40522011-01-07 12:34:04 +010041netmask2bits(uint32_t netmask)
Svenning Soerensenb2f9cb72001-05-03 01:15:09 +000042{
Jan Engelhardt7ac40522011-01-07 12:34:04 +010043 uint32_t bm;
Svenning Soerensenb2f9cb72001-05-03 01:15:09 +000044 int bits;
45
46 netmask = ntohl(netmask);
47 for (bits = 0, bm = 0x80000000; netmask & bm; netmask <<= 1)
48 bits++;
49 if (netmask)
50 return -1; /* holes in netmask */
51 return bits;
52}
53
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +000054static void NETMAP_init(struct xt_entry_target *t)
Svenning Soerensenb2f9cb72001-05-03 01:15:09 +000055{
Jan Engelhardt978e27e2009-02-21 04:42:32 +010056 struct nf_nat_multi_range *mr = (struct nf_nat_multi_range *)t->data;
Svenning Soerensenb2f9cb72001-05-03 01:15:09 +000057
58 /* Actually, it's 0, but it's ignored at the moment. */
59 mr->rangesize = 1;
60
Svenning Soerensenb2f9cb72001-05-03 01:15:09 +000061}
62
63/* Parses network address */
64static void
Jan Engelhardt978e27e2009-02-21 04:42:32 +010065parse_to(char *arg, struct nf_nat_range *range)
Svenning Soerensenb2f9cb72001-05-03 01:15:09 +000066{
67 char *slash;
Jan Engelhardtbd943842008-01-20 13:38:08 +000068 const struct in_addr *ip;
Jan Engelhardt7ac40522011-01-07 12:34:04 +010069 uint32_t netmask;
Harald Welteb4719762001-07-23 02:14:22 +000070 unsigned int bits;
Svenning Soerensenb2f9cb72001-05-03 01:15:09 +000071
72 range->flags |= IP_NAT_RANGE_MAP_IPS;
73 slash = strchr(arg, '/');
74 if (slash)
75 *slash = '\0';
76
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +010077 ip = xtables_numeric_to_ipaddr(arg);
Svenning Soerensenb2f9cb72001-05-03 01:15:09 +000078 if (!ip)
Jan Engelhardt1829ed42009-02-21 03:29:44 +010079 xtables_error(PARAMETER_PROBLEM, "Bad IP address \"%s\"\n",
Svenning Soerensenb2f9cb72001-05-03 01:15:09 +000080 arg);
81 range->min_ip = ip->s_addr;
82 if (slash) {
83 if (strchr(slash+1, '.')) {
Jan Engelhardt1e01b0b2009-01-30 04:20:32 +010084 ip = xtables_numeric_to_ipmask(slash+1);
Svenning Soerensenb2f9cb72001-05-03 01:15:09 +000085 if (!ip)
Jan Engelhardt1829ed42009-02-21 03:29:44 +010086 xtables_error(PARAMETER_PROBLEM, "Bad netmask \"%s\"\n",
Svenning Soerensenb2f9cb72001-05-03 01:15:09 +000087 slash+1);
88 netmask = ip->s_addr;
89 }
90 else {
Jan Engelhardt5f2922c2009-01-27 18:43:01 +010091 if (!xtables_strtoui(slash+1, NULL, &bits, 0, 32))
Jan Engelhardt1829ed42009-02-21 03:29:44 +010092 xtables_error(PARAMETER_PROBLEM, "Bad netmask \"%s\"\n",
Svenning Soerensenb2f9cb72001-05-03 01:15:09 +000093 slash+1);
94 netmask = bits2netmask(bits);
95 }
96 /* Don't allow /0 (/1 is probably insane, too) */
97 if (netmask == 0)
Jan Engelhardt1829ed42009-02-21 03:29:44 +010098 xtables_error(PARAMETER_PROBLEM, "Netmask needed\n");
Svenning Soerensenb2f9cb72001-05-03 01:15:09 +000099 }
100 else
101 netmask = ~0;
102
103 if (range->min_ip & ~netmask) {
104 if (slash)
105 *slash = '/';
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100106 xtables_error(PARAMETER_PROBLEM, "Bad network address \"%s\"\n",
Svenning Soerensenb2f9cb72001-05-03 01:15:09 +0000107 arg);
108 }
109 range->max_ip = range->min_ip | ~netmask;
110}
111
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000112static int NETMAP_parse(int c, char **argv, int invert, unsigned int *flags,
113 const void *entry, struct xt_entry_target **target)
Svenning Soerensenb2f9cb72001-05-03 01:15:09 +0000114{
Jan Engelhardt978e27e2009-02-21 04:42:32 +0100115 struct nf_nat_multi_range *mr
116 = (struct nf_nat_multi_range *)(*target)->data;
Svenning Soerensenb2f9cb72001-05-03 01:15:09 +0000117
118 switch (c) {
119 case '1':
Jan Engelhardtbf971282009-11-03 19:55:11 +0100120 if (xtables_check_inverse(optarg, &invert, NULL, 0, argv))
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100121 xtables_error(PARAMETER_PROBLEM,
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000122 "Unexpected `!' after --%s", NETMAP_opts[0].name);
Svenning Soerensenb2f9cb72001-05-03 01:15:09 +0000123
124 parse_to(optarg, &mr->range[0]);
125 *flags = 1;
126 return 1;
Svenning Soerensenb2f9cb72001-05-03 01:15:09 +0000127 }
Jan Engelhardtd09b6d52011-01-08 03:02:37 +0100128 return 0;
Svenning Soerensenb2f9cb72001-05-03 01:15:09 +0000129}
130
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000131static void NETMAP_check(unsigned int flags)
Svenning Soerensenb2f9cb72001-05-03 01:15:09 +0000132{
133 if (!flags)
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100134 xtables_error(PARAMETER_PROBLEM,
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000135 MODULENAME" needs --%s", NETMAP_opts[0].name);
Svenning Soerensenb2f9cb72001-05-03 01:15:09 +0000136}
137
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000138static void NETMAP_print(const void *ip, const struct xt_entry_target *target,
139 int numeric)
Svenning Soerensenb2f9cb72001-05-03 01:15:09 +0000140{
Jan Engelhardt69f564e2009-05-26 13:14:06 +0200141 const struct nf_nat_multi_range *mr = (const void *)target->data;
142 const struct nf_nat_range *r = &mr->range[0];
Svenning Soerensenb2f9cb72001-05-03 01:15:09 +0000143 struct in_addr a;
144 int bits;
145
146 a.s_addr = r->min_ip;
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100147 printf("%s", xtables_ipaddr_to_numeric(&a));
Svenning Soerensenb2f9cb72001-05-03 01:15:09 +0000148 a.s_addr = ~(r->min_ip ^ r->max_ip);
149 bits = netmask2bits(a.s_addr);
150 if (bits < 0)
Jan Engelhardte44ea7f2009-01-30 03:55:09 +0100151 printf("/%s", xtables_ipaddr_to_numeric(&a));
Svenning Soerensenb2f9cb72001-05-03 01:15:09 +0000152 else
153 printf("/%d", bits);
154}
155
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000156static void NETMAP_save(const void *ip, const struct xt_entry_target *target)
Svenning Soerensenb2f9cb72001-05-03 01:15:09 +0000157{
Jan Engelhardt73866352010-12-18 02:04:59 +0100158 printf(" --%s ", NETMAP_opts[0].name);
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000159 NETMAP_print(ip, target, 0);
Svenning Soerensenb2f9cb72001-05-03 01:15:09 +0000160}
161
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200162static struct xtables_target netmap_tg_reg = {
Pablo Neira8caee8b2004-12-28 13:11:59 +0000163 .name = MODULENAME,
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200164 .version = XTABLES_VERSION,
Jan Engelhardt03d99482008-11-18 12:27:54 +0100165 .family = NFPROTO_IPV4,
Jan Engelhardt978e27e2009-02-21 04:42:32 +0100166 .size = XT_ALIGN(sizeof(struct nf_nat_multi_range)),
167 .userspacesize = XT_ALIGN(sizeof(struct nf_nat_multi_range)),
Jan Engelhardt1d5b63d2007-10-04 16:29:00 +0000168 .help = NETMAP_help,
169 .init = NETMAP_init,
170 .parse = NETMAP_parse,
171 .final_check = NETMAP_check,
172 .print = NETMAP_print,
173 .save = NETMAP_save,
174 .extra_opts = NETMAP_opts,
Svenning Soerensenb2f9cb72001-05-03 01:15:09 +0000175};
176
177void _init(void)
178{
Jan Engelhardt8b7c64d2008-04-15 11:48:25 +0200179 xtables_register_target(&netmap_tg_reg);
Svenning Soerensenb2f9cb72001-05-03 01:15:09 +0000180}