libxt_CLASSIFY.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #include <stdio.h>
  2. #include <xtables.h>
  3. #include <linux/netfilter/xt_CLASSIFY.h>
  4. #include <linux/pkt_sched.h>
  5. enum {
  6. O_SET_CLASS = 0,
  7. };
  8. static void
  9. CLASSIFY_help(void)
  10. {
  11. printf(
  12. "CLASSIFY target options:\n"
  13. "--set-class MAJOR:MINOR Set skb->priority value (always hexadecimal!)\n");
  14. }
  15. static const struct xt_option_entry CLASSIFY_opts[] = {
  16. {.name = "set-class", .id = O_SET_CLASS, .type = XTTYPE_STRING,
  17. .flags = XTOPT_MAND},
  18. XTOPT_TABLEEND,
  19. };
  20. static int CLASSIFY_string_to_priority(const char *s, unsigned int *p)
  21. {
  22. unsigned int i, j;
  23. if (sscanf(s, "%x:%x", &i, &j) != 2)
  24. return 1;
  25. *p = TC_H_MAKE(i<<16, j);
  26. return 0;
  27. }
  28. static void CLASSIFY_parse(struct xt_option_call *cb)
  29. {
  30. struct xt_classify_target_info *clinfo = cb->data;
  31. xtables_option_parse(cb);
  32. if (CLASSIFY_string_to_priority(cb->arg, &clinfo->priority))
  33. xtables_error(PARAMETER_PROBLEM,
  34. "Bad class value \"%s\"", cb->arg);
  35. }
  36. static void
  37. CLASSIFY_print_class(unsigned int priority, int numeric)
  38. {
  39. printf(" %x:%x", TC_H_MAJ(priority)>>16, TC_H_MIN(priority));
  40. }
  41. static void
  42. CLASSIFY_print(const void *ip,
  43. const struct xt_entry_target *target,
  44. int numeric)
  45. {
  46. const struct xt_classify_target_info *clinfo =
  47. (const struct xt_classify_target_info *)target->data;
  48. printf(" CLASSIFY set");
  49. CLASSIFY_print_class(clinfo->priority, numeric);
  50. }
  51. static void
  52. CLASSIFY_save(const void *ip, const struct xt_entry_target *target)
  53. {
  54. const struct xt_classify_target_info *clinfo =
  55. (const struct xt_classify_target_info *)target->data;
  56. printf(" --set-class %.4x:%.4x",
  57. TC_H_MAJ(clinfo->priority)>>16, TC_H_MIN(clinfo->priority));
  58. }
  59. static struct xtables_target classify_target = {
  60. .family = NFPROTO_UNSPEC,
  61. .name = "CLASSIFY",
  62. .version = XTABLES_VERSION,
  63. .size = XT_ALIGN(sizeof(struct xt_classify_target_info)),
  64. .userspacesize = XT_ALIGN(sizeof(struct xt_classify_target_info)),
  65. .help = CLASSIFY_help,
  66. .print = CLASSIFY_print,
  67. .save = CLASSIFY_save,
  68. .x6_parse = CLASSIFY_parse,
  69. .x6_options = CLASSIFY_opts,
  70. };
  71. void _init(void)
  72. {
  73. xtables_register_target(&classify_target);
  74. }