10 #include <linux/module.h>
11 #include <linux/netfilter.h>
12 #include <linux/netfilter_ipv4.h>
13 #include <linux/netfilter_ipv4/ip_tables.h>
21 static const struct xt_table nf_nat_ipv4_table = {
31 static unsigned int alloc_null_binding(
struct nf_conn *
ct,
unsigned int hooknum)
39 pr_debug(
"Allocating NULL binding for %p (%pI4)\n", ct,
47 static unsigned int nf_nat_rule_find(
struct sk_buff *
skb,
unsigned int hooknum,
52 struct net *
net = nf_ct_net(ct);
57 if (!nf_nat_initialized(ct,
HOOK2MANIP(hooknum)))
58 ret = alloc_null_binding(ct, hooknum);
64 nf_nat_ipv4_fn(
unsigned int hooknum,
81 ct = nf_ct_get(skb, &ctinfo);
91 if (nf_ct_is_untracked(ct))
97 if (nf_ct_is_confirmed(ct))
101 pr_debug(
"failed to add NAT extension\n");
121 if (!nf_nat_initialized(ct, maniptype)) {
124 ret = nf_nat_rule_find(skb, hooknum, in, out, ct);
128 pr_debug(
"Already setup manip %s for ct %p\n",
143 nf_nat_ipv4_in(
unsigned int hooknum,
152 ret = nf_nat_ipv4_fn(hooknum, skb, in, out, okfn);
154 daddr != ip_hdr(skb)->daddr)
161 nf_nat_ipv4_out(
unsigned int hooknum,
174 if (skb->
len <
sizeof(
struct iphdr) ||
175 ip_hdrlen(skb) <
sizeof(
struct iphdr))
178 ret = nf_nat_ipv4_fn(hooknum, skb, in, out, okfn);
182 (ct = nf_ct_get(skb, &ctinfo)) !=
NULL) {
185 if ((ct->
tuplehash[dir].tuple.src.u3.ip !=
198 nf_nat_ipv4_local_fn(
unsigned int hooknum,
209 if (skb->
len <
sizeof(
struct iphdr) ||
210 ip_hdrlen(skb) <
sizeof(
struct iphdr))
213 ret = nf_nat_ipv4_fn(hooknum, skb, in, out, okfn);
215 (ct = nf_ct_get(skb, &ctinfo)) !=
NULL) {
218 if (ct->
tuplehash[dir].tuple.dst.u3.ip !=
224 else if (!(
IPCB(skb)->
flags & IPSKB_XFRM_TRANSFORMED) &&
235 static struct nf_hook_ops nf_nat_ipv4_ops[]
__read_mostly = {
238 .hook = nf_nat_ipv4_in,
246 .hook = nf_nat_ipv4_out,
254 .hook = nf_nat_ipv4_local_fn,
262 .hook = nf_nat_ipv4_fn,
270 static int __net_init iptable_nat_net_init(
struct net *net)
279 if (IS_ERR(net->
ipv4.nat_table))
280 return PTR_ERR(net->
ipv4.nat_table);
284 static void __net_exit iptable_nat_net_exit(
struct net *net)
290 .init = iptable_nat_net_init,
291 .exit = iptable_nat_net_exit,
294 static int __init iptable_nat_init(
void)
313 static void __exit iptable_nat_exit(
void)