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