xt_dscp.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. pr_info("dscp %x out of range\n", info->dscp);
  43. return -EDOM;
  44. }
  45. return 0;
  46. }
  47. static bool tos_mt(const struct sk_buff *skb, struct xt_action_param *par)
  48. {
  49. const struct xt_tos_match_info *info = par->matchinfo;
  50. if (par->family == NFPROTO_IPV4)
  51. return ((ip_hdr(skb)->tos & info->tos_mask) ==
  52. info->tos_value) ^ !!info->invert;
  53. else
  54. return ((ipv6_get_dsfield(ipv6_hdr(skb)) & info->tos_mask) ==
  55. info->tos_value) ^ !!info->invert;
  56. }
  57. static struct xt_match dscp_mt_reg[] __read_mostly = {
  58. {
  59. .name = "dscp",
  60. .family = NFPROTO_IPV4,
  61. .checkentry = dscp_mt_check,
  62. .match = dscp_mt,
  63. .matchsize = sizeof(struct xt_dscp_info),
  64. .me = THIS_MODULE,
  65. },
  66. {
  67. .name = "dscp",
  68. .family = NFPROTO_IPV6,
  69. .checkentry = dscp_mt_check,
  70. .match = dscp_mt6,
  71. .matchsize = sizeof(struct xt_dscp_info),
  72. .me = THIS_MODULE,
  73. },
  74. {
  75. .name = "tos",
  76. .revision = 1,
  77. .family = NFPROTO_IPV4,
  78. .match = tos_mt,
  79. .matchsize = sizeof(struct xt_tos_match_info),
  80. .me = THIS_MODULE,
  81. },
  82. {
  83. .name = "tos",
  84. .revision = 1,
  85. .family = NFPROTO_IPV6,
  86. .match = tos_mt,
  87. .matchsize = sizeof(struct xt_tos_match_info),
  88. .me = THIS_MODULE,
  89. },
  90. };
  91. static int __init dscp_mt_init(void)
  92. {
  93. return xt_register_matches(dscp_mt_reg, ARRAY_SIZE(dscp_mt_reg));
  94. }
  95. static void __exit dscp_mt_exit(void)
  96. {
  97. xt_unregister_matches(dscp_mt_reg, ARRAY_SIZE(dscp_mt_reg));
  98. }
  99. module_init(dscp_mt_init);
  100. module_exit(dscp_mt_exit);