GNU Linux-libre 4.14.324-gnu1
[releases.git] / net / bridge / netfilter / ebtable_nat.c
1 /*
2  *  ebtable_nat
3  *
4  *      Authors:
5  *      Bart De Schuymer <bdschuym@pandora.be>
6  *
7  *  April, 2002
8  *
9  */
10
11 #include <linux/netfilter_bridge/ebtables.h>
12 #include <linux/module.h>
13
14 #define NAT_VALID_HOOKS ((1 << NF_BR_PRE_ROUTING) | (1 << NF_BR_LOCAL_OUT) | \
15                          (1 << NF_BR_POST_ROUTING))
16
17 static struct ebt_entries initial_chains[] = {
18         {
19                 .name   = "PREROUTING",
20                 .policy = EBT_ACCEPT,
21         },
22         {
23                 .name   = "OUTPUT",
24                 .policy = EBT_ACCEPT,
25         },
26         {
27                 .name   = "POSTROUTING",
28                 .policy = EBT_ACCEPT,
29         }
30 };
31
32 static struct ebt_replace_kernel initial_table = {
33         .name           = "nat",
34         .valid_hooks    = NAT_VALID_HOOKS,
35         .entries_size   = 3 * sizeof(struct ebt_entries),
36         .hook_entry     = {
37                 [NF_BR_PRE_ROUTING]     = &initial_chains[0],
38                 [NF_BR_LOCAL_OUT]       = &initial_chains[1],
39                 [NF_BR_POST_ROUTING]    = &initial_chains[2],
40         },
41         .entries        = (char *)initial_chains,
42 };
43
44 static const struct ebt_table frame_nat = {
45         .name           = "nat",
46         .table          = &initial_table,
47         .valid_hooks    = NAT_VALID_HOOKS,
48         .me             = THIS_MODULE,
49 };
50
51 static unsigned int
52 ebt_nat_in(void *priv, struct sk_buff *skb,
53            const struct nf_hook_state *state)
54 {
55         return ebt_do_table(skb, state, state->net->xt.frame_nat);
56 }
57
58 static unsigned int
59 ebt_nat_out(void *priv, struct sk_buff *skb,
60             const struct nf_hook_state *state)
61 {
62         return ebt_do_table(skb, state, state->net->xt.frame_nat);
63 }
64
65 static const struct nf_hook_ops ebt_ops_nat[] = {
66         {
67                 .hook           = ebt_nat_out,
68                 .pf             = NFPROTO_BRIDGE,
69                 .hooknum        = NF_BR_LOCAL_OUT,
70                 .priority       = NF_BR_PRI_NAT_DST_OTHER,
71         },
72         {
73                 .hook           = ebt_nat_out,
74                 .pf             = NFPROTO_BRIDGE,
75                 .hooknum        = NF_BR_POST_ROUTING,
76                 .priority       = NF_BR_PRI_NAT_SRC,
77         },
78         {
79                 .hook           = ebt_nat_in,
80                 .pf             = NFPROTO_BRIDGE,
81                 .hooknum        = NF_BR_PRE_ROUTING,
82                 .priority       = NF_BR_PRI_NAT_DST_BRIDGED,
83         },
84 };
85
86 static int __net_init frame_nat_net_init(struct net *net)
87 {
88         return ebt_register_table(net, &frame_nat, ebt_ops_nat,
89                                   &net->xt.frame_nat);
90 }
91
92 static void __net_exit frame_nat_net_exit(struct net *net)
93 {
94         ebt_unregister_table(net, net->xt.frame_nat, ebt_ops_nat);
95 }
96
97 static struct pernet_operations frame_nat_net_ops = {
98         .init = frame_nat_net_init,
99         .exit = frame_nat_net_exit,
100 };
101
102 static int __init ebtable_nat_init(void)
103 {
104         return register_pernet_subsys(&frame_nat_net_ops);
105 }
106
107 static void __exit ebtable_nat_fini(void)
108 {
109         unregister_pernet_subsys(&frame_nat_net_ops);
110 }
111
112 module_init(ebtable_nat_init);
113 module_exit(ebtable_nat_fini);
114 MODULE_LICENSE("GPL");