libxt_DSCP.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* Shared library add-on to iptables for DSCP
  2. *
  3. * (C) 2000- 2002 by Matthew G. Marsh <mgm@paktronix.com>,
  4. * Harald Welte <laforge@gnumonks.org>
  5. *
  6. * This program is distributed under the terms of GNU GPL v2, 1991
  7. *
  8. * libipt_DSCP.c borrowed heavily from libipt_TOS.c
  9. *
  10. * --set-class added by Iain Barnes
  11. */
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <xtables.h>
  15. #include <linux/netfilter/xt_DSCP.h>
  16. /* This is evil, but it's my code - HW*/
  17. #include "dscp_helper.c"
  18. enum {
  19. O_SET_DSCP = 0,
  20. O_SET_DSCP_CLASS,
  21. F_SET_DSCP = 1 << O_SET_DSCP,
  22. F_SET_DSCP_CLASS = 1 << O_SET_DSCP_CLASS,
  23. };
  24. static void DSCP_help(void)
  25. {
  26. printf(
  27. "DSCP target options\n"
  28. " --set-dscp value Set DSCP field in packet header to value\n"
  29. " This value can be in decimal (ex: 32)\n"
  30. " or in hex (ex: 0x20)\n"
  31. " --set-dscp-class class Set the DSCP field in packet header to the\n"
  32. " value represented by the DiffServ class value.\n"
  33. " This class may be EF,BE or any of the CSxx\n"
  34. " or AFxx classes.\n"
  35. "\n"
  36. " These two options are mutually exclusive !\n"
  37. );
  38. }
  39. static const struct xt_option_entry DSCP_opts[] = {
  40. {.name = "set-dscp", .id = O_SET_DSCP, .excl = F_SET_DSCP_CLASS,
  41. .type = XTTYPE_UINT8, .min = 0, .max = XT_DSCP_MAX,
  42. .flags = XTOPT_PUT,
  43. XTOPT_POINTER(struct xt_DSCP_info, dscp)},
  44. {.name = "set-dscp-class", .id = O_SET_DSCP_CLASS, .excl = F_SET_DSCP,
  45. .type = XTTYPE_STRING},
  46. XTOPT_TABLEEND,
  47. };
  48. static void DSCP_parse(struct xt_option_call *cb)
  49. {
  50. struct xt_DSCP_info *dinfo = cb->data;
  51. xtables_option_parse(cb);
  52. switch (cb->entry->id) {
  53. case O_SET_DSCP_CLASS:
  54. dinfo->dscp = class_to_dscp(cb->arg);
  55. break;
  56. }
  57. }
  58. static void DSCP_check(struct xt_fcheck_call *cb)
  59. {
  60. if (cb->xflags == 0)
  61. xtables_error(PARAMETER_PROBLEM,
  62. "DSCP target: Parameter --set-dscp is required");
  63. }
  64. static void
  65. print_dscp(uint8_t dscp, int numeric)
  66. {
  67. printf(" 0x%02x", dscp);
  68. }
  69. static void DSCP_print(const void *ip, const struct xt_entry_target *target,
  70. int numeric)
  71. {
  72. const struct xt_DSCP_info *dinfo =
  73. (const struct xt_DSCP_info *)target->data;
  74. printf(" DSCP set");
  75. print_dscp(dinfo->dscp, numeric);
  76. }
  77. static void DSCP_save(const void *ip, const struct xt_entry_target *target)
  78. {
  79. const struct xt_DSCP_info *dinfo =
  80. (const struct xt_DSCP_info *)target->data;
  81. printf(" --set-dscp 0x%02x", dinfo->dscp);
  82. }
  83. static struct xtables_target dscp_target = {
  84. .family = NFPROTO_UNSPEC,
  85. .name = "DSCP",
  86. .version = XTABLES_VERSION,
  87. .size = XT_ALIGN(sizeof(struct xt_DSCP_info)),
  88. .userspacesize = XT_ALIGN(sizeof(struct xt_DSCP_info)),
  89. .help = DSCP_help,
  90. .print = DSCP_print,
  91. .save = DSCP_save,
  92. .x6_parse = DSCP_parse,
  93. .x6_fcheck = DSCP_check,
  94. .x6_options = DSCP_opts,
  95. };
  96. void _init(void)
  97. {
  98. xtables_register_target(&dscp_target);
  99. }