ebt_arp.c 3.6 KB

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