Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *  ebtable_broute
0004  *
0005  *  Authors:
0006  *  Bart De Schuymer <bdschuym@pandora.be>
0007  *
0008  *  April, 2002
0009  *
0010  *  This table lets you choose between routing and bridging for frames
0011  *  entering on a bridge enslaved nic. This table is traversed before any
0012  *  other ebtables table. See net/bridge/br_input.c.
0013  */
0014 
0015 #include <linux/netfilter_bridge/ebtables.h>
0016 #include <linux/module.h>
0017 #include <linux/if_bridge.h>
0018 
0019 #include "../br_private.h"
0020 
0021 /* EBT_ACCEPT means the frame will be bridged
0022  * EBT_DROP means the frame will be routed
0023  */
0024 static struct ebt_entries initial_chain = {
0025     .name       = "BROUTING",
0026     .policy     = EBT_ACCEPT,
0027 };
0028 
0029 static struct ebt_replace_kernel initial_table = {
0030     .name       = "broute",
0031     .valid_hooks    = 1 << NF_BR_BROUTING,
0032     .entries_size   = sizeof(struct ebt_entries),
0033     .hook_entry = {
0034         [NF_BR_BROUTING]    = &initial_chain,
0035     },
0036     .entries    = (char *)&initial_chain,
0037 };
0038 
0039 static const struct ebt_table broute_table = {
0040     .name       = "broute",
0041     .table      = &initial_table,
0042     .valid_hooks    = 1 << NF_BR_BROUTING,
0043     .me     = THIS_MODULE,
0044 };
0045 
0046 static unsigned int ebt_broute(void *priv, struct sk_buff *skb,
0047                    const struct nf_hook_state *s)
0048 {
0049     struct net_bridge_port *p = br_port_get_rcu(skb->dev);
0050     struct nf_hook_state state;
0051     unsigned char *dest;
0052     int ret;
0053 
0054     if (!p || p->state != BR_STATE_FORWARDING)
0055         return NF_ACCEPT;
0056 
0057     nf_hook_state_init(&state, NF_BR_BROUTING,
0058                NFPROTO_BRIDGE, s->in, NULL, NULL,
0059                s->net, NULL);
0060 
0061     ret = ebt_do_table(priv, skb, &state);
0062     if (ret != NF_DROP)
0063         return ret;
0064 
0065     /* DROP in ebtables -t broute means that the
0066      * skb should be routed, not bridged.
0067      * This is awkward, but can't be changed for compatibility
0068      * reasons.
0069      *
0070      * We map DROP to ACCEPT and set the ->br_netfilter_broute flag.
0071      */
0072     BR_INPUT_SKB_CB(skb)->br_netfilter_broute = 1;
0073 
0074     /* undo PACKET_HOST mangling done in br_input in case the dst
0075      * address matches the logical bridge but not the port.
0076      */
0077     dest = eth_hdr(skb)->h_dest;
0078     if (skb->pkt_type == PACKET_HOST &&
0079         !ether_addr_equal(skb->dev->dev_addr, dest) &&
0080          ether_addr_equal(p->br->dev->dev_addr, dest))
0081         skb->pkt_type = PACKET_OTHERHOST;
0082 
0083     return NF_ACCEPT;
0084 }
0085 
0086 static const struct nf_hook_ops ebt_ops_broute = {
0087     .hook       = ebt_broute,
0088     .pf     = NFPROTO_BRIDGE,
0089     .hooknum    = NF_BR_PRE_ROUTING,
0090     .priority   = NF_BR_PRI_FIRST,
0091 };
0092 
0093 static int broute_table_init(struct net *net)
0094 {
0095     return ebt_register_table(net, &broute_table, &ebt_ops_broute);
0096 }
0097 
0098 static void __net_exit broute_net_pre_exit(struct net *net)
0099 {
0100     ebt_unregister_table_pre_exit(net, "broute");
0101 }
0102 
0103 static void __net_exit broute_net_exit(struct net *net)
0104 {
0105     ebt_unregister_table(net, "broute");
0106 }
0107 
0108 static struct pernet_operations broute_net_ops = {
0109     .exit = broute_net_exit,
0110     .pre_exit = broute_net_pre_exit,
0111 };
0112 
0113 static int __init ebtable_broute_init(void)
0114 {
0115     int ret = ebt_register_template(&broute_table, broute_table_init);
0116 
0117     if (ret)
0118         return ret;
0119 
0120     ret = register_pernet_subsys(&broute_net_ops);
0121     if (ret) {
0122         ebt_unregister_template(&broute_table);
0123         return ret;
0124     }
0125 
0126     return 0;
0127 }
0128 
0129 static void __exit ebtable_broute_fini(void)
0130 {
0131     unregister_pernet_subsys(&broute_net_ops);
0132     ebt_unregister_template(&broute_table);
0133 }
0134 
0135 module_init(ebtable_broute_init);
0136 module_exit(ebtable_broute_fini);
0137 MODULE_LICENSE("GPL");