ebt_arpreply.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ebt_arpreply
  4. *
  5. * Authors:
  6. * Grzegorz Borowiak <grzes@gnu.univ.gda.pl>
  7. * Bart De Schuymer <bdschuym@pandora.be>
  8. *
  9. * August, 2003
  10. *
  11. */
  12. #include <linux/if_arp.h>
  13. #include <net/arp.h>
  14. #include <linux/module.h>
  15. #include <linux/netfilter/x_tables.h>
  16. #include <linux/netfilter_bridge/ebtables.h>
  17. #include <linux/netfilter_bridge/ebt_arpreply.h>
  18. static unsigned int
  19. ebt_arpreply_tg(struct sk_buff *skb, const struct xt_action_param *par)
  20. {
  21. const struct ebt_arpreply_info *info = par->targinfo;
  22. const __be32 *siptr, *diptr;
  23. __be32 _sip, _dip;
  24. const struct arphdr *ap;
  25. struct arphdr _ah;
  26. const unsigned char *shp;
  27. unsigned char _sha[ETH_ALEN];
  28. ap = skb_header_pointer(skb, 0, sizeof(_ah), &_ah);
  29. if (ap == NULL)
  30. return EBT_DROP;
  31. if (ap->ar_op != htons(ARPOP_REQUEST) ||
  32. ap->ar_hln != ETH_ALEN ||
  33. ap->ar_pro != htons(ETH_P_IP) ||
  34. ap->ar_pln != 4)
  35. return EBT_CONTINUE;
  36. shp = skb_header_pointer(skb, sizeof(_ah), ETH_ALEN, &_sha);
  37. if (shp == NULL)
  38. return EBT_DROP;
  39. siptr = skb_header_pointer(skb, sizeof(_ah) + ETH_ALEN,
  40. sizeof(_sip), &_sip);
  41. if (siptr == NULL)
  42. return EBT_DROP;
  43. diptr = skb_header_pointer(skb,
  44. sizeof(_ah) + 2 * ETH_ALEN + sizeof(_sip),
  45. sizeof(_dip), &_dip);
  46. if (diptr == NULL)
  47. return EBT_DROP;
  48. arp_send(ARPOP_REPLY, ETH_P_ARP, *siptr,
  49. (struct net_device *)xt_in(par),
  50. *diptr, shp, info->mac, shp);
  51. return info->target;
  52. }
  53. static int ebt_arpreply_tg_check(const struct xt_tgchk_param *par)
  54. {
  55. const struct ebt_arpreply_info *info = par->targinfo;
  56. const struct ebt_entry *e = par->entryinfo;
  57. if (BASE_CHAIN && info->target == EBT_RETURN)
  58. return -EINVAL;
  59. if (e->ethproto != htons(ETH_P_ARP) ||
  60. e->invflags & EBT_IPROTO)
  61. return -EINVAL;
  62. if (ebt_invalid_target(info->target))
  63. return -EINVAL;
  64. return 0;
  65. }
  66. static struct xt_target ebt_arpreply_tg_reg __read_mostly = {
  67. .name = "arpreply",
  68. .revision = 0,
  69. .family = NFPROTO_BRIDGE,
  70. .table = "nat",
  71. .hooks = (1 << NF_BR_NUMHOOKS) | (1 << NF_BR_PRE_ROUTING),
  72. .target = ebt_arpreply_tg,
  73. .checkentry = ebt_arpreply_tg_check,
  74. .targetsize = sizeof(struct ebt_arpreply_info),
  75. .me = THIS_MODULE,
  76. };
  77. static int __init ebt_arpreply_init(void)
  78. {
  79. return xt_register_target(&ebt_arpreply_tg_reg);
  80. }
  81. static void __exit ebt_arpreply_fini(void)
  82. {
  83. xt_unregister_target(&ebt_arpreply_tg_reg);
  84. }
  85. module_init(ebt_arpreply_init);
  86. module_exit(ebt_arpreply_fini);
  87. MODULE_DESCRIPTION("Ebtables: ARP reply target");
  88. MODULE_LICENSE("GPL");