Linux-libre 4.19.123-gnu
[librecmc/linux-libre.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 int check(const struct ebt_table_info *info, unsigned int valid_hooks)
46 {
47         if (valid_hooks & ~NAT_VALID_HOOKS)
48                 return -EINVAL;
49         return 0;
50 }
51
52 static const struct ebt_table frame_nat = {
53         .name           = "nat",
54         .table          = &initial_table,
55         .valid_hooks    = NAT_VALID_HOOKS,
56         .check          = check,
57         .me             = THIS_MODULE,
58 };
59
60 static unsigned int
61 ebt_nat_in(void *priv, struct sk_buff *skb,
62            const struct nf_hook_state *state)
63 {
64         return ebt_do_table(skb, state, state->net->xt.frame_nat);
65 }
66
67 static unsigned int
68 ebt_nat_out(void *priv, struct sk_buff *skb,
69             const struct nf_hook_state *state)
70 {
71         return ebt_do_table(skb, state, state->net->xt.frame_nat);
72 }
73
74 static const struct nf_hook_ops ebt_ops_nat[] = {
75         {
76                 .hook           = ebt_nat_out,
77                 .pf             = NFPROTO_BRIDGE,
78                 .hooknum        = NF_BR_LOCAL_OUT,
79                 .priority       = NF_BR_PRI_NAT_DST_OTHER,
80         },
81         {
82                 .hook           = ebt_nat_out,
83                 .pf             = NFPROTO_BRIDGE,
84                 .hooknum        = NF_BR_POST_ROUTING,
85                 .priority       = NF_BR_PRI_NAT_SRC,
86         },
87         {
88                 .hook           = ebt_nat_in,
89                 .pf             = NFPROTO_BRIDGE,
90                 .hooknum        = NF_BR_PRE_ROUTING,
91                 .priority       = NF_BR_PRI_NAT_DST_BRIDGED,
92         },
93 };
94
95 static int __net_init frame_nat_net_init(struct net *net)
96 {
97         return ebt_register_table(net, &frame_nat, ebt_ops_nat,
98                                   &net->xt.frame_nat);
99 }
100
101 static void __net_exit frame_nat_net_exit(struct net *net)
102 {
103         ebt_unregister_table(net, net->xt.frame_nat, ebt_ops_nat);
104 }
105
106 static struct pernet_operations frame_nat_net_ops = {
107         .init = frame_nat_net_init,
108         .exit = frame_nat_net_exit,
109 };
110
111 static int __init ebtable_nat_init(void)
112 {
113         return register_pernet_subsys(&frame_nat_net_ops);
114 }
115
116 static void __exit ebtable_nat_fini(void)
117 {
118         unregister_pernet_subsys(&frame_nat_net_ops);
119 }
120
121 module_init(ebtable_nat_init);
122 module_exit(ebtable_nat_fini);
123 MODULE_LICENSE("GPL");