ebt_redirect.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * ebt_redirect
  3. *
  4. * Authors:
  5. * Bart De Schuymer <bdschuym@pandora.be>
  6. *
  7. * April, 2002
  8. *
  9. */
  10. #include <linux/module.h>
  11. #include <net/sock.h>
  12. #include "../br_private.h"
  13. #include <linux/netfilter.h>
  14. #include <linux/netfilter/x_tables.h>
  15. #include <linux/netfilter_bridge/ebtables.h>
  16. #include <linux/netfilter_bridge/ebt_redirect.h>
  17. static unsigned int
  18. ebt_redirect_tg(struct sk_buff *skb, const struct xt_action_param *par)
  19. {
  20. const struct ebt_redirect_info *info = par->targinfo;
  21. if (!skb_make_writable(skb, 0))
  22. return EBT_DROP;
  23. if (par->hooknum != NF_BR_BROUTING)
  24. /* rcu_read_lock()ed by nf_hook_slow */
  25. ether_addr_copy(eth_hdr(skb)->h_dest,
  26. br_port_get_rcu(par->in)->br->dev->dev_addr);
  27. else
  28. ether_addr_copy(eth_hdr(skb)->h_dest, par->in->dev_addr);
  29. skb->pkt_type = PACKET_HOST;
  30. return info->target;
  31. }
  32. static int ebt_redirect_tg_check(const struct xt_tgchk_param *par)
  33. {
  34. const struct ebt_redirect_info *info = par->targinfo;
  35. unsigned int hook_mask;
  36. if (BASE_CHAIN && info->target == EBT_RETURN)
  37. return -EINVAL;
  38. hook_mask = par->hook_mask & ~(1 << NF_BR_NUMHOOKS);
  39. if ((strcmp(par->table, "nat") != 0 ||
  40. hook_mask & ~(1 << NF_BR_PRE_ROUTING)) &&
  41. (strcmp(par->table, "broute") != 0 ||
  42. hook_mask & ~(1 << NF_BR_BROUTING)))
  43. return -EINVAL;
  44. if (INVALID_TARGET)
  45. return -EINVAL;
  46. return 0;
  47. }
  48. static struct xt_target ebt_redirect_tg_reg __read_mostly = {
  49. .name = "redirect",
  50. .revision = 0,
  51. .family = NFPROTO_BRIDGE,
  52. .hooks = (1 << NF_BR_NUMHOOKS) | (1 << NF_BR_PRE_ROUTING) |
  53. (1 << NF_BR_BROUTING),
  54. .target = ebt_redirect_tg,
  55. .checkentry = ebt_redirect_tg_check,
  56. .targetsize = sizeof(struct ebt_redirect_info),
  57. .me = THIS_MODULE,
  58. };
  59. static int __init ebt_redirect_init(void)
  60. {
  61. return xt_register_target(&ebt_redirect_tg_reg);
  62. }
  63. static void __exit ebt_redirect_fini(void)
  64. {
  65. xt_unregister_target(&ebt_redirect_tg_reg);
  66. }
  67. module_init(ebt_redirect_init);
  68. module_exit(ebt_redirect_fini);
  69. MODULE_DESCRIPTION("Ebtables: Packet redirection to localhost");
  70. MODULE_LICENSE("GPL");