libipt_CLUSTERIP.c revision 0463ee1f28946cc49815737daa0ced0c68f39f0b
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 <stdio.h> 7#include <string.h> 8#include <stdlib.h> 9#include <getopt.h> 10#include <stddef.h> 11 12#if defined(__GLIBC__) && __GLIBC__ == 2 13#include <net/ethernet.h> 14#else 15#include <linux/if_ether.h> 16#endif 17 18#include <iptables.h> 19#include <linux/netfilter_ipv4/ip_tables.h> 20#include "../include/linux/netfilter_ipv4/ipt_CLUSTERIP.h" 21 22static void 23help(void) 24{ 25 printf( 26"CLUSTERIP target v%s options:\n" 27" --new Create a new ClusterIP\n" 28" --hashmode <mode> Specify hashing mode\n" 29" sourceip\n" 30" sourceip-sourceport\n" 31" sourceip-sourceport-destport\n" 32" --clustermac <mac> Set clusterIP MAC address\n" 33" --total-nodes <num> Set number of total nodes in cluster\n" 34" --local-node <num> Set the local node number\n" 35" --hash-init <num> Set init value of the Jenkins hash\n" 36"\n", 37IPTABLES_VERSION); 38} 39 40#define PARAM_NEW 0x0001 41#define PARAM_HMODE 0x0002 42#define PARAM_MAC 0x0004 43#define PARAM_TOTALNODE 0x0008 44#define PARAM_LOCALNODE 0x0010 45#define PARAM_HASHINIT 0x0020 46 47static const struct option opts[] = { 48 { "new", 0, NULL, '1' }, 49 { "hashmode", 1, NULL, '2' }, 50 { "clustermac", 1, NULL, '3' }, 51 { "total-nodes", 1, NULL, '4' }, 52 { "local-node", 1, NULL, '5' }, 53 { "hash-init", 1, NULL, '6' }, 54 { } 55}; 56 57static void 58parse_mac(const char *mac, char *macbuf) 59{ 60 unsigned int i = 0; 61 62 if (strlen(mac) != ETH_ALEN*3-1) 63 exit_error(PARAMETER_PROBLEM, "Bad mac address `%s'", mac); 64 65 for (i = 0; i < ETH_ALEN; i++) { 66 long number; 67 char *end; 68 69 number = strtol(mac + i*3, &end, 16); 70 71 if (end == mac + i*3 + 2 72 && number >= 0 73 && number <= 255) 74 macbuf[i] = number; 75 else 76 exit_error(PARAMETER_PROBLEM, 77 "Bad mac address `%s'", mac); 78 } 79} 80 81static int 82parse(int c, char **argv, int invert, unsigned int *flags, 83 const void *entry, 84 struct xt_entry_target **target) 85{ 86 struct ipt_clusterip_tgt_info *cipinfo 87 = (struct ipt_clusterip_tgt_info *)(*target)->data; 88 89 switch (c) { 90 unsigned int num; 91 case '1': 92 cipinfo->flags |= CLUSTERIP_FLAG_NEW; 93 if (*flags & PARAM_NEW) 94 exit_error(PARAMETER_PROBLEM, "Can only specify `--new' once\n"); 95 *flags |= PARAM_NEW; 96 break; 97 case '2': 98 if (!(*flags & PARAM_NEW)) 99 exit_error(PARAMETER_PROBLEM, "Can only specify hashmode combined with `--new'\n"); 100 if (*flags & PARAM_HMODE) 101 exit_error(PARAMETER_PROBLEM, "Can only specify hashmode once\n"); 102 if (!strcmp(optarg, "sourceip")) 103 cipinfo->hash_mode = CLUSTERIP_HASHMODE_SIP; 104 else if (!strcmp(optarg, "sourceip-sourceport")) 105 cipinfo->hash_mode = CLUSTERIP_HASHMODE_SIP_SPT; 106 else if (!strcmp(optarg, "sourceip-sourceport-destport")) 107 cipinfo->hash_mode = CLUSTERIP_HASHMODE_SIP_SPT_DPT; 108 else 109 exit_error(PARAMETER_PROBLEM, "Unknown hashmode `%s'\n", 110 optarg); 111 *flags |= PARAM_HMODE; 112 break; 113 case '3': 114 if (!(*flags & PARAM_NEW)) 115 exit_error(PARAMETER_PROBLEM, "Can only specify MAC combined with `--new'\n"); 116 if (*flags & PARAM_MAC) 117 exit_error(PARAMETER_PROBLEM, "Can only specify MAC once\n"); 118 parse_mac(optarg, (char *)cipinfo->clustermac); 119 if (!(cipinfo->clustermac[0] & 0x01)) 120 exit_error(PARAMETER_PROBLEM, "MAC has to be a multicast ethernet address\n"); 121 *flags |= PARAM_MAC; 122 break; 123 case '4': 124 if (!(*flags & PARAM_NEW)) 125 exit_error(PARAMETER_PROBLEM, "Can only specify node number combined with `--new'\n"); 126 if (*flags & PARAM_TOTALNODE) 127 exit_error(PARAMETER_PROBLEM, "Can only specify total node number once\n"); 128 if (string_to_number(optarg, 1, CLUSTERIP_MAX_NODES, &num) < 0) 129 exit_error(PARAMETER_PROBLEM, "Unable to parse `%s'\n", optarg); 130 cipinfo->num_total_nodes = (u_int16_t)num; 131 *flags |= PARAM_TOTALNODE; 132 break; 133 case '5': 134 if (!(*flags & PARAM_NEW)) 135 exit_error(PARAMETER_PROBLEM, "Can only specify node number combined with `--new'\n"); 136 if (*flags & PARAM_LOCALNODE) 137 exit_error(PARAMETER_PROBLEM, "Can only specify local node number once\n"); 138 if (string_to_number(optarg, 1, CLUSTERIP_MAX_NODES, &num) < 0) 139 exit_error(PARAMETER_PROBLEM, "Unable to parse `%s'\n", optarg); 140 cipinfo->num_local_nodes = 1; 141 cipinfo->local_nodes[0] = (u_int16_t)num; 142 *flags |= PARAM_LOCALNODE; 143 break; 144 case '6': 145 if (!(*flags & PARAM_NEW)) 146 exit_error(PARAMETER_PROBLEM, "Can only specify hash init value combined with `--new'\n"); 147 if (*flags & PARAM_HASHINIT) 148 exit_error(PARAMETER_PROBLEM, "Can specify hash init value only once\n"); 149 if (string_to_number(optarg, 0, UINT_MAX, &num) < 0) 150 exit_error(PARAMETER_PROBLEM, "Unable to parse `%s'\n", optarg); 151 cipinfo->hash_initval = num; 152 *flags |= PARAM_HASHINIT; 153 break; 154 default: 155 return 0; 156 } 157 158 return 1; 159} 160 161static void 162final_check(unsigned int flags) 163{ 164 if (flags == 0) 165 return; 166 167 if ((flags & (PARAM_NEW|PARAM_HMODE|PARAM_MAC|PARAM_TOTALNODE|PARAM_LOCALNODE)) 168 == (PARAM_NEW|PARAM_HMODE|PARAM_MAC|PARAM_TOTALNODE|PARAM_LOCALNODE)) 169 return; 170 171 exit_error(PARAMETER_PROBLEM, "CLUSTERIP target: Invalid parameter combination\n"); 172} 173 174static char *hashmode2str(enum clusterip_hashmode mode) 175{ 176 char *retstr; 177 switch (mode) { 178 case CLUSTERIP_HASHMODE_SIP: 179 retstr = "sourceip"; 180 break; 181 case CLUSTERIP_HASHMODE_SIP_SPT: 182 retstr = "sourceip-sourceport"; 183 break; 184 case CLUSTERIP_HASHMODE_SIP_SPT_DPT: 185 retstr = "sourceip-sourceport-destport"; 186 break; 187 default: 188 retstr = "unknown-error"; 189 break; 190 } 191 return retstr; 192} 193 194static char *mac2str(const u_int8_t mac[ETH_ALEN]) 195{ 196 static char buf[ETH_ALEN*3]; 197 sprintf(buf, "%02X:%02X:%02X:%02X:%02X:%02X", 198 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); 199 return buf; 200} 201 202 203/* Prints out the targinfo. */ 204static void 205print(const void *ip, 206 const struct xt_entry_target *target, 207 int numeric) 208{ 209 const struct ipt_clusterip_tgt_info *cipinfo = 210 (const struct ipt_clusterip_tgt_info *)target->data; 211 212 if (!cipinfo->flags & CLUSTERIP_FLAG_NEW) { 213 printf("CLUSTERIP"); 214 return; 215 } 216 217 printf("CLUSTERIP hashmode=%s clustermac=%s total_nodes=%u local_node=%u hash_init=%u", 218 hashmode2str(cipinfo->hash_mode), 219 mac2str(cipinfo->clustermac), 220 cipinfo->num_total_nodes, 221 cipinfo->local_nodes[0], 222 cipinfo->hash_initval); 223} 224 225/* Saves the union ipt_targinfo in parsable form to stdout. */ 226static void 227save(const void *ip, const struct xt_entry_target *target) 228{ 229 const struct ipt_clusterip_tgt_info *cipinfo = 230 (const struct ipt_clusterip_tgt_info *)target->data; 231 232 /* if this is not a new entry, we don't need to save target 233 * parameters */ 234 if (!cipinfo->flags & CLUSTERIP_FLAG_NEW) 235 return; 236 237 printf("--new --hashmode %s --clustermac %s --total-nodes %d --local-node %d --hash-init %u", 238 hashmode2str(cipinfo->hash_mode), 239 mac2str(cipinfo->clustermac), 240 cipinfo->num_total_nodes, 241 cipinfo->local_nodes[0], 242 cipinfo->hash_initval); 243} 244 245static struct iptables_target clusterip = { 246 .name = "CLUSTERIP", 247 .version = IPTABLES_VERSION, 248 .size = IPT_ALIGN(sizeof(struct ipt_clusterip_tgt_info)), 249 .userspacesize = offsetof(struct ipt_clusterip_tgt_info, config), 250 .help = &help, 251 .parse = &parse, 252 .final_check = &final_check, 253 .print = &print, 254 .save = &save, 255 .extra_opts = opts 256}; 257 258void _init(void) 259{ 260 register_target(&clusterip); 261} 262