libxt_tos.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Shared library add-on to iptables to add tos match support
  3. *
  4. * Copyright © CC Computer Consultants GmbH, 2007
  5. * Contact: Jan Engelhardt <jengelh@computergmbh.de>
  6. */
  7. #include <getopt.h>
  8. #include <netdb.h>
  9. #include <stdbool.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <xtables.h>
  14. #include <linux/netfilter/xt_dscp.h>
  15. #include "tos_values.c"
  16. struct ipt_tos_info {
  17. uint8_t tos;
  18. uint8_t invert;
  19. };
  20. enum {
  21. O_TOS = 1 << 0,
  22. };
  23. static const struct xt_option_entry tos_mt_opts_v0[] = {
  24. {.name = "tos", .id = O_TOS, .type = XTTYPE_TOSMASK,
  25. .flags = XTOPT_INVERT | XTOPT_MAND, .max = 0xFF},
  26. XTOPT_TABLEEND,
  27. };
  28. static const struct xt_option_entry tos_mt_opts[] = {
  29. {.name = "tos", .id = O_TOS, .type = XTTYPE_TOSMASK,
  30. .flags = XTOPT_INVERT | XTOPT_MAND, .max = 0x3F},
  31. XTOPT_TABLEEND,
  32. };
  33. static void tos_mt_help(void)
  34. {
  35. const struct tos_symbol_info *symbol;
  36. printf(
  37. "tos match options:\n"
  38. "[!] --tos value[/mask] Match Type of Service/Priority field value\n"
  39. "[!] --tos symbol Match TOS field (IPv4 only) by symbol\n"
  40. " Accepted symbolic names for value are:\n");
  41. for (symbol = tos_symbol_names; symbol->name != NULL; ++symbol)
  42. printf(" (0x%02x) %2u %s\n",
  43. symbol->value, symbol->value, symbol->name);
  44. printf("\n");
  45. }
  46. static void tos_mt_parse_v0(struct xt_option_call *cb)
  47. {
  48. struct ipt_tos_info *info = cb->data;
  49. xtables_option_parse(cb);
  50. if (cb->val.tos_mask != 0xFF)
  51. xtables_error(PARAMETER_PROBLEM, "tos: Your kernel is "
  52. "too old to support anything besides /0xFF "
  53. "as a mask.");
  54. info->tos = cb->val.tos_value;
  55. if (cb->invert)
  56. info->invert = true;
  57. }
  58. static void tos_mt_parse(struct xt_option_call *cb)
  59. {
  60. struct xt_tos_match_info *info = cb->data;
  61. xtables_option_parse(cb);
  62. info->tos_value = cb->val.tos_value;
  63. info->tos_mask = cb->val.tos_mask;
  64. if (cb->invert)
  65. info->invert = true;
  66. }
  67. static void tos_mt_print_v0(const void *ip, const struct xt_entry_match *match,
  68. int numeric)
  69. {
  70. const struct ipt_tos_info *info = (const void *)match->data;
  71. printf(" tos match ");
  72. if (info->invert)
  73. printf("!");
  74. if (numeric || !tos_try_print_symbolic("", info->tos, 0x3F))
  75. printf("0x%02x", info->tos);
  76. }
  77. static void tos_mt_print(const void *ip, const struct xt_entry_match *match,
  78. int numeric)
  79. {
  80. const struct xt_tos_match_info *info = (const void *)match->data;
  81. printf(" tos match");
  82. if (info->invert)
  83. printf("!");
  84. if (numeric ||
  85. !tos_try_print_symbolic("", info->tos_value, info->tos_mask))
  86. printf("0x%02x/0x%02x", info->tos_value, info->tos_mask);
  87. }
  88. static void tos_mt_save_v0(const void *ip, const struct xt_entry_match *match)
  89. {
  90. const struct ipt_tos_info *info = (const void *)match->data;
  91. if (info->invert)
  92. printf(" !");
  93. printf(" --tos 0x%02x", info->tos);
  94. }
  95. static void tos_mt_save(const void *ip, const struct xt_entry_match *match)
  96. {
  97. const struct xt_tos_match_info *info = (const void *)match->data;
  98. if (info->invert)
  99. printf(" !");
  100. printf(" --tos 0x%02x/0x%02x", info->tos_value, info->tos_mask);
  101. }
  102. static struct xtables_match tos_mt_reg[] = {
  103. {
  104. .version = XTABLES_VERSION,
  105. .name = "tos",
  106. .family = NFPROTO_IPV4,
  107. .revision = 0,
  108. .size = XT_ALIGN(sizeof(struct ipt_tos_info)),
  109. .userspacesize = XT_ALIGN(sizeof(struct ipt_tos_info)),
  110. .help = tos_mt_help,
  111. .print = tos_mt_print_v0,
  112. .save = tos_mt_save_v0,
  113. .x6_parse = tos_mt_parse_v0,
  114. .x6_options = tos_mt_opts_v0,
  115. },
  116. {
  117. .version = XTABLES_VERSION,
  118. .name = "tos",
  119. .family = NFPROTO_UNSPEC,
  120. .revision = 1,
  121. .size = XT_ALIGN(sizeof(struct xt_tos_match_info)),
  122. .userspacesize = XT_ALIGN(sizeof(struct xt_tos_match_info)),
  123. .help = tos_mt_help,
  124. .print = tos_mt_print,
  125. .save = tos_mt_save,
  126. .x6_parse = tos_mt_parse,
  127. .x6_options = tos_mt_opts,
  128. },
  129. };
  130. void _init(void)
  131. {
  132. xtables_register_matches(tos_mt_reg, ARRAY_SIZE(tos_mt_reg));
  133. }