]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - net/ipv4/netfilter/ipt_NETMAP.c
Merge branch 'x86-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-omap-h63xx.git] / 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 net_device *in,
45           const struct net_device *out, unsigned int hooknum,
46           const struct xt_target *target, const void *targinfo)
47 {
48         struct nf_conn *ct;
49         enum ip_conntrack_info ctinfo;
50         __be32 new_ip, netmask;
51         const struct nf_nat_multi_range_compat *mr = targinfo;
52         struct nf_nat_range newrange;
53
54         NF_CT_ASSERT(hooknum == NF_INET_PRE_ROUTING
55                      || hooknum == NF_INET_POST_ROUTING
56                      || hooknum == NF_INET_LOCAL_OUT);
57         ct = nf_ct_get(skb, &ctinfo);
58
59         netmask = ~(mr->range[0].min_ip ^ mr->range[0].max_ip);
60
61         if (hooknum == NF_INET_PRE_ROUTING || hooknum == NF_INET_LOCAL_OUT)
62                 new_ip = ip_hdr(skb)->daddr & ~netmask;
63         else
64                 new_ip = ip_hdr(skb)->saddr & ~netmask;
65         new_ip |= mr->range[0].min_ip & netmask;
66
67         newrange = ((struct nf_nat_range)
68                 { mr->range[0].flags | IP_NAT_RANGE_MAP_IPS,
69                   new_ip, new_ip,
70                   mr->range[0].min, mr->range[0].max });
71
72         /* Hand modified range to generic setup. */
73         return nf_nat_setup_info(ct, &newrange, HOOK2MANIP(hooknum));
74 }
75
76 static struct xt_target netmap_tg_reg __read_mostly = {
77         .name           = "NETMAP",
78         .family         = AF_INET,
79         .target         = netmap_tg,
80         .targetsize     = sizeof(struct nf_nat_multi_range_compat),
81         .table          = "nat",
82         .hooks          = (1 << NF_INET_PRE_ROUTING) |
83                           (1 << NF_INET_POST_ROUTING) |
84                           (1 << NF_INET_LOCAL_OUT),
85         .checkentry     = netmap_tg_check,
86         .me             = THIS_MODULE
87 };
88
89 static int __init netmap_tg_init(void)
90 {
91         return xt_register_target(&netmap_tg_reg);
92 }
93
94 static void __exit netmap_tg_exit(void)
95 {
96         xt_unregister_target(&netmap_tg_reg);
97 }
98
99 module_init(netmap_tg_init);
100 module_exit(netmap_tg_exit);