change cp to $(CP)
[openwrt.git] / target / linux / generic-2.4 / patches / 606-netfilter_NETMAP.patch
1 diff -urN linux-2.4.30.orig/Documentation/Configure.help linux-2.4.30/Documentation/Configure.help
2 --- linux-2.4.30.orig/Documentation/Configure.help      2005-07-01 02:06:36.000000000 +0200
3 +++ linux-2.4.30/Documentation/Configure.help   2005-07-01 00:41:09.000000000 +0200
4 @@ -3086,6 +3086,17 @@
5    If you want to compile it as a module, say M here and read
6    <file:Documentation/modules.txt>.  If unsure, say `N'.
7  
8 +NETMAP target support
9 +CONFIG_IP_NF_TARGET_NETMAP
10 +  NETMAP is an implementation of static 1:1 NAT mapping of network
11 +  addresses. It maps the network address part, while keeping the
12 +  host address part intact. It is similar to Fast NAT, except that
13 +  Netfilter's connection tracking doesn't work well with Fast NAT.
14 +
15 +  If you want to compile it as a module, say M here and read
16 +  Documentation/modules.txt.  The module will be called
17 +  ipt_NETMAP.o.  If unsure, say `N'.
18 +
19  Packet mangling
20  CONFIG_IP_NF_MANGLE
21    This option adds a `mangle' table to iptables: see the man page for
22 diff -urN linux-2.4.30.orig/net/ipv4/netfilter/Config.in linux-2.4.30/net/ipv4/netfilter/Config.in
23 --- linux-2.4.30.orig/net/ipv4/netfilter/Config.in      2005-07-01 02:06:35.000000000 +0200
24 +++ linux-2.4.30/net/ipv4/netfilter/Config.in   2005-07-01 00:41:09.000000000 +0200
25 @@ -69,6 +69,7 @@
26        define_bool CONFIG_IP_NF_NAT_NEEDED y
27        dep_tristate '    MASQUERADE target support' CONFIG_IP_NF_TARGET_MASQUERADE $CONFIG_IP_NF_NAT
28        dep_tristate '    REDIRECT target support' CONFIG_IP_NF_TARGET_REDIRECT $CONFIG_IP_NF_NAT
29 +      dep_tristate '    NETMAP target support' CONFIG_IP_NF_TARGET_NETMAP $CONFIG_IP_NF_NAT
30        if [ "$CONFIG_IP_NF_PPTP" = "m" ]; then
31          define_tristate CONFIG_IP_NF_NAT_PPTP m
32        else
33 diff -urN linux-2.4.30.orig/net/ipv4/netfilter/ipt_NETMAP.c linux-2.4.30/net/ipv4/netfilter/ipt_NETMAP.c
34 --- linux-2.4.30.orig/net/ipv4/netfilter/ipt_NETMAP.c   1970-01-01 01:00:00.000000000 +0100
35 +++ linux-2.4.30/net/ipv4/netfilter/ipt_NETMAP.c        2005-07-01 00:41:09.000000000 +0200
36 @@ -0,0 +1,112 @@
37 +/* NETMAP - static NAT mapping of IP network addresses (1:1).
38 +   The mapping can be applied to source (POSTROUTING),
39 +   destination (PREROUTING), or both (with separate rules).
40 +
41 +   Author: Svenning Soerensen <svenning@post5.tele.dk>
42 +*/
43 +
44 +#include <linux/config.h>
45 +#include <linux/ip.h>
46 +#include <linux/module.h>
47 +#include <linux/netdevice.h>
48 +#include <linux/netfilter.h>
49 +#include <linux/netfilter_ipv4.h>
50 +#include <linux/netfilter_ipv4/ip_nat_rule.h>
51 +
52 +#define MODULENAME "NETMAP"
53 +MODULE_LICENSE("GPL");
54 +MODULE_AUTHOR("Svenning Soerensen <svenning@post5.tele.dk>");
55 +MODULE_DESCRIPTION("iptables 1:1 NAT mapping of IP networks target");
56 +
57 +#if 0
58 +#define DEBUGP printk
59 +#else
60 +#define DEBUGP(format, args...)
61 +#endif
62 +
63 +static int
64 +check(const char *tablename,
65 +      const struct ipt_entry *e,
66 +      void *targinfo,
67 +      unsigned int targinfosize,
68 +      unsigned int hook_mask)
69 +{
70 +       const struct ip_nat_multi_range *mr = targinfo;
71 +
72 +       if (strcmp(tablename, "nat") != 0) {
73 +               DEBUGP(MODULENAME":check: bad table `%s'.\n", tablename);
74 +               return 0;
75 +       }
76 +       if (targinfosize != IPT_ALIGN(sizeof(*mr))) {
77 +               DEBUGP(MODULENAME":check: size %u.\n", targinfosize);
78 +               return 0;
79 +       }
80 +       if (hook_mask & ~((1 << NF_IP_PRE_ROUTING) | (1 << NF_IP_POST_ROUTING))) {
81 +               DEBUGP(MODULENAME":check: bad hooks %x.\n", hook_mask);
82 +               return 0;
83 +       }
84 +       if (!(mr->range[0].flags & IP_NAT_RANGE_MAP_IPS)) {
85 +               DEBUGP(MODULENAME":check: bad MAP_IPS.\n");
86 +               return 0;
87 +       }
88 +       if (mr->rangesize != 1) {
89 +               DEBUGP(MODULENAME":check: bad rangesize %u.\n", mr->rangesize);
90 +               return 0;
91 +       }
92 +       return 1;
93 +}
94 +
95 +static unsigned int
96 +target(struct sk_buff **pskb,
97 +       unsigned int hooknum,
98 +       const struct net_device *in,
99 +       const struct net_device *out,
100 +       const void *targinfo,
101 +       void *userinfo)
102 +{
103 +       struct ip_conntrack *ct;
104 +       enum ip_conntrack_info ctinfo;
105 +       u_int32_t new_ip, netmask;
106 +       const struct ip_nat_multi_range *mr = targinfo;
107 +       struct ip_nat_multi_range newrange;
108 +
109 +       IP_NF_ASSERT(hooknum == NF_IP_PRE_ROUTING
110 +                    || hooknum == NF_IP_POST_ROUTING);
111 +       ct = ip_conntrack_get(*pskb, &ctinfo);
112 +
113 +       netmask = ~(mr->range[0].min_ip ^ mr->range[0].max_ip);
114 +
115 +       if (hooknum == NF_IP_PRE_ROUTING)
116 +               new_ip = (*pskb)->nh.iph->daddr & ~netmask;
117 +       else
118 +               new_ip = (*pskb)->nh.iph->saddr & ~netmask;
119 +       new_ip |= mr->range[0].min_ip & netmask;
120 +
121 +       newrange = ((struct ip_nat_multi_range)
122 +       { 1, { { mr->range[0].flags | IP_NAT_RANGE_MAP_IPS,
123 +                new_ip, new_ip,
124 +                mr->range[0].min, mr->range[0].max } } });
125 +
126 +       /* Hand modified range to generic setup. */
127 +       return ip_nat_setup_info(ct, &newrange, hooknum);
128 +}
129 +
130 +static struct ipt_target target_module = { 
131 +       .name = MODULENAME,
132 +       .target = target,
133 +       .checkentry = check,
134 +       .me = THIS_MODULE
135 +};
136 +
137 +static int __init init(void)
138 +{
139 +       return ipt_register_target(&target_module);
140 +}
141 +
142 +static void __exit fini(void)
143 +{
144 +       ipt_unregister_target(&target_module);
145 +}
146 +
147 +module_init(init);
148 +module_exit(fini);
149 diff -urN linux-2.4.30.orig/net/ipv4/netfilter/Makefile linux-2.4.30/net/ipv4/netfilter/Makefile
150 --- linux-2.4.30.orig/net/ipv4/netfilter/Makefile       2005-07-01 02:06:35.000000000 +0200
151 +++ linux-2.4.30/net/ipv4/netfilter/Makefile    2005-07-01 00:41:09.000000000 +0200
152 @@ -110,6 +110,7 @@
153  obj-$(CONFIG_IP_NF_TARGET_MARK) += ipt_MARK.o
154  obj-$(CONFIG_IP_NF_TARGET_MASQUERADE) += ipt_MASQUERADE.o
155  obj-$(CONFIG_IP_NF_TARGET_REDIRECT) += ipt_REDIRECT.o
156 +obj-$(CONFIG_IP_NF_TARGET_NETMAP) += ipt_NETMAP.o
157  obj-$(CONFIG_IP_NF_NAT_SNMP_BASIC) += ip_nat_snmp_basic.o
158  obj-$(CONFIG_IP_NF_TARGET_LOG) += ipt_LOG.o
159  obj-$(CONFIG_IP_NF_TARGET_TTL) += ipt_TTL.o