libxt_CONNSECMARK.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Shared library add-on to iptables to add CONNSECMARK target support.
  3. *
  4. * Based on the MARK and CONNMARK targets.
  5. *
  6. * Copyright (C) 2006 Red Hat, Inc., James Morris <jmorris@redhat.com>
  7. */
  8. #include <stdio.h>
  9. #include <xtables.h>
  10. #include <linux/netfilter/xt_CONNSECMARK.h>
  11. #define PFX "CONNSECMARK target: "
  12. enum {
  13. O_SAVE = 0,
  14. O_RESTORE,
  15. F_SAVE = 1 << O_SAVE,
  16. F_RESTORE = 1 << O_RESTORE,
  17. };
  18. static void CONNSECMARK_help(void)
  19. {
  20. printf(
  21. "CONNSECMARK target options:\n"
  22. " --save Copy security mark from packet to conntrack\n"
  23. " --restore Copy security mark from connection to packet\n");
  24. }
  25. static const struct xt_option_entry CONNSECMARK_opts[] = {
  26. {.name = "save", .id = O_SAVE, .excl = F_RESTORE, .type = XTTYPE_NONE},
  27. {.name = "restore", .id = O_RESTORE, .excl = F_SAVE,
  28. .type = XTTYPE_NONE},
  29. XTOPT_TABLEEND,
  30. };
  31. static void CONNSECMARK_parse(struct xt_option_call *cb)
  32. {
  33. struct xt_connsecmark_target_info *info = cb->data;
  34. xtables_option_parse(cb);
  35. switch (cb->entry->id) {
  36. case O_SAVE:
  37. info->mode = CONNSECMARK_SAVE;
  38. break;
  39. case O_RESTORE:
  40. info->mode = CONNSECMARK_RESTORE;
  41. break;
  42. }
  43. }
  44. static void CONNSECMARK_check(struct xt_fcheck_call *cb)
  45. {
  46. if (cb->xflags == 0)
  47. xtables_error(PARAMETER_PROBLEM, PFX "parameter required");
  48. }
  49. static void print_connsecmark(const struct xt_connsecmark_target_info *info)
  50. {
  51. switch (info->mode) {
  52. case CONNSECMARK_SAVE:
  53. printf("save");
  54. break;
  55. case CONNSECMARK_RESTORE:
  56. printf("restore");
  57. break;
  58. default:
  59. xtables_error(OTHER_PROBLEM, PFX "invalid mode %hhu\n", info->mode);
  60. }
  61. }
  62. static void
  63. CONNSECMARK_print(const void *ip, const struct xt_entry_target *target,
  64. int numeric)
  65. {
  66. const struct xt_connsecmark_target_info *info =
  67. (struct xt_connsecmark_target_info*)(target)->data;
  68. printf(" CONNSECMARK ");
  69. print_connsecmark(info);
  70. }
  71. static void
  72. CONNSECMARK_save(const void *ip, const struct xt_entry_target *target)
  73. {
  74. const struct xt_connsecmark_target_info *info =
  75. (struct xt_connsecmark_target_info*)target->data;
  76. printf(" --");
  77. print_connsecmark(info);
  78. }
  79. static struct xtables_target connsecmark_target = {
  80. .family = NFPROTO_UNSPEC,
  81. .name = "CONNSECMARK",
  82. .version = XTABLES_VERSION,
  83. .revision = 0,
  84. .size = XT_ALIGN(sizeof(struct xt_connsecmark_target_info)),
  85. .userspacesize = XT_ALIGN(sizeof(struct xt_connsecmark_target_info)),
  86. .help = CONNSECMARK_help,
  87. .print = CONNSECMARK_print,
  88. .save = CONNSECMARK_save,
  89. .x6_parse = CONNSECMARK_parse,
  90. .x6_fcheck = CONNSECMARK_check,
  91. .x6_options = CONNSECMARK_opts,
  92. };
  93. void _init(void)
  94. {
  95. xtables_register_target(&connsecmark_target);
  96. }