libxt_cluster.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * (C) 2009 by Pablo Neira Ayuso <pablo@netfilter.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #include <stdio.h>
  9. #include <xtables.h>
  10. #include <linux/netfilter/xt_cluster.h>
  11. static void
  12. cluster_help(void)
  13. {
  14. printf(
  15. "cluster match options:\n"
  16. " --cluster-total-nodes <num> Set number of total nodes in cluster\n"
  17. " [!] --cluster-local-node <num> Set the local node number\n"
  18. " [!] --cluster-local-nodemask <num> Set the local node mask\n"
  19. " --cluster-hash-seed <num> Set seed value of the Jenkins hash\n");
  20. }
  21. enum {
  22. O_CL_TOTAL_NODES = 0,
  23. O_CL_LOCAL_NODE,
  24. O_CL_LOCAL_NODEMASK,
  25. O_CL_HASH_SEED,
  26. F_CL_TOTAL_NODES = 1 << O_CL_TOTAL_NODES,
  27. F_CL_LOCAL_NODE = 1 << O_CL_LOCAL_NODE,
  28. F_CL_LOCAL_NODEMASK = 1 << O_CL_LOCAL_NODEMASK,
  29. F_CL_HASH_SEED = 1 << O_CL_HASH_SEED,
  30. };
  31. #define s struct xt_cluster_match_info
  32. static const struct xt_option_entry cluster_opts[] = {
  33. {.name = "cluster-total-nodes", .id = O_CL_TOTAL_NODES,
  34. .type = XTTYPE_UINT32, .min = 1, .max = XT_CLUSTER_NODES_MAX,
  35. .flags = XTOPT_MAND | XTOPT_PUT, XTOPT_POINTER(s, total_nodes)},
  36. {.name = "cluster-local-node", .id = O_CL_LOCAL_NODE,
  37. .excl = F_CL_LOCAL_NODEMASK, .flags = XTOPT_INVERT,
  38. .type = XTTYPE_UINT32, .min = 1, .max = XT_CLUSTER_NODES_MAX},
  39. {.name = "cluster-local-nodemask", .id = O_CL_LOCAL_NODEMASK,
  40. .excl = F_CL_LOCAL_NODE, .type = XTTYPE_UINT32,
  41. .min = 1, .max = XT_CLUSTER_NODES_MAX,
  42. .flags = XTOPT_INVERT | XTOPT_PUT, XTOPT_POINTER(s, node_mask)},
  43. {.name = "cluster-hash-seed", .id = O_CL_HASH_SEED,
  44. .type = XTTYPE_UINT32, .flags = XTOPT_MAND | XTOPT_PUT,
  45. XTOPT_POINTER(s, hash_seed)},
  46. XTOPT_TABLEEND,
  47. };
  48. static void cluster_parse(struct xt_option_call *cb)
  49. {
  50. struct xt_cluster_match_info *info = cb->data;
  51. xtables_option_parse(cb);
  52. switch (cb->entry->id) {
  53. case O_CL_LOCAL_NODE:
  54. if (cb->invert)
  55. info->flags |= XT_CLUSTER_F_INV;
  56. info->node_mask = 1 << (cb->val.u32 - 1);
  57. break;
  58. case O_CL_LOCAL_NODEMASK:
  59. if (cb->invert)
  60. info->flags |= XT_CLUSTER_F_INV;
  61. break;
  62. }
  63. }
  64. static void cluster_check(struct xt_fcheck_call *cb)
  65. {
  66. const struct xt_cluster_match_info *info = cb->data;
  67. unsigned int test;
  68. test = F_CL_TOTAL_NODES | F_CL_LOCAL_NODE | F_CL_HASH_SEED;
  69. if ((cb->xflags & test) == test) {
  70. if (info->node_mask >= (1ULL << info->total_nodes))
  71. xtables_error(PARAMETER_PROBLEM,
  72. "cluster match: "
  73. "`--cluster-local-node' "
  74. "must be <= `--cluster-total-nodes'");
  75. return;
  76. }
  77. test = F_CL_TOTAL_NODES | F_CL_LOCAL_NODEMASK | F_CL_HASH_SEED;
  78. if ((cb->xflags & test) == test) {
  79. if (info->node_mask >= (1ULL << info->total_nodes))
  80. xtables_error(PARAMETER_PROBLEM,
  81. "cluster match: "
  82. "`--cluster-local-nodemask' too big "
  83. "for `--cluster-total-nodes'");
  84. return;
  85. }
  86. if (!(cb->xflags & (F_CL_LOCAL_NODE | F_CL_LOCAL_NODEMASK)))
  87. xtables_error(PARAMETER_PROBLEM,
  88. "cluster match: `--cluster-local-node' or"
  89. "`--cluster-local-nodemask' is missing");
  90. }
  91. static void
  92. cluster_print(const void *ip, const struct xt_entry_match *match, int numeric)
  93. {
  94. const struct xt_cluster_match_info *info = (void *)match->data;
  95. printf(" cluster ");
  96. if (info->flags & XT_CLUSTER_F_INV)
  97. printf("!node_mask=0x%08x", info->node_mask);
  98. else
  99. printf("node_mask=0x%08x", info->node_mask);
  100. printf(" total_nodes=%u hash_seed=0x%08x",
  101. info->total_nodes, info->hash_seed);
  102. }
  103. static void
  104. cluster_save(const void *ip, const struct xt_entry_match *match)
  105. {
  106. const struct xt_cluster_match_info *info = (void *)match->data;
  107. if (info->flags & XT_CLUSTER_F_INV)
  108. printf(" ! --cluster-local-nodemask 0x%08x", info->node_mask);
  109. else
  110. printf(" --cluster-local-nodemask 0x%08x", info->node_mask);
  111. printf(" --cluster-total-nodes %u --cluster-hash-seed 0x%08x",
  112. info->total_nodes, info->hash_seed);
  113. }
  114. static struct xtables_match cluster_mt_reg = {
  115. .family = NFPROTO_UNSPEC,
  116. .name = "cluster",
  117. .version = XTABLES_VERSION,
  118. .size = XT_ALIGN(sizeof(struct xt_cluster_match_info)),
  119. .userspacesize = XT_ALIGN(sizeof(struct xt_cluster_match_info)),
  120. .help = cluster_help,
  121. .print = cluster_print,
  122. .save = cluster_save,
  123. .x6_parse = cluster_parse,
  124. .x6_fcheck = cluster_check,
  125. .x6_options = cluster_opts,
  126. };
  127. void _init(void)
  128. {
  129. xtables_register_match(&cluster_mt_reg);
  130. }