ebt_arp.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ebt_arp
  4. *
  5. * Authors:
  6. * Bart De Schuymer <bdschuym@pandora.be>
  7. * Tim Gardner <timg@tpi.com>
  8. *
  9. * April, 2002
  10. *
  11. */
  12. #include <linux/if_arp.h>
  13. #include <linux/if_ether.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_arp.h>
  18. static bool
  19. ebt_arp_mt(const struct sk_buff *skb, struct xt_action_param *par)
  20. {
  21. const struct ebt_arp_info *info = par->matchinfo;
  22. const struct arphdr *ah;
  23. struct arphdr _arph;
  24. ah = skb_header_pointer(skb, 0, sizeof(_arph), &_arph);
  25. if (ah == NULL)
  26. return false;
  27. if ((info->bitmask & EBT_ARP_OPCODE) &&
  28. NF_INVF(info, EBT_ARP_OPCODE, info->opcode != ah->ar_op))
  29. return false;
  30. if ((info->bitmask & EBT_ARP_HTYPE) &&
  31. NF_INVF(info, EBT_ARP_HTYPE, info->htype != ah->ar_hrd))
  32. return false;
  33. if ((info->bitmask & EBT_ARP_PTYPE) &&
  34. NF_INVF(info, EBT_ARP_PTYPE, info->ptype != ah->ar_pro))
  35. return false;
  36. if (info->bitmask & (EBT_ARP_SRC_IP | EBT_ARP_DST_IP | EBT_ARP_GRAT)) {
  37. const __be32 *sap, *dap;
  38. __be32 saddr, daddr;
  39. if (ah->ar_pln != sizeof(__be32) || ah->ar_pro != htons(ETH_P_IP))
  40. return false;
  41. sap = skb_header_pointer(skb, sizeof(struct arphdr) +
  42. ah->ar_hln, sizeof(saddr),
  43. &saddr);
  44. if (sap == NULL)
  45. return false;
  46. dap = skb_header_pointer(skb, sizeof(struct arphdr) +
  47. 2*ah->ar_hln+sizeof(saddr),
  48. sizeof(daddr), &daddr);
  49. if (dap == NULL)
  50. return false;
  51. if ((info->bitmask & EBT_ARP_SRC_IP) &&
  52. NF_INVF(info, EBT_ARP_SRC_IP,
  53. info->saddr != (*sap & info->smsk)))
  54. return false;
  55. if ((info->bitmask & EBT_ARP_DST_IP) &&
  56. NF_INVF(info, EBT_ARP_DST_IP,
  57. info->daddr != (*dap & info->dmsk)))
  58. return false;
  59. if ((info->bitmask & EBT_ARP_GRAT) &&
  60. NF_INVF(info, EBT_ARP_GRAT, *dap != *sap))
  61. return false;
  62. }
  63. if (info->bitmask & (EBT_ARP_SRC_MAC | EBT_ARP_DST_MAC)) {
  64. const unsigned char *mp;
  65. unsigned char _mac[ETH_ALEN];
  66. if (ah->ar_hln != ETH_ALEN || ah->ar_hrd != htons(ARPHRD_ETHER))
  67. return false;
  68. if (info->bitmask & EBT_ARP_SRC_MAC) {
  69. mp = skb_header_pointer(skb, sizeof(struct arphdr),
  70. sizeof(_mac), &_mac);
  71. if (mp == NULL)
  72. return false;
  73. if (NF_INVF(info, EBT_ARP_SRC_MAC,
  74. !ether_addr_equal_masked(mp, info->smaddr,
  75. info->smmsk)))
  76. return false;
  77. }
  78. if (info->bitmask & EBT_ARP_DST_MAC) {
  79. mp = skb_header_pointer(skb, sizeof(struct arphdr) +
  80. ah->ar_hln + ah->ar_pln,
  81. sizeof(_mac), &_mac);
  82. if (mp == NULL)
  83. return false;
  84. if (NF_INVF(info, EBT_ARP_DST_MAC,
  85. !ether_addr_equal_masked(mp, info->dmaddr,
  86. info->dmmsk)))
  87. return false;
  88. }
  89. }
  90. return true;
  91. }
  92. static int ebt_arp_mt_check(const struct xt_mtchk_param *par)
  93. {
  94. const struct ebt_arp_info *info = par->matchinfo;
  95. const struct ebt_entry *e = par->entryinfo;
  96. if ((e->ethproto != htons(ETH_P_ARP) &&
  97. e->ethproto != htons(ETH_P_RARP)) ||
  98. e->invflags & EBT_IPROTO)
  99. return -EINVAL;
  100. if (info->bitmask & ~EBT_ARP_MASK || info->invflags & ~EBT_ARP_MASK)
  101. return -EINVAL;
  102. return 0;
  103. }
  104. static struct xt_match ebt_arp_mt_reg __read_mostly = {
  105. .name = "arp",
  106. .revision = 0,
  107. .family = NFPROTO_BRIDGE,
  108. .match = ebt_arp_mt,
  109. .checkentry = ebt_arp_mt_check,
  110. .matchsize = sizeof(struct ebt_arp_info),
  111. .me = THIS_MODULE,
  112. };
  113. static int __init ebt_arp_init(void)
  114. {
  115. return xt_register_match(&ebt_arp_mt_reg);
  116. }
  117. static void __exit ebt_arp_fini(void)
  118. {
  119. xt_unregister_match(&ebt_arp_mt_reg);
  120. }
  121. module_init(ebt_arp_init);
  122. module_exit(ebt_arp_fini);
  123. MODULE_DESCRIPTION("Ebtables: ARP protocol packet match");
  124. MODULE_LICENSE("GPL");