libxt_dscp.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* Shared library add-on to iptables for DSCP
  2. *
  3. * (C) 2002 by Harald Welte <laforge@gnumonks.org>
  4. *
  5. * This program is distributed under the terms of GNU GPL v2, 1991
  6. *
  7. * libipt_dscp.c borrowed heavily from libipt_tos.c
  8. *
  9. * --class support added by Iain Barnes
  10. *
  11. * For a list of DSCP codepoints see
  12. * http://www.iana.org/assignments/dscp-registry
  13. *
  14. */
  15. #include <stdio.h>
  16. #include <string.h>
  17. #include <xtables.h>
  18. #include <linux/netfilter/xt_dscp.h>
  19. /* This is evil, but it's my code - HW*/
  20. #include "dscp_helper.c"
  21. enum {
  22. O_DSCP = 0,
  23. O_DSCP_CLASS,
  24. F_DSCP = 1 << O_DSCP,
  25. F_DSCP_CLASS = 1 << O_DSCP_CLASS,
  26. };
  27. static void dscp_help(void)
  28. {
  29. printf(
  30. "dscp match options\n"
  31. "[!] --dscp value Match DSCP codepoint with numerical value\n"
  32. " This value can be in decimal (ex: 32)\n"
  33. " or in hex (ex: 0x20)\n"
  34. "[!] --dscp-class name Match the DiffServ class. This value may\n"
  35. " be any of the BE,EF, AFxx or CSx classes\n"
  36. "\n"
  37. " These two options are mutually exclusive !\n");
  38. }
  39. static const struct xt_option_entry dscp_opts[] = {
  40. {.name = "dscp", .id = O_DSCP, .excl = F_DSCP_CLASS,
  41. .type = XTTYPE_UINT8, .min = 0, .max = XT_DSCP_MAX,
  42. .flags = XTOPT_INVERT | XTOPT_PUT,
  43. XTOPT_POINTER(struct xt_dscp_info, dscp)},
  44. {.name = "dscp-class", .id = O_DSCP_CLASS, .excl = F_DSCP,
  45. .type = XTTYPE_STRING, .flags = XTOPT_INVERT},
  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_DSCP:
  54. if (cb->invert)
  55. dinfo->invert = 1;
  56. break;
  57. case O_DSCP_CLASS:
  58. dinfo->dscp = class_to_dscp(cb->arg);
  59. if (cb->invert)
  60. dinfo->invert = 1;
  61. break;
  62. }
  63. }
  64. static void dscp_check(struct xt_fcheck_call *cb)
  65. {
  66. if (cb->xflags == 0)
  67. xtables_error(PARAMETER_PROBLEM,
  68. "DSCP match: Parameter --dscp is required");
  69. }
  70. static void
  71. dscp_print(const void *ip, const struct xt_entry_match *match, int numeric)
  72. {
  73. const struct xt_dscp_info *dinfo =
  74. (const struct xt_dscp_info *)match->data;
  75. printf(" DSCP match %s0x%02x", dinfo->invert ? "!" : "", dinfo->dscp);
  76. }
  77. static void dscp_save(const void *ip, const struct xt_entry_match *match)
  78. {
  79. const struct xt_dscp_info *dinfo =
  80. (const struct xt_dscp_info *)match->data;
  81. printf("%s --dscp 0x%02x", dinfo->invert ? " !" : "", dinfo->dscp);
  82. }
  83. static struct xtables_match dscp_match = {
  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_match(&dscp_match);
  99. }