1 /* (C) 1999-2001 Paul `Rusty' Russell
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
10 #include <linux/module.h>
11 #include <linux/netfilter.h>
12 #include <linux/netfilter_ipv4.h>
13 #include <linux/netfilter_ipv4/ip_tables.h>
17 #include <net/netfilter/nf_nat.h>
19 static int __net_init iptable_nat_table_init(struct net *net);
21 static const struct xt_table nf_nat_ipv4_table = {
23 .valid_hooks = (1 << NF_INET_PRE_ROUTING) |
24 (1 << NF_INET_POST_ROUTING) |
25 (1 << NF_INET_LOCAL_OUT) |
26 (1 << NF_INET_LOCAL_IN),
29 .table_init = iptable_nat_table_init,
32 static unsigned int iptable_nat_do_chain(void *priv,
34 const struct nf_hook_state *state)
36 return ipt_do_table(skb, state, state->net->ipv4.nat_table);
39 static const struct nf_hook_ops nf_nat_ipv4_ops[] = {
41 .hook = iptable_nat_do_chain,
43 .hooknum = NF_INET_PRE_ROUTING,
44 .priority = NF_IP_PRI_NAT_DST,
47 .hook = iptable_nat_do_chain,
49 .hooknum = NF_INET_POST_ROUTING,
50 .priority = NF_IP_PRI_NAT_SRC,
53 .hook = iptable_nat_do_chain,
55 .hooknum = NF_INET_LOCAL_OUT,
56 .priority = NF_IP_PRI_NAT_DST,
59 .hook = iptable_nat_do_chain,
61 .hooknum = NF_INET_LOCAL_IN,
62 .priority = NF_IP_PRI_NAT_SRC,
66 static int ipt_nat_register_lookups(struct net *net)
70 for (i = 0; i < ARRAY_SIZE(nf_nat_ipv4_ops); i++) {
71 ret = nf_nat_ipv4_register_fn(net, &nf_nat_ipv4_ops[i]);
74 nf_nat_ipv4_unregister_fn(net, &nf_nat_ipv4_ops[--i]);
83 static void ipt_nat_unregister_lookups(struct net *net)
87 for (i = 0; i < ARRAY_SIZE(nf_nat_ipv4_ops); i++)
88 nf_nat_ipv4_unregister_fn(net, &nf_nat_ipv4_ops[i]);
91 static int __net_init iptable_nat_table_init(struct net *net)
93 struct ipt_replace *repl;
96 if (net->ipv4.nat_table)
99 repl = ipt_alloc_initial_table(&nf_nat_ipv4_table);
102 ret = ipt_register_table(net, &nf_nat_ipv4_table, repl,
103 NULL, &net->ipv4.nat_table);
109 ret = ipt_nat_register_lookups(net);
111 ipt_unregister_table(net, net->ipv4.nat_table, NULL);
112 net->ipv4.nat_table = NULL;
119 static void __net_exit iptable_nat_net_exit(struct net *net)
121 if (!net->ipv4.nat_table)
123 ipt_nat_unregister_lookups(net);
124 ipt_unregister_table(net, net->ipv4.nat_table, NULL);
125 net->ipv4.nat_table = NULL;
128 static struct pernet_operations iptable_nat_net_ops = {
129 .exit = iptable_nat_net_exit,
132 static int __init iptable_nat_init(void)
134 int ret = register_pernet_subsys(&iptable_nat_net_ops);
139 ret = iptable_nat_table_init(&init_net);
141 unregister_pernet_subsys(&iptable_nat_net_ops);
145 static void __exit iptable_nat_exit(void)
147 unregister_pernet_subsys(&iptable_nat_net_ops);
150 module_init(iptable_nat_init);
151 module_exit(iptable_nat_exit);
153 MODULE_LICENSE("GPL");