ebt_snat.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ebt_snat
  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 <linux/if_arp.h>
  14. #include <net/arp.h>
  15. #include <linux/netfilter.h>
  16. #include <linux/netfilter/x_tables.h>
  17. #include <linux/netfilter_bridge/ebtables.h>
  18. #include <linux/netfilter_bridge/ebt_nat.h>
  19. static unsigned int
  20. ebt_snat_tg(struct sk_buff *skb, const struct xt_action_param *par)
  21. {
  22. const struct ebt_nat_info *info = par->targinfo;
  23. if (skb_ensure_writable(skb, 0))
  24. return EBT_DROP;
  25. ether_addr_copy(eth_hdr(skb)->h_source, info->mac);
  26. if (!(info->target & NAT_ARP_BIT) &&
  27. eth_hdr(skb)->h_proto == htons(ETH_P_ARP)) {
  28. const struct arphdr *ap;
  29. struct arphdr _ah;
  30. ap = skb_header_pointer(skb, 0, sizeof(_ah), &_ah);
  31. if (ap == NULL)
  32. return EBT_DROP;
  33. if (ap->ar_hln != ETH_ALEN)
  34. goto out;
  35. if (skb_store_bits(skb, sizeof(_ah), info->mac, ETH_ALEN))
  36. return EBT_DROP;
  37. }
  38. out:
  39. return info->target | ~EBT_VERDICT_BITS;
  40. }
  41. static int ebt_snat_tg_check(const struct xt_tgchk_param *par)
  42. {
  43. const struct ebt_nat_info *info = par->targinfo;
  44. int tmp;
  45. tmp = info->target | ~EBT_VERDICT_BITS;
  46. if (BASE_CHAIN && tmp == EBT_RETURN)
  47. return -EINVAL;
  48. if (ebt_invalid_target(tmp))
  49. return -EINVAL;
  50. tmp = info->target | EBT_VERDICT_BITS;
  51. if ((tmp & ~NAT_ARP_BIT) != ~NAT_ARP_BIT)
  52. return -EINVAL;
  53. return 0;
  54. }
  55. static struct xt_target ebt_snat_tg_reg __read_mostly = {
  56. .name = "snat",
  57. .revision = 0,
  58. .family = NFPROTO_BRIDGE,
  59. .table = "nat",
  60. .hooks = (1 << NF_BR_NUMHOOKS) | (1 << NF_BR_POST_ROUTING),
  61. .target = ebt_snat_tg,
  62. .checkentry = ebt_snat_tg_check,
  63. .targetsize = sizeof(struct ebt_nat_info),
  64. .me = THIS_MODULE,
  65. };
  66. static int __init ebt_snat_init(void)
  67. {
  68. return xt_register_target(&ebt_snat_tg_reg);
  69. }
  70. static void __exit ebt_snat_fini(void)
  71. {
  72. xt_unregister_target(&ebt_snat_tg_reg);
  73. }
  74. module_init(ebt_snat_init);
  75. module_exit(ebt_snat_fini);
  76. MODULE_DESCRIPTION("Ebtables: Source MAC address translation");
  77. MODULE_LICENSE("GPL");