netfilter: add protocol independent NAT core
[deliverable/linux.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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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.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 int netmap_tg_check(const struct xt_tgchk_param *par)
26 {
27 const struct nf_nat_ipv4_multi_range_compat *mr = par->targinfo;
28
29 if (!(mr->range[0].flags & NF_NAT_RANGE_MAP_IPS)) {
30 pr_debug("bad MAP_IPS.\n");
31 return -EINVAL;
32 }
33 if (mr->rangesize != 1) {
34 pr_debug("bad rangesize %u.\n", mr->rangesize);
35 return -EINVAL;
36 }
37 return 0;
38 }
39
40 static unsigned int
41 netmap_tg(struct sk_buff *skb, const struct xt_action_param *par)
42 {
43 struct nf_conn *ct;
44 enum ip_conntrack_info ctinfo;
45 __be32 new_ip, netmask;
46 const struct nf_nat_ipv4_multi_range_compat *mr = par->targinfo;
47 struct nf_nat_range newrange;
48
49 NF_CT_ASSERT(par->hooknum == NF_INET_PRE_ROUTING ||
50 par->hooknum == NF_INET_POST_ROUTING ||
51 par->hooknum == NF_INET_LOCAL_OUT ||
52 par->hooknum == NF_INET_LOCAL_IN);
53 ct = nf_ct_get(skb, &ctinfo);
54
55 netmask = ~(mr->range[0].min_ip ^ mr->range[0].max_ip);
56
57 if (par->hooknum == NF_INET_PRE_ROUTING ||
58 par->hooknum == NF_INET_LOCAL_OUT)
59 new_ip = ip_hdr(skb)->daddr & ~netmask;
60 else
61 new_ip = ip_hdr(skb)->saddr & ~netmask;
62 new_ip |= mr->range[0].min_ip & netmask;
63
64 memset(&newrange.min_addr, 0, sizeof(newrange.min_addr));
65 memset(&newrange.max_addr, 0, sizeof(newrange.max_addr));
66 newrange.flags = mr->range[0].flags | NF_NAT_RANGE_MAP_IPS;
67 newrange.min_addr.ip = new_ip;
68 newrange.max_addr.ip = new_ip;
69 newrange.min_proto = mr->range[0].min;
70 newrange.max_proto = mr->range[0].max;
71
72 /* Hand modified range to generic setup. */
73 return nf_nat_setup_info(ct, &newrange, HOOK2MANIP(par->hooknum));
74 }
75
76 static struct xt_target netmap_tg_reg __read_mostly = {
77 .name = "NETMAP",
78 .family = NFPROTO_IPV4,
79 .target = netmap_tg,
80 .targetsize = sizeof(struct nf_nat_ipv4_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 (1 << NF_INET_LOCAL_IN),
86 .checkentry = netmap_tg_check,
87 .me = THIS_MODULE
88 };
89
90 static int __init netmap_tg_init(void)
91 {
92 return xt_register_target(&netmap_tg_reg);
93 }
94
95 static void __exit netmap_tg_exit(void)
96 {
97 xt_unregister_target(&netmap_tg_reg);
98 }
99
100 module_init(netmap_tg_init);
101 module_exit(netmap_tg_exit);
This page took 0.048192 seconds and 5 git commands to generate.