1 /* Shared library add-on to iptables to add static NAT support.
2 Author: Svenning Soerensen <svenning@post5.tele.dk>
11 #include <linux/netfilter_ipv4/ip_tables.h>
12 #include <linux/netfilter/nf_nat.h>
14 #define MODULENAME "NETMAP"
16 static const struct option NETMAP_opts[] = {
17 { "to", 1, NULL, '1' },
21 /* Function which prints out usage message. */
22 static void NETMAP_help(void)
24 printf(MODULENAME" target options:\n"
25 " --%s address[/mask]\n"
26 " Network address to map to.\n\n",
31 bits2netmask(int bits)
33 u_int32_t netmask, bm;
35 if (bits >= 32 || bits < 0)
37 for (netmask = 0, bm = 0x80000000; bits; bits--, bm >>= 1)
39 return htonl(netmask);
43 netmask2bits(u_int32_t netmask)
48 netmask = ntohl(netmask);
49 for (bits = 0, bm = 0x80000000; netmask & bm; netmask <<= 1)
52 return -1; /* holes in netmask */
56 /* Initialize the target. */
57 static void NETMAP_init(struct xt_entry_target *t)
59 struct ip_nat_multi_range *mr = (struct ip_nat_multi_range *)t->data;
61 /* Actually, it's 0, but it's ignored at the moment. */
66 /* Parses network address */
68 parse_to(char *arg, struct ip_nat_range *range)
71 const struct in_addr *ip;
75 range->flags |= IP_NAT_RANGE_MAP_IPS;
76 slash = strchr(arg, '/');
80 ip = numeric_to_ipaddr(arg);
82 exit_error(PARAMETER_PROBLEM, "Bad IP address `%s'\n",
84 range->min_ip = ip->s_addr;
86 if (strchr(slash+1, '.')) {
87 ip = numeric_to_ipmask(slash+1);
89 exit_error(PARAMETER_PROBLEM, "Bad netmask `%s'\n",
94 if (string_to_number(slash+1, 0, 32, &bits) == -1)
95 exit_error(PARAMETER_PROBLEM, "Bad netmask `%s'\n",
97 netmask = bits2netmask(bits);
99 /* Don't allow /0 (/1 is probably insane, too) */
101 exit_error(PARAMETER_PROBLEM, "Netmask needed\n");
106 if (range->min_ip & ~netmask) {
109 exit_error(PARAMETER_PROBLEM, "Bad network address `%s'\n",
112 range->max_ip = range->min_ip | ~netmask;
115 /* Function which parses command options; returns true if it
117 static int NETMAP_parse(int c, char **argv, int invert, unsigned int *flags,
118 const void *entry, struct xt_entry_target **target)
120 struct ip_nat_multi_range *mr
121 = (struct ip_nat_multi_range *)(*target)->data;
125 if (check_inverse(optarg, &invert, NULL, 0))
126 exit_error(PARAMETER_PROBLEM,
127 "Unexpected `!' after --%s", NETMAP_opts[0].name);
129 parse_to(optarg, &mr->range[0]);
138 /* Final check; need --to */
139 static void NETMAP_check(unsigned int flags)
142 exit_error(PARAMETER_PROBLEM,
143 MODULENAME" needs --%s", NETMAP_opts[0].name);
146 /* Prints out the targinfo. */
147 static void NETMAP_print(const void *ip, const struct xt_entry_target *target,
150 struct ip_nat_multi_range *mr
151 = (struct ip_nat_multi_range *)target->data;
152 struct ip_nat_range *r = &mr->range[0];
156 a.s_addr = r->min_ip;
157 printf("%s", ipaddr_to_numeric(&a));
158 a.s_addr = ~(r->min_ip ^ r->max_ip);
159 bits = netmask2bits(a.s_addr);
161 printf("/%s", ipaddr_to_numeric(&a));
166 /* Saves the targinfo in parsable form to stdout. */
167 static void NETMAP_save(const void *ip, const struct xt_entry_target *target)
169 printf("--%s ", NETMAP_opts[0].name);
170 NETMAP_print(ip, target, 0);
173 static struct xtables_target netmap_tg_reg = {
175 .version = XTABLES_VERSION,
177 .size = XT_ALIGN(sizeof(struct ip_nat_multi_range)),
178 .userspacesize = XT_ALIGN(sizeof(struct ip_nat_multi_range)),
181 .parse = NETMAP_parse,
182 .final_check = NETMAP_check,
183 .print = NETMAP_print,
185 .extra_opts = NETMAP_opts,
190 xtables_register_target(&netmap_tg_reg);