ebt_mark_m.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ebt_mark_m
  4. *
  5. * Authors:
  6. * Bart De Schuymer <bdschuym@pandora.be>
  7. *
  8. * July, 2002
  9. *
  10. */
  11. #include <linux/module.h>
  12. #include <linux/netfilter/x_tables.h>
  13. #include <linux/netfilter_bridge/ebtables.h>
  14. #include <linux/netfilter_bridge/ebt_mark_m.h>
  15. static bool
  16. ebt_mark_mt(const struct sk_buff *skb, struct xt_action_param *par)
  17. {
  18. const struct ebt_mark_m_info *info = par->matchinfo;
  19. if (info->bitmask & EBT_MARK_OR)
  20. return !!(skb->mark & info->mask) ^ info->invert;
  21. return ((skb->mark & info->mask) == info->mark) ^ info->invert;
  22. }
  23. static int ebt_mark_mt_check(const struct xt_mtchk_param *par)
  24. {
  25. const struct ebt_mark_m_info *info = par->matchinfo;
  26. if (info->bitmask & ~EBT_MARK_MASK)
  27. return -EINVAL;
  28. if ((info->bitmask & EBT_MARK_OR) && (info->bitmask & EBT_MARK_AND))
  29. return -EINVAL;
  30. if (!info->bitmask)
  31. return -EINVAL;
  32. return 0;
  33. }
  34. #ifdef CONFIG_COMPAT
  35. struct compat_ebt_mark_m_info {
  36. compat_ulong_t mark, mask;
  37. uint8_t invert, bitmask;
  38. };
  39. static void mark_mt_compat_from_user(void *dst, const void *src)
  40. {
  41. const struct compat_ebt_mark_m_info *user = src;
  42. struct ebt_mark_m_info *kern = dst;
  43. kern->mark = user->mark;
  44. kern->mask = user->mask;
  45. kern->invert = user->invert;
  46. kern->bitmask = user->bitmask;
  47. }
  48. static int mark_mt_compat_to_user(void __user *dst, const void *src)
  49. {
  50. struct compat_ebt_mark_m_info __user *user = dst;
  51. const struct ebt_mark_m_info *kern = src;
  52. if (put_user(kern->mark, &user->mark) ||
  53. put_user(kern->mask, &user->mask) ||
  54. put_user(kern->invert, &user->invert) ||
  55. put_user(kern->bitmask, &user->bitmask))
  56. return -EFAULT;
  57. return 0;
  58. }
  59. #endif
  60. static struct xt_match ebt_mark_mt_reg __read_mostly = {
  61. .name = "mark_m",
  62. .revision = 0,
  63. .family = NFPROTO_BRIDGE,
  64. .match = ebt_mark_mt,
  65. .checkentry = ebt_mark_mt_check,
  66. .matchsize = sizeof(struct ebt_mark_m_info),
  67. #ifdef CONFIG_COMPAT
  68. .compatsize = sizeof(struct compat_ebt_mark_m_info),
  69. .compat_from_user = mark_mt_compat_from_user,
  70. .compat_to_user = mark_mt_compat_to_user,
  71. #endif
  72. .me = THIS_MODULE,
  73. };
  74. static int __init ebt_mark_m_init(void)
  75. {
  76. return xt_register_match(&ebt_mark_mt_reg);
  77. }
  78. static void __exit ebt_mark_m_fini(void)
  79. {
  80. xt_unregister_match(&ebt_mark_mt_reg);
  81. }
  82. module_init(ebt_mark_m_init);
  83. module_exit(ebt_mark_m_fini);
  84. MODULE_DESCRIPTION("Ebtables: Packet mark match");
  85. MODULE_LICENSE("GPL");