/net/bridge/netfilter/ebtable_broute.c

http://github.com/mirrors/linux · C · 130 lines · 83 code · 21 blank · 26 comment · 8 complexity · d554af6943085529201f6bd0133fd041 MD5 · raw file

  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ebtable_broute
  4. *
  5. * Authors:
  6. * Bart De Schuymer <bdschuym@pandora.be>
  7. *
  8. * April, 2002
  9. *
  10. * This table lets you choose between routing and bridging for frames
  11. * entering on a bridge enslaved nic. This table is traversed before any
  12. * other ebtables table. See net/bridge/br_input.c.
  13. */
  14. #include <linux/netfilter_bridge/ebtables.h>
  15. #include <linux/module.h>
  16. #include <linux/if_bridge.h>
  17. #include "../br_private.h"
  18. /* EBT_ACCEPT means the frame will be bridged
  19. * EBT_DROP means the frame will be routed
  20. */
  21. static struct ebt_entries initial_chain = {
  22. .name = "BROUTING",
  23. .policy = EBT_ACCEPT,
  24. };
  25. static struct ebt_replace_kernel initial_table = {
  26. .name = "broute",
  27. .valid_hooks = 1 << NF_BR_BROUTING,
  28. .entries_size = sizeof(struct ebt_entries),
  29. .hook_entry = {
  30. [NF_BR_BROUTING] = &initial_chain,
  31. },
  32. .entries = (char *)&initial_chain,
  33. };
  34. static int check(const struct ebt_table_info *info, unsigned int valid_hooks)
  35. {
  36. if (valid_hooks & ~(1 << NF_BR_BROUTING))
  37. return -EINVAL;
  38. return 0;
  39. }
  40. static const struct ebt_table broute_table = {
  41. .name = "broute",
  42. .table = &initial_table,
  43. .valid_hooks = 1 << NF_BR_BROUTING,
  44. .check = check,
  45. .me = THIS_MODULE,
  46. };
  47. static unsigned int ebt_broute(void *priv, struct sk_buff *skb,
  48. const struct nf_hook_state *s)
  49. {
  50. struct net_bridge_port *p = br_port_get_rcu(skb->dev);
  51. struct nf_hook_state state;
  52. unsigned char *dest;
  53. int ret;
  54. if (!p || p->state != BR_STATE_FORWARDING)
  55. return NF_ACCEPT;
  56. nf_hook_state_init(&state, NF_BR_BROUTING,
  57. NFPROTO_BRIDGE, s->in, NULL, NULL,
  58. s->net, NULL);
  59. ret = ebt_do_table(skb, &state, state.net->xt.broute_table);
  60. if (ret != NF_DROP)
  61. return ret;
  62. /* DROP in ebtables -t broute means that the
  63. * skb should be routed, not bridged.
  64. * This is awkward, but can't be changed for compatibility
  65. * reasons.
  66. *
  67. * We map DROP to ACCEPT and set the ->br_netfilter_broute flag.
  68. */
  69. BR_INPUT_SKB_CB(skb)->br_netfilter_broute = 1;
  70. /* undo PACKET_HOST mangling done in br_input in case the dst
  71. * address matches the logical bridge but not the port.
  72. */
  73. dest = eth_hdr(skb)->h_dest;
  74. if (skb->pkt_type == PACKET_HOST &&
  75. !ether_addr_equal(skb->dev->dev_addr, dest) &&
  76. ether_addr_equal(p->br->dev->dev_addr, dest))
  77. skb->pkt_type = PACKET_OTHERHOST;
  78. return NF_ACCEPT;
  79. }
  80. static const struct nf_hook_ops ebt_ops_broute = {
  81. .hook = ebt_broute,
  82. .pf = NFPROTO_BRIDGE,
  83. .hooknum = NF_BR_PRE_ROUTING,
  84. .priority = NF_BR_PRI_FIRST,
  85. };
  86. static int __net_init broute_net_init(struct net *net)
  87. {
  88. return ebt_register_table(net, &broute_table, &ebt_ops_broute,
  89. &net->xt.broute_table);
  90. }
  91. static void __net_exit broute_net_exit(struct net *net)
  92. {
  93. ebt_unregister_table(net, net->xt.broute_table, &ebt_ops_broute);
  94. }
  95. static struct pernet_operations broute_net_ops = {
  96. .init = broute_net_init,
  97. .exit = broute_net_exit,
  98. };
  99. static int __init ebtable_broute_init(void)
  100. {
  101. return register_pernet_subsys(&broute_net_ops);
  102. }
  103. static void __exit ebtable_broute_fini(void)
  104. {
  105. unregister_pernet_subsys(&broute_net_ops);
  106. }
  107. module_init(ebtable_broute_init);
  108. module_exit(ebtable_broute_fini);
  109. MODULE_LICENSE("GPL");