netfilter: xtables: move extension arguments into compound structure (4/6)
[linux-2.6] / net / ipv4 / netfilter / ipt_NETMAP.c
1 /* NETMAP - static NAT mapping of IP network addresses (1:1).
2  * The mapping can be applied to source (POSTROUTING),
3  * destination (PREROUTING), or both (with separate rules).
4  */
5
6 /* (C) 2000-2001 Svenning Soerensen <svenning@post5.tele.dk>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12
13 #include <linux/ip.h>
14 #include <linux/module.h>
15 #include <linux/netdevice.h>
16 #include <linux/netfilter.h>
17 #include <linux/netfilter_ipv4.h>
18 #include <linux/netfilter/x_tables.h>
19 #include <net/netfilter/nf_nat_rule.h>
20
21 MODULE_LICENSE("GPL");
22 MODULE_AUTHOR("Svenning Soerensen <svenning@post5.tele.dk>");
23 MODULE_DESCRIPTION("Xtables: 1:1 NAT mapping of IPv4 subnets");
24
25 static bool
26 netmap_tg_check(const char *tablename, const void *e,
27                 const struct xt_target *target, void *targinfo,
28                 unsigned int hook_mask)
29 {
30         const struct nf_nat_multi_range_compat *mr = targinfo;
31
32         if (!(mr->range[0].flags & IP_NAT_RANGE_MAP_IPS)) {
33                 pr_debug("NETMAP:check: bad MAP_IPS.\n");
34                 return false;
35         }
36         if (mr->rangesize != 1) {
37                 pr_debug("NETMAP:check: bad rangesize %u.\n", mr->rangesize);
38                 return false;
39         }
40         return true;
41 }
42
43 static unsigned int
44 netmap_tg(struct sk_buff *skb, const struct xt_target_param *par)
45 {
46         struct nf_conn *ct;
47         enum ip_conntrack_info ctinfo;
48         __be32 new_ip, netmask;
49         const struct nf_nat_multi_range_compat *mr = par->targinfo;
50         struct nf_nat_range newrange;
51
52         NF_CT_ASSERT(par->hooknum == NF_INET_PRE_ROUTING ||
53                      par->hooknum == NF_INET_POST_ROUTING ||
54                      par->hooknum == NF_INET_LOCAL_OUT);
55         ct = nf_ct_get(skb, &ctinfo);
56
57         netmask = ~(mr->range[0].min_ip ^ mr->range[0].max_ip);
58
59         if (par->hooknum == NF_INET_PRE_ROUTING ||
60             par->hooknum == NF_INET_LOCAL_OUT)
61                 new_ip = ip_hdr(skb)->daddr & ~netmask;
62         else
63                 new_ip = ip_hdr(skb)->saddr & ~netmask;
64         new_ip |= mr->range[0].min_ip & netmask;
65
66         newrange = ((struct nf_nat_range)
67                 { mr->range[0].flags | IP_NAT_RANGE_MAP_IPS,
68                   new_ip, new_ip,
69                   mr->range[0].min, mr->range[0].max });
70
71         /* Hand modified range to generic setup. */
72         return nf_nat_setup_info(ct, &newrange, HOOK2MANIP(par->hooknum));
73 }
74
75 static struct xt_target netmap_tg_reg __read_mostly = {
76         .name           = "NETMAP",
77         .family         = NFPROTO_IPV4,
78         .target         = netmap_tg,
79         .targetsize     = sizeof(struct nf_nat_multi_range_compat),
80         .table          = "nat",
81         .hooks          = (1 << NF_INET_PRE_ROUTING) |
82                           (1 << NF_INET_POST_ROUTING) |
83                           (1 << NF_INET_LOCAL_OUT),
84         .checkentry     = netmap_tg_check,
85         .me             = THIS_MODULE
86 };
87
88 static int __init netmap_tg_init(void)
89 {
90         return xt_register_target(&netmap_tg_reg);
91 }
92
93 static void __exit netmap_tg_exit(void)
94 {
95         xt_unregister_target(&netmap_tg_reg);
96 }
97
98 module_init(netmap_tg_init);
99 module_exit(netmap_tg_exit);