1
2
3
4
5
6
7
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
18static 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
33static 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
45static 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
52static 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
60static unsigned int
61ebt_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
67static unsigned int
68ebt_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
74static 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
95static 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
101static void __net_exit frame_nat_net_pre_exit(struct net *net)
102{
103 ebt_unregister_table_pre_exit(net, "nat", ebt_ops_nat);
104}
105
106static void __net_exit frame_nat_net_exit(struct net *net)
107{
108 ebt_unregister_table(net, net->xt.frame_nat);
109}
110
111static struct pernet_operations frame_nat_net_ops = {
112 .init = frame_nat_net_init,
113 .exit = frame_nat_net_exit,
114 .pre_exit = frame_nat_net_pre_exit,
115};
116
117static int __init ebtable_nat_init(void)
118{
119 return register_pernet_subsys(&frame_nat_net_ops);
120}
121
122static void __exit ebtable_nat_fini(void)
123{
124 unregister_pernet_subsys(&frame_nat_net_ops);
125}
126
127module_init(ebtable_nat_init);
128module_exit(ebtable_nat_fini);
129MODULE_LICENSE("GPL");
130