xt_DSCP.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /* x_tables module for setting the IPv4/IPv6 DSCP field, Version 1.8
  2. *
  3. * (C) 2002 by Harald Welte <laforge@netfilter.org>
  4. * based on ipt_FTOS.c (C) 2000 by Matthew G. Marsh <mgm@paktronix.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * See RFC2474 for a description of the DSCP field within the IP Header.
  11. */
  12. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  13. #include <linux/module.h>
  14. #include <linux/skbuff.h>
  15. #include <linux/ip.h>
  16. #include <linux/ipv6.h>
  17. #include <net/dsfield.h>
  18. #include <linux/netfilter/x_tables.h>
  19. #include <linux/netfilter/xt_DSCP.h>
  20. MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
  21. MODULE_DESCRIPTION("Xtables: DSCP/TOS field modification");
  22. MODULE_LICENSE("GPL");
  23. MODULE_ALIAS("ipt_DSCP");
  24. MODULE_ALIAS("ip6t_DSCP");
  25. MODULE_ALIAS("ipt_TOS");
  26. MODULE_ALIAS("ip6t_TOS");
  27. static unsigned int
  28. dscp_tg(struct sk_buff *skb, const struct xt_action_param *par)
  29. {
  30. const struct xt_DSCP_info *dinfo = par->targinfo;
  31. u_int8_t dscp = ipv4_get_dsfield(ip_hdr(skb)) >> XT_DSCP_SHIFT;
  32. if (dscp != dinfo->dscp) {
  33. if (!skb_make_writable(skb, sizeof(struct iphdr)))
  34. return NF_DROP;
  35. ipv4_change_dsfield(ip_hdr(skb), (__u8)(~XT_DSCP_MASK),
  36. dinfo->dscp << XT_DSCP_SHIFT);
  37. }
  38. return XT_CONTINUE;
  39. }
  40. static unsigned int
  41. dscp_tg6(struct sk_buff *skb, const struct xt_action_param *par)
  42. {
  43. const struct xt_DSCP_info *dinfo = par->targinfo;
  44. u_int8_t dscp = ipv6_get_dsfield(ipv6_hdr(skb)) >> XT_DSCP_SHIFT;
  45. if (dscp != dinfo->dscp) {
  46. if (!skb_make_writable(skb, sizeof(struct ipv6hdr)))
  47. return NF_DROP;
  48. ipv6_change_dsfield(ipv6_hdr(skb), (__u8)(~XT_DSCP_MASK),
  49. dinfo->dscp << XT_DSCP_SHIFT);
  50. }
  51. return XT_CONTINUE;
  52. }
  53. static int dscp_tg_check(const struct xt_tgchk_param *par)
  54. {
  55. const struct xt_DSCP_info *info = par->targinfo;
  56. if (info->dscp > XT_DSCP_MAX) {
  57. pr_info("dscp %x out of range\n", info->dscp);
  58. return -EDOM;
  59. }
  60. return 0;
  61. }
  62. static unsigned int
  63. tos_tg(struct sk_buff *skb, const struct xt_action_param *par)
  64. {
  65. const struct xt_tos_target_info *info = par->targinfo;
  66. struct iphdr *iph = ip_hdr(skb);
  67. u_int8_t orig, nv;
  68. orig = ipv4_get_dsfield(iph);
  69. nv = (orig & ~info->tos_mask) ^ info->tos_value;
  70. if (orig != nv) {
  71. if (!skb_make_writable(skb, sizeof(struct iphdr)))
  72. return NF_DROP;
  73. iph = ip_hdr(skb);
  74. ipv4_change_dsfield(iph, 0, nv);
  75. }
  76. return XT_CONTINUE;
  77. }
  78. static unsigned int
  79. tos_tg6(struct sk_buff *skb, const struct xt_action_param *par)
  80. {
  81. const struct xt_tos_target_info *info = par->targinfo;
  82. struct ipv6hdr *iph = ipv6_hdr(skb);
  83. u_int8_t orig, nv;
  84. orig = ipv6_get_dsfield(iph);
  85. nv = (orig & ~info->tos_mask) ^ info->tos_value;
  86. if (orig != nv) {
  87. if (!skb_make_writable(skb, sizeof(struct iphdr)))
  88. return NF_DROP;
  89. iph = ipv6_hdr(skb);
  90. ipv6_change_dsfield(iph, 0, nv);
  91. }
  92. return XT_CONTINUE;
  93. }
  94. static struct xt_target dscp_tg_reg[] __read_mostly = {
  95. {
  96. .name = "DSCP",
  97. .family = NFPROTO_IPV4,
  98. .checkentry = dscp_tg_check,
  99. .target = dscp_tg,
  100. .targetsize = sizeof(struct xt_DSCP_info),
  101. .table = "mangle",
  102. .me = THIS_MODULE,
  103. },
  104. {
  105. .name = "DSCP",
  106. .family = NFPROTO_IPV6,
  107. .checkentry = dscp_tg_check,
  108. .target = dscp_tg6,
  109. .targetsize = sizeof(struct xt_DSCP_info),
  110. .table = "mangle",
  111. .me = THIS_MODULE,
  112. },
  113. {
  114. .name = "TOS",
  115. .revision = 1,
  116. .family = NFPROTO_IPV4,
  117. .table = "mangle",
  118. .target = tos_tg,
  119. .targetsize = sizeof(struct xt_tos_target_info),
  120. .me = THIS_MODULE,
  121. },
  122. {
  123. .name = "TOS",
  124. .revision = 1,
  125. .family = NFPROTO_IPV6,
  126. .table = "mangle",
  127. .target = tos_tg6,
  128. .targetsize = sizeof(struct xt_tos_target_info),
  129. .me = THIS_MODULE,
  130. },
  131. };
  132. static int __init dscp_tg_init(void)
  133. {
  134. return xt_register_targets(dscp_tg_reg, ARRAY_SIZE(dscp_tg_reg));
  135. }
  136. static void __exit dscp_tg_exit(void)
  137. {
  138. xt_unregister_targets(dscp_tg_reg, ARRAY_SIZE(dscp_tg_reg));
  139. }
  140. module_init(dscp_tg_init);
  141. module_exit(dscp_tg_exit);