xt_DSCP.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. * xt_DSCP.c,v 1.8 2002/08/06 18:41:57 laforge Exp
  13. */
  14. #include <linux/module.h>
  15. #include <linux/skbuff.h>
  16. #include <linux/ip.h>
  17. #include <linux/ipv6.h>
  18. #include <net/dsfield.h>
  19. #include <linux/netfilter/x_tables.h>
  20. #include <linux/netfilter/xt_DSCP.h>
  21. MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
  22. MODULE_DESCRIPTION("x_tables DSCP modification module");
  23. MODULE_LICENSE("GPL");
  24. MODULE_ALIAS("ipt_DSCP");
  25. MODULE_ALIAS("ip6t_DSCP");
  26. static unsigned int target(struct sk_buff **pskb,
  27. const struct net_device *in,
  28. const struct net_device *out,
  29. unsigned int hooknum,
  30. const struct xt_target *target,
  31. const void *targinfo)
  32. {
  33. const struct xt_DSCP_info *dinfo = targinfo;
  34. u_int8_t dscp = ipv4_get_dsfield((*pskb)->nh.iph) >> XT_DSCP_SHIFT;
  35. if (dscp != dinfo->dscp) {
  36. if (!skb_make_writable(pskb, sizeof(struct iphdr)))
  37. return NF_DROP;
  38. ipv4_change_dsfield((*pskb)->nh.iph, (__u8)(~XT_DSCP_MASK),
  39. dinfo->dscp << XT_DSCP_SHIFT);
  40. }
  41. return XT_CONTINUE;
  42. }
  43. static unsigned int target6(struct sk_buff **pskb,
  44. const struct net_device *in,
  45. const struct net_device *out,
  46. unsigned int hooknum,
  47. const struct xt_target *target,
  48. const void *targinfo)
  49. {
  50. const struct xt_DSCP_info *dinfo = targinfo;
  51. u_int8_t dscp = ipv6_get_dsfield((*pskb)->nh.ipv6h) >> XT_DSCP_SHIFT;
  52. if (dscp != dinfo->dscp) {
  53. if (!skb_make_writable(pskb, sizeof(struct ipv6hdr)))
  54. return NF_DROP;
  55. ipv6_change_dsfield((*pskb)->nh.ipv6h, (__u8)(~XT_DSCP_MASK),
  56. dinfo->dscp << XT_DSCP_SHIFT);
  57. }
  58. return XT_CONTINUE;
  59. }
  60. static int checkentry(const char *tablename,
  61. const void *e_void,
  62. const struct xt_target *target,
  63. void *targinfo,
  64. unsigned int hook_mask)
  65. {
  66. const u_int8_t dscp = ((struct xt_DSCP_info *)targinfo)->dscp;
  67. if ((dscp > XT_DSCP_MAX)) {
  68. printk(KERN_WARNING "DSCP: dscp %x out of range\n", dscp);
  69. return 0;
  70. }
  71. return 1;
  72. }
  73. static struct xt_target xt_dscp_target[] = {
  74. {
  75. .name = "DSCP",
  76. .family = AF_INET,
  77. .checkentry = checkentry,
  78. .target = target,
  79. .targetsize = sizeof(struct xt_DSCP_info),
  80. .table = "mangle",
  81. .me = THIS_MODULE,
  82. },
  83. {
  84. .name = "DSCP",
  85. .family = AF_INET6,
  86. .checkentry = checkentry,
  87. .target = target6,
  88. .targetsize = sizeof(struct xt_DSCP_info),
  89. .table = "mangle",
  90. .me = THIS_MODULE,
  91. },
  92. };
  93. static int __init xt_dscp_target_init(void)
  94. {
  95. return xt_register_targets(xt_dscp_target, ARRAY_SIZE(xt_dscp_target));
  96. }
  97. static void __exit xt_dscp_target_fini(void)
  98. {
  99. xt_unregister_targets(xt_dscp_target, ARRAY_SIZE(xt_dscp_target));
  100. }
  101. module_init(xt_dscp_target_init);
  102. module_exit(xt_dscp_target_fini);