Merge branch 'next' of git://git.kernel.org/pub/scm/linux/kernel/git/rzhang/linux
[firefly-linux-kernel-4.4.55.git] / net / ipv4 / netfilter / nft_chain_nat_ipv4.c
1 /*
2  * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
3  * Copyright (c) 2012 Pablo Neira Ayuso <pablo@netfilter.org>
4  * Copyright (c) 2012 Intel Corporation
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * Development of this code funded by Astaro AG (http://www.astaro.com/)
11  */
12
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/list.h>
16 #include <linux/skbuff.h>
17 #include <linux/ip.h>
18 #include <linux/netfilter.h>
19 #include <linux/netfilter_ipv4.h>
20 #include <linux/netfilter/nf_tables.h>
21 #include <net/netfilter/nf_conntrack.h>
22 #include <net/netfilter/nf_nat.h>
23 #include <net/netfilter/nf_nat_core.h>
24 #include <net/netfilter/nf_tables.h>
25 #include <net/netfilter/nf_tables_ipv4.h>
26 #include <net/netfilter/nf_nat_l3proto.h>
27 #include <net/ip.h>
28
29 static unsigned int nft_nat_do_chain(const struct nf_hook_ops *ops,
30                                       struct sk_buff *skb,
31                                       const struct net_device *in,
32                                       const struct net_device *out,
33                                       struct nf_conn *ct)
34 {
35         struct nft_pktinfo pkt;
36
37         nft_set_pktinfo_ipv4(&pkt, ops, skb, in, out);
38
39         return nft_do_chain(&pkt, ops);
40 }
41
42 static unsigned int nft_nat_ipv4_fn(const struct nf_hook_ops *ops,
43                                     struct sk_buff *skb,
44                                     const struct net_device *in,
45                                     const struct net_device *out,
46                                     int (*okfn)(struct sk_buff *))
47 {
48         return nf_nat_ipv4_fn(ops, skb, in, out, nft_nat_do_chain);
49 }
50
51 static unsigned int nft_nat_ipv4_in(const struct nf_hook_ops *ops,
52                                     struct sk_buff *skb,
53                                     const struct net_device *in,
54                                     const struct net_device *out,
55                                     int (*okfn)(struct sk_buff *))
56 {
57         return nf_nat_ipv4_in(ops, skb, in, out, nft_nat_do_chain);
58 }
59
60 static unsigned int nft_nat_ipv4_out(const struct nf_hook_ops *ops,
61                                      struct sk_buff *skb,
62                                      const struct net_device *in,
63                                      const struct net_device *out,
64                                      int (*okfn)(struct sk_buff *))
65 {
66         return nf_nat_ipv4_out(ops, skb, in, out, nft_nat_do_chain);
67 }
68
69 static unsigned int nft_nat_ipv4_local_fn(const struct nf_hook_ops *ops,
70                                           struct sk_buff *skb,
71                                           const struct net_device *in,
72                                           const struct net_device *out,
73                                           int (*okfn)(struct sk_buff *))
74 {
75         return nf_nat_ipv4_local_fn(ops, skb, in, out, nft_nat_do_chain);
76 }
77
78 static const struct nf_chain_type nft_chain_nat_ipv4 = {
79         .name           = "nat",
80         .type           = NFT_CHAIN_T_NAT,
81         .family         = NFPROTO_IPV4,
82         .owner          = THIS_MODULE,
83         .hook_mask      = (1 << NF_INET_PRE_ROUTING) |
84                           (1 << NF_INET_POST_ROUTING) |
85                           (1 << NF_INET_LOCAL_OUT) |
86                           (1 << NF_INET_LOCAL_IN),
87         .hooks          = {
88                 [NF_INET_PRE_ROUTING]   = nft_nat_ipv4_in,
89                 [NF_INET_POST_ROUTING]  = nft_nat_ipv4_out,
90                 [NF_INET_LOCAL_OUT]     = nft_nat_ipv4_local_fn,
91                 [NF_INET_LOCAL_IN]      = nft_nat_ipv4_fn,
92         },
93 };
94
95 static int __init nft_chain_nat_init(void)
96 {
97         int err;
98
99         err = nft_register_chain_type(&nft_chain_nat_ipv4);
100         if (err < 0)
101                 return err;
102
103         return 0;
104 }
105
106 static void __exit nft_chain_nat_exit(void)
107 {
108         nft_unregister_chain_type(&nft_chain_nat_ipv4);
109 }
110
111 module_init(nft_chain_nat_init);
112 module_exit(nft_chain_nat_exit);
113
114 MODULE_LICENSE("GPL");
115 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
116 MODULE_ALIAS_NFT_CHAIN(AF_INET, "nat");