0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #include <linux/netfilter_bridge/ebtables.h>
0013 #include <uapi/linux/netfilter_bridge.h>
0014 #include <linux/module.h>
0015
0016 #define NAT_VALID_HOOKS ((1 << NF_BR_PRE_ROUTING) | (1 << NF_BR_LOCAL_OUT) | \
0017 (1 << NF_BR_POST_ROUTING))
0018
0019 static struct ebt_entries initial_chains[] = {
0020 {
0021 .name = "PREROUTING",
0022 .policy = EBT_ACCEPT,
0023 },
0024 {
0025 .name = "OUTPUT",
0026 .policy = EBT_ACCEPT,
0027 },
0028 {
0029 .name = "POSTROUTING",
0030 .policy = EBT_ACCEPT,
0031 }
0032 };
0033
0034 static struct ebt_replace_kernel initial_table = {
0035 .name = "nat",
0036 .valid_hooks = NAT_VALID_HOOKS,
0037 .entries_size = 3 * sizeof(struct ebt_entries),
0038 .hook_entry = {
0039 [NF_BR_PRE_ROUTING] = &initial_chains[0],
0040 [NF_BR_LOCAL_OUT] = &initial_chains[1],
0041 [NF_BR_POST_ROUTING] = &initial_chains[2],
0042 },
0043 .entries = (char *)initial_chains,
0044 };
0045
0046 static const struct ebt_table frame_nat = {
0047 .name = "nat",
0048 .table = &initial_table,
0049 .valid_hooks = NAT_VALID_HOOKS,
0050 .me = THIS_MODULE,
0051 };
0052
0053 static const struct nf_hook_ops ebt_ops_nat[] = {
0054 {
0055 .hook = ebt_do_table,
0056 .pf = NFPROTO_BRIDGE,
0057 .hooknum = NF_BR_LOCAL_OUT,
0058 .priority = NF_BR_PRI_NAT_DST_OTHER,
0059 },
0060 {
0061 .hook = ebt_do_table,
0062 .pf = NFPROTO_BRIDGE,
0063 .hooknum = NF_BR_POST_ROUTING,
0064 .priority = NF_BR_PRI_NAT_SRC,
0065 },
0066 {
0067 .hook = ebt_do_table,
0068 .pf = NFPROTO_BRIDGE,
0069 .hooknum = NF_BR_PRE_ROUTING,
0070 .priority = NF_BR_PRI_NAT_DST_BRIDGED,
0071 },
0072 };
0073
0074 static int frame_nat_table_init(struct net *net)
0075 {
0076 return ebt_register_table(net, &frame_nat, ebt_ops_nat);
0077 }
0078
0079 static void __net_exit frame_nat_net_pre_exit(struct net *net)
0080 {
0081 ebt_unregister_table_pre_exit(net, "nat");
0082 }
0083
0084 static void __net_exit frame_nat_net_exit(struct net *net)
0085 {
0086 ebt_unregister_table(net, "nat");
0087 }
0088
0089 static struct pernet_operations frame_nat_net_ops = {
0090 .exit = frame_nat_net_exit,
0091 .pre_exit = frame_nat_net_pre_exit,
0092 };
0093
0094 static int __init ebtable_nat_init(void)
0095 {
0096 int ret = ebt_register_template(&frame_nat, frame_nat_table_init);
0097
0098 if (ret)
0099 return ret;
0100
0101 ret = register_pernet_subsys(&frame_nat_net_ops);
0102 if (ret) {
0103 ebt_unregister_template(&frame_nat);
0104 return ret;
0105 }
0106
0107 return ret;
0108 }
0109
0110 static void __exit ebtable_nat_fini(void)
0111 {
0112 unregister_pernet_subsys(&frame_nat_net_ops);
0113 ebt_unregister_template(&frame_nat);
0114 }
0115
0116 module_init(ebtable_nat_init);
0117 module_exit(ebtable_nat_fini);
0118 MODULE_LICENSE("GPL");