ebt_dnat.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ebt_dnat
  4. *
  5. * Authors:
  6. * Bart De Schuymer <bdschuym@pandora.be>
  7. *
  8. * June, 2002
  9. *
  10. */
  11. #include <linux/module.h>
  12. #include <net/sock.h>
  13. #include "../br_private.h"
  14. #include <linux/netfilter.h>
  15. #include <linux/netfilter/x_tables.h>
  16. #include <linux/netfilter_bridge/ebtables.h>
  17. #include <linux/netfilter_bridge/ebt_nat.h>
  18. static unsigned int
  19. ebt_dnat_tg(struct sk_buff *skb, const struct xt_action_param *par)
  20. {
  21. const struct ebt_nat_info *info = par->targinfo;
  22. if (skb_ensure_writable(skb, 0))
  23. return EBT_DROP;
  24. ether_addr_copy(eth_hdr(skb)->h_dest, info->mac);
  25. if (is_multicast_ether_addr(info->mac)) {
  26. if (is_broadcast_ether_addr(info->mac))
  27. skb->pkt_type = PACKET_BROADCAST;
  28. else
  29. skb->pkt_type = PACKET_MULTICAST;
  30. } else {
  31. const struct net_device *dev;
  32. switch (xt_hooknum(par)) {
  33. case NF_BR_BROUTING:
  34. dev = xt_in(par);
  35. break;
  36. case NF_BR_PRE_ROUTING:
  37. dev = br_port_get_rcu(xt_in(par))->br->dev;
  38. break;
  39. default:
  40. dev = NULL;
  41. break;
  42. }
  43. if (!dev) /* NF_BR_LOCAL_OUT */
  44. return info->target;
  45. if (ether_addr_equal(info->mac, dev->dev_addr))
  46. skb->pkt_type = PACKET_HOST;
  47. else
  48. skb->pkt_type = PACKET_OTHERHOST;
  49. }
  50. return info->target;
  51. }
  52. static int ebt_dnat_tg_check(const struct xt_tgchk_param *par)
  53. {
  54. const struct ebt_nat_info *info = par->targinfo;
  55. unsigned int hook_mask;
  56. if (BASE_CHAIN && info->target == EBT_RETURN)
  57. return -EINVAL;
  58. hook_mask = par->hook_mask & ~(1 << NF_BR_NUMHOOKS);
  59. if ((strcmp(par->table, "nat") != 0 ||
  60. (hook_mask & ~((1 << NF_BR_PRE_ROUTING) |
  61. (1 << NF_BR_LOCAL_OUT)))) &&
  62. (strcmp(par->table, "broute") != 0 ||
  63. hook_mask & ~(1 << NF_BR_BROUTING)))
  64. return -EINVAL;
  65. if (ebt_invalid_target(info->target))
  66. return -EINVAL;
  67. return 0;
  68. }
  69. static struct xt_target ebt_dnat_tg_reg __read_mostly = {
  70. .name = "dnat",
  71. .revision = 0,
  72. .family = NFPROTO_BRIDGE,
  73. .hooks = (1 << NF_BR_NUMHOOKS) | (1 << NF_BR_PRE_ROUTING) |
  74. (1 << NF_BR_LOCAL_OUT) | (1 << NF_BR_BROUTING),
  75. .target = ebt_dnat_tg,
  76. .checkentry = ebt_dnat_tg_check,
  77. .targetsize = sizeof(struct ebt_nat_info),
  78. .me = THIS_MODULE,
  79. };
  80. static int __init ebt_dnat_init(void)
  81. {
  82. return xt_register_target(&ebt_dnat_tg_reg);
  83. }
  84. static void __exit ebt_dnat_fini(void)
  85. {
  86. xt_unregister_target(&ebt_dnat_tg_reg);
  87. }
  88. module_init(ebt_dnat_init);
  89. module_exit(ebt_dnat_fini);
  90. MODULE_DESCRIPTION("Ebtables: Destination MAC address translation");
  91. MODULE_LICENSE("GPL");