xt_DSCP.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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),
  36. (__force __u8)(~XT_DSCP_MASK),
  37. dinfo->dscp << XT_DSCP_SHIFT);
  38. }
  39. return XT_CONTINUE;
  40. }
  41. static unsigned int
  42. dscp_tg6(struct sk_buff *skb, const struct xt_action_param *par)
  43. {
  44. const struct xt_DSCP_info *dinfo = par->targinfo;
  45. u_int8_t dscp = ipv6_get_dsfield(ipv6_hdr(skb)) >> XT_DSCP_SHIFT;
  46. if (dscp != dinfo->dscp) {
  47. if (!skb_make_writable(skb, sizeof(struct ipv6hdr)))
  48. return NF_DROP;
  49. ipv6_change_dsfield(ipv6_hdr(skb),
  50. (__force __u8)(~XT_DSCP_MASK),
  51. dinfo->dscp << XT_DSCP_SHIFT);
  52. }
  53. return XT_CONTINUE;
  54. }
  55. static int dscp_tg_check(const struct xt_tgchk_param *par)
  56. {
  57. const struct xt_DSCP_info *info = par->targinfo;
  58. if (info->dscp > XT_DSCP_MAX) {
  59. pr_info("dscp %x out of range\n", info->dscp);
  60. return -EDOM;
  61. }
  62. return 0;
  63. }
  64. static unsigned int
  65. tos_tg(struct sk_buff *skb, const struct xt_action_param *par)
  66. {
  67. const struct xt_tos_target_info *info = par->targinfo;
  68. struct iphdr *iph = ip_hdr(skb);
  69. u_int8_t orig, nv;
  70. orig = ipv4_get_dsfield(iph);
  71. nv = (orig & ~info->tos_mask) ^ info->tos_value;
  72. if (orig != nv) {
  73. if (!skb_make_writable(skb, sizeof(struct iphdr)))
  74. return NF_DROP;
  75. iph = ip_hdr(skb);
  76. ipv4_change_dsfield(iph, 0, nv);
  77. }
  78. return XT_CONTINUE;
  79. }
  80. static unsigned int
  81. tos_tg6(struct sk_buff *skb, const struct xt_action_param *par)
  82. {
  83. const struct xt_tos_target_info *info = par->targinfo;
  84. struct ipv6hdr *iph = ipv6_hdr(skb);
  85. u_int8_t orig, nv;
  86. orig = ipv6_get_dsfield(iph);
  87. nv = (orig & ~info->tos_mask) ^ info->tos_value;
  88. if (orig != nv) {
  89. if (!skb_make_writable(skb, sizeof(struct iphdr)))
  90. return NF_DROP;
  91. iph = ipv6_hdr(skb);
  92. ipv6_change_dsfield(iph, 0, nv);
  93. }
  94. return XT_CONTINUE;
  95. }
  96. static struct xt_target dscp_tg_reg[] __read_mostly = {
  97. {
  98. .name = "DSCP",
  99. .family = NFPROTO_IPV4,
  100. .checkentry = dscp_tg_check,
  101. .target = dscp_tg,
  102. .targetsize = sizeof(struct xt_DSCP_info),
  103. .table = "mangle",
  104. .me = THIS_MODULE,
  105. },
  106. {
  107. .name = "DSCP",
  108. .family = NFPROTO_IPV6,
  109. .checkentry = dscp_tg_check,
  110. .target = dscp_tg6,
  111. .targetsize = sizeof(struct xt_DSCP_info),
  112. .table = "mangle",
  113. .me = THIS_MODULE,
  114. },
  115. {
  116. .name = "TOS",
  117. .revision = 1,
  118. .family = NFPROTO_IPV4,
  119. .table = "mangle",
  120. .target = tos_tg,
  121. .targetsize = sizeof(struct xt_tos_target_info),
  122. .me = THIS_MODULE,
  123. },
  124. {
  125. .name = "TOS",
  126. .revision = 1,
  127. .family = NFPROTO_IPV6,
  128. .table = "mangle",
  129. .target = tos_tg6,
  130. .targetsize = sizeof(struct xt_tos_target_info),
  131. .me = THIS_MODULE,
  132. },
  133. };
  134. static int __init dscp_tg_init(void)
  135. {
  136. return xt_register_targets(dscp_tg_reg, ARRAY_SIZE(dscp_tg_reg));
  137. }
  138. static void __exit dscp_tg_exit(void)
  139. {
  140. xt_unregister_targets(dscp_tg_reg, ARRAY_SIZE(dscp_tg_reg));
  141. }
  142. module_init(dscp_tg_init);
  143. module_exit(dscp_tg_exit);