libipt_CLUSTERIP.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /* Shared library add-on to iptables to add CLUSTERIP target support.
  2. * (C) 2003 by Harald Welte <laforge@gnumonks.org>
  3. *
  4. * Development of this code was funded by SuSE AG, http://www.suse.com/
  5. */
  6. #include <stdbool.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <stdlib.h>
  10. #include <getopt.h>
  11. #include <stddef.h>
  12. #if defined(__GLIBC__) && __GLIBC__ == 2
  13. #include <net/ethernet.h>
  14. #else
  15. #include <linux/if_ether.h>
  16. #endif
  17. #include <xtables.h>
  18. #include <linux/netfilter_ipv4/ipt_CLUSTERIP.h>
  19. enum {
  20. O_NEW = 0,
  21. O_HASHMODE,
  22. O_CLUSTERMAC,
  23. O_TOTAL_NODES,
  24. O_LOCAL_NODE,
  25. O_HASH_INIT,
  26. F_NEW = 1 << O_NEW,
  27. F_HASHMODE = 1 << O_HASHMODE,
  28. F_CLUSTERMAC = 1 << O_CLUSTERMAC,
  29. F_TOTAL_NODES = 1 << O_TOTAL_NODES,
  30. F_LOCAL_NODE = 1 << O_LOCAL_NODE,
  31. F_FULL = F_NEW | F_HASHMODE | F_CLUSTERMAC |
  32. F_TOTAL_NODES | F_LOCAL_NODE,
  33. };
  34. static void CLUSTERIP_help(void)
  35. {
  36. printf(
  37. "CLUSTERIP target options:\n"
  38. " --new Create a new ClusterIP\n"
  39. " --hashmode <mode> Specify hashing mode\n"
  40. " sourceip\n"
  41. " sourceip-sourceport\n"
  42. " sourceip-sourceport-destport\n"
  43. " --clustermac <mac> Set clusterIP MAC address\n"
  44. " --total-nodes <num> Set number of total nodes in cluster\n"
  45. " --local-node <num> Set the local node number\n"
  46. " --hash-init <num> Set init value of the Jenkins hash\n");
  47. }
  48. #define s struct ipt_clusterip_tgt_info
  49. static const struct xt_option_entry CLUSTERIP_opts[] = {
  50. {.name = "new", .id = O_NEW, .type = XTTYPE_NONE},
  51. {.name = "hashmode", .id = O_HASHMODE, .type = XTTYPE_STRING,
  52. .also = O_NEW},
  53. {.name = "clustermac", .id = O_CLUSTERMAC, .type = XTTYPE_ETHERMAC,
  54. .also = O_NEW, .flags = XTOPT_PUT, XTOPT_POINTER(s, clustermac)},
  55. {.name = "total-nodes", .id = O_TOTAL_NODES, .type = XTTYPE_UINT16,
  56. .flags = XTOPT_PUT, XTOPT_POINTER(s, num_total_nodes),
  57. .also = O_NEW, .max = CLUSTERIP_MAX_NODES},
  58. {.name = "local-node", .id = O_LOCAL_NODE, .type = XTTYPE_UINT16,
  59. .flags = XTOPT_PUT, XTOPT_POINTER(s, local_nodes[0]),
  60. .also = O_NEW, .max = CLUSTERIP_MAX_NODES},
  61. {.name = "hash-init", .id = O_HASH_INIT, .type = XTTYPE_UINT32,
  62. .flags = XTOPT_PUT, XTOPT_POINTER(s, hash_initval),
  63. .also = O_NEW, .max = UINT_MAX},
  64. XTOPT_TABLEEND,
  65. };
  66. #undef s
  67. static void CLUSTERIP_parse(struct xt_option_call *cb)
  68. {
  69. struct ipt_clusterip_tgt_info *cipinfo = cb->data;
  70. xtables_option_parse(cb);
  71. switch (cb->entry->id) {
  72. case O_NEW:
  73. cipinfo->flags |= CLUSTERIP_FLAG_NEW;
  74. break;
  75. case O_HASHMODE:
  76. if (strcmp(cb->arg, "sourceip") == 0)
  77. cipinfo->hash_mode = CLUSTERIP_HASHMODE_SIP;
  78. else if (strcmp(cb->arg, "sourceip-sourceport") == 0)
  79. cipinfo->hash_mode = CLUSTERIP_HASHMODE_SIP_SPT;
  80. else if (strcmp(cb->arg, "sourceip-sourceport-destport") == 0)
  81. cipinfo->hash_mode = CLUSTERIP_HASHMODE_SIP_SPT_DPT;
  82. else
  83. xtables_error(PARAMETER_PROBLEM, "Unknown hashmode \"%s\"\n",
  84. cb->arg);
  85. break;
  86. case O_CLUSTERMAC:
  87. if (!(cipinfo->clustermac[0] & 0x01))
  88. xtables_error(PARAMETER_PROBLEM, "MAC has to be a multicast ethernet address\n");
  89. break;
  90. case O_LOCAL_NODE:
  91. cipinfo->num_local_nodes = 1;
  92. break;
  93. }
  94. }
  95. static void CLUSTERIP_check(struct xt_fcheck_call *cb)
  96. {
  97. if (cb->xflags == 0)
  98. return;
  99. if ((cb->xflags & F_FULL) == F_FULL)
  100. return;
  101. xtables_error(PARAMETER_PROBLEM, "CLUSTERIP target: Invalid parameter combination\n");
  102. }
  103. static const char *hashmode2str(enum clusterip_hashmode mode)
  104. {
  105. const char *retstr;
  106. switch (mode) {
  107. case CLUSTERIP_HASHMODE_SIP:
  108. retstr = "sourceip";
  109. break;
  110. case CLUSTERIP_HASHMODE_SIP_SPT:
  111. retstr = "sourceip-sourceport";
  112. break;
  113. case CLUSTERIP_HASHMODE_SIP_SPT_DPT:
  114. retstr = "sourceip-sourceport-destport";
  115. break;
  116. default:
  117. retstr = "unknown-error";
  118. break;
  119. }
  120. return retstr;
  121. }
  122. static const char *mac2str(const uint8_t mac[ETH_ALEN])
  123. {
  124. static char buf[ETH_ALEN*3];
  125. sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X",
  126. mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  127. return buf;
  128. }
  129. static void CLUSTERIP_print(const void *ip,
  130. const struct xt_entry_target *target, int numeric)
  131. {
  132. const struct ipt_clusterip_tgt_info *cipinfo =
  133. (const struct ipt_clusterip_tgt_info *)target->data;
  134. if (!(cipinfo->flags & CLUSTERIP_FLAG_NEW)) {
  135. printf(" CLUSTERIP");
  136. return;
  137. }
  138. printf(" CLUSTERIP hashmode=%s clustermac=%s total_nodes=%u local_node=%u hash_init=%u",
  139. hashmode2str(cipinfo->hash_mode),
  140. mac2str(cipinfo->clustermac),
  141. cipinfo->num_total_nodes,
  142. cipinfo->local_nodes[0],
  143. cipinfo->hash_initval);
  144. }
  145. static void CLUSTERIP_save(const void *ip, const struct xt_entry_target *target)
  146. {
  147. const struct ipt_clusterip_tgt_info *cipinfo =
  148. (const struct ipt_clusterip_tgt_info *)target->data;
  149. /* if this is not a new entry, we don't need to save target
  150. * parameters */
  151. if (!(cipinfo->flags & CLUSTERIP_FLAG_NEW))
  152. return;
  153. printf(" --new --hashmode %s --clustermac %s --total-nodes %d --local-node %d --hash-init %u",
  154. hashmode2str(cipinfo->hash_mode),
  155. mac2str(cipinfo->clustermac),
  156. cipinfo->num_total_nodes,
  157. cipinfo->local_nodes[0],
  158. cipinfo->hash_initval);
  159. }
  160. static struct xtables_target clusterip_tg_reg = {
  161. .name = "CLUSTERIP",
  162. .version = XTABLES_VERSION,
  163. .family = NFPROTO_IPV4,
  164. .size = XT_ALIGN(sizeof(struct ipt_clusterip_tgt_info)),
  165. .userspacesize = offsetof(struct ipt_clusterip_tgt_info, config),
  166. .help = CLUSTERIP_help,
  167. .x6_parse = CLUSTERIP_parse,
  168. .x6_fcheck = CLUSTERIP_check,
  169. .print = CLUSTERIP_print,
  170. .save = CLUSTERIP_save,
  171. .x6_options = CLUSTERIP_opts,
  172. };
  173. void _init(void)
  174. {
  175. xtables_register_target(&clusterip_tg_reg);
  176. }