K2.6 patches and update.
[tomato.git] / release / src-rt / linux / linux-2.6 / net / ipv4 / netfilter / ipt_NETMAP.c
blobec9f294dca30202a0c2d99b70506210477b57d4c
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 */
6 /* (C) 2000-2001 Svenning Soerensen <svenning@post5.tele.dk>
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.
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>
21 #define MODULENAME "NETMAP"
22 MODULE_LICENSE("GPL");
23 MODULE_AUTHOR("Svenning Soerensen <svenning@post5.tele.dk>");
24 MODULE_DESCRIPTION("iptables 1:1 NAT mapping of IP networks target");
26 #if 0
27 #define DEBUGP printk
28 #else
29 #define DEBUGP(format, args...)
30 #endif
32 static int
33 check(const char *tablename,
34 const void *e,
35 const struct xt_target *target,
36 void *targinfo,
37 unsigned int hook_mask)
39 const struct nf_nat_multi_range_compat *mr = targinfo;
41 if (!(mr->range[0].flags & IP_NAT_RANGE_MAP_IPS)) {
42 DEBUGP(MODULENAME":check: bad MAP_IPS.\n");
43 return 0;
45 if (mr->rangesize != 1) {
46 DEBUGP(MODULENAME":check: bad rangesize %u.\n", mr->rangesize);
47 return 0;
49 return 1;
52 static unsigned int
53 target(struct sk_buff *skb,
54 const struct net_device *in,
55 const struct net_device *out,
56 unsigned int hooknum,
57 const struct xt_target *target,
58 const void *targinfo)
60 struct nf_conn *ct;
61 enum ip_conntrack_info ctinfo;
62 __be32 new_ip, netmask;
63 const struct nf_nat_multi_range_compat *mr = targinfo;
64 struct nf_nat_range newrange;
66 NF_CT_ASSERT(hooknum == NF_IP_PRE_ROUTING
67 || hooknum == NF_IP_POST_ROUTING
68 || hooknum == NF_IP_LOCAL_OUT);
69 ct = nf_ct_get(skb, &ctinfo);
71 netmask = ~(mr->range[0].min_ip ^ mr->range[0].max_ip);
73 if (hooknum == NF_IP_PRE_ROUTING || hooknum == NF_IP_LOCAL_OUT)
74 new_ip = ip_hdr(skb)->daddr & ~netmask;
75 else
76 new_ip = ip_hdr(skb)->saddr & ~netmask;
77 new_ip |= mr->range[0].min_ip & netmask;
79 newrange = ((struct nf_nat_range)
80 { mr->range[0].flags | IP_NAT_RANGE_MAP_IPS,
81 new_ip, new_ip,
82 mr->range[0].min, mr->range[0].max });
84 /* Hand modified range to generic setup. */
85 return nf_nat_setup_info(ct, &newrange, hooknum);
88 static struct xt_target target_module = {
89 .name = MODULENAME,
90 .family = AF_INET,
91 .target = target,
92 .targetsize = sizeof(struct nf_nat_multi_range_compat),
93 .table = "nat",
94 .hooks = (1 << NF_IP_PRE_ROUTING) | (1 << NF_IP_POST_ROUTING) |
95 (1 << NF_IP_LOCAL_OUT),
96 .checkentry = check,
97 .me = THIS_MODULE
100 static int __init ipt_netmap_init(void)
102 return xt_register_target(&target_module);
105 static void __exit ipt_netmap_fini(void)
107 xt_unregister_target(&target_module);
110 module_init(ipt_netmap_init);
111 module_exit(ipt_netmap_fini);