xt_dscp.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* IP tables module for matching the value of the IPv4/IPv6 DSCP field
  2. *
  3. * (C) 2002 by Harald Welte <laforge@netfilter.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  10. #include <linux/module.h>
  11. #include <linux/skbuff.h>
  12. #include <linux/ip.h>
  13. #include <linux/ipv6.h>
  14. #include <net/dsfield.h>
  15. #include <linux/netfilter/x_tables.h>
  16. #include <linux/netfilter/xt_dscp.h>
  17. MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
  18. MODULE_DESCRIPTION("Xtables: DSCP/TOS field match");
  19. MODULE_LICENSE("GPL");
  20. MODULE_ALIAS("ipt_dscp");
  21. MODULE_ALIAS("ip6t_dscp");
  22. MODULE_ALIAS("ipt_tos");
  23. MODULE_ALIAS("ip6t_tos");
  24. static bool
  25. dscp_mt(const struct sk_buff *skb, struct xt_action_param *par)
  26. {
  27. const struct xt_dscp_info *info = par->matchinfo;
  28. u_int8_t dscp = ipv4_get_dsfield(ip_hdr(skb)) >> XT_DSCP_SHIFT;
  29. return (dscp == info->dscp) ^ !!info->invert;
  30. }
  31. static bool
  32. dscp_mt6(const struct sk_buff *skb, struct xt_action_param *par)
  33. {
  34. const struct xt_dscp_info *info = par->matchinfo;
  35. u_int8_t dscp = ipv6_get_dsfield(ipv6_hdr(skb)) >> XT_DSCP_SHIFT;
  36. return (dscp == info->dscp) ^ !!info->invert;
  37. }
  38. static int dscp_mt_check(const struct xt_mtchk_param *par)
  39. {
  40. const struct xt_dscp_info *info = par->matchinfo;
  41. if (info->dscp > XT_DSCP_MAX)
  42. return -EDOM;
  43. return 0;
  44. }
  45. static bool tos_mt(const struct sk_buff *skb, struct xt_action_param *par)
  46. {
  47. const struct xt_tos_match_info *info = par->matchinfo;
  48. if (xt_family(par) == NFPROTO_IPV4)
  49. return ((ip_hdr(skb)->tos & info->tos_mask) ==
  50. info->tos_value) ^ !!info->invert;
  51. else
  52. return ((ipv6_get_dsfield(ipv6_hdr(skb)) & info->tos_mask) ==
  53. info->tos_value) ^ !!info->invert;
  54. }
  55. static struct xt_match dscp_mt_reg[] __read_mostly = {
  56. {
  57. .name = "dscp",
  58. .family = NFPROTO_IPV4,
  59. .checkentry = dscp_mt_check,
  60. .match = dscp_mt,
  61. .matchsize = sizeof(struct xt_dscp_info),
  62. .me = THIS_MODULE,
  63. },
  64. {
  65. .name = "dscp",
  66. .family = NFPROTO_IPV6,
  67. .checkentry = dscp_mt_check,
  68. .match = dscp_mt6,
  69. .matchsize = sizeof(struct xt_dscp_info),
  70. .me = THIS_MODULE,
  71. },
  72. {
  73. .name = "tos",
  74. .revision = 1,
  75. .family = NFPROTO_IPV4,
  76. .match = tos_mt,
  77. .matchsize = sizeof(struct xt_tos_match_info),
  78. .me = THIS_MODULE,
  79. },
  80. {
  81. .name = "tos",
  82. .revision = 1,
  83. .family = NFPROTO_IPV6,
  84. .match = tos_mt,
  85. .matchsize = sizeof(struct xt_tos_match_info),
  86. .me = THIS_MODULE,
  87. },
  88. };
  89. static int __init dscp_mt_init(void)
  90. {
  91. return xt_register_matches(dscp_mt_reg, ARRAY_SIZE(dscp_mt_reg));
  92. }
  93. static void __exit dscp_mt_exit(void)
  94. {
  95. xt_unregister_matches(dscp_mt_reg, ARRAY_SIZE(dscp_mt_reg));
  96. }
  97. module_init(dscp_mt_init);
  98. module_exit(dscp_mt_exit);