1 /* Shared library add-on to iptables to add simple load-balance support. */
8 #include <linux/netfilter_ipv4/ip_tables.h>
9 #include <linux/netfilter_ipv4/ip_nat_rule.h>
11 #define BREAKUP_IP(x) (x)>>24, ((x)>>16) & 0xFF, ((x)>>8) & 0xFF, (x) & 0xFF
13 /* Function which prints out usage message. */
18 "BALANCE v%s options:\n"
19 " --to-destination <ipaddr>-<ipaddr>\n"
20 " Addresses to map destination to.\n",
24 static struct option opts[] = {
25 { "to-destination", 1, 0, '1' },
29 /* Initialize the target. */
31 init(struct ipt_entry_target *t, unsigned int *nfcache)
33 struct ip_nat_multi_range *mr = (struct ip_nat_multi_range *)t->data;
35 /* Actually, it's 0, but it's ignored at the moment. */
38 /* Can't cache this */
39 *nfcache |= NFC_UNKNOWN;
42 /* Parses range of IPs */
44 parse_to(char *arg, struct ip_nat_range *range)
49 range->flags |= IP_NAT_RANGE_MAP_IPS;
50 dash = strchr(arg, '-');
54 exit_error(PARAMETER_PROBLEM, "Bad IP range `%s'\n", arg);
56 ip = dotted_to_addr(arg);
58 exit_error(PARAMETER_PROBLEM, "Bad IP address `%s'\n",
60 range->min_ip = ip->s_addr;
61 ip = dotted_to_addr(dash+1);
63 exit_error(PARAMETER_PROBLEM, "Bad IP address `%s'\n",
65 range->max_ip = ip->s_addr;
68 /* Function which parses command options; returns true if it
71 parse(int c, char **argv, int invert, unsigned int *flags,
72 const struct ipt_entry *entry,
73 struct ipt_entry_target **target)
75 struct ip_nat_multi_range *mr
76 = (struct ip_nat_multi_range *)(*target)->data;
80 if (check_inverse(optarg, &invert, NULL, 0))
81 exit_error(PARAMETER_PROBLEM,
82 "Unexpected `!' after --to-destination");
84 parse_to(optarg, &mr->range[0]);
93 /* Final check; need --to-dest. */
94 static void final_check(unsigned int flags)
97 exit_error(PARAMETER_PROBLEM,
98 "BALANCE needs --to-destination");
101 /* Prints out the targinfo. */
103 print(const struct ipt_ip *ip,
104 const struct ipt_entry_target *target,
107 struct ip_nat_multi_range *mr
108 = (struct ip_nat_multi_range *)target->data;
109 struct ip_nat_range *r = &mr->range[0];
112 a.s_addr = r->min_ip;
114 printf("balance %s", addr_to_dotted(&a));
115 a.s_addr = r->max_ip;
116 printf("-%s ", addr_to_dotted(&a));
119 /* Saves the union ipt_targinfo in parsable form to stdout. */
121 save(const struct ipt_ip *ip, const struct ipt_entry_target *target)
123 struct ip_nat_multi_range *mr
124 = (struct ip_nat_multi_range *)target->data;
125 struct ip_nat_range *r = &mr->range[0];
128 a.s_addr = r->min_ip;
129 printf("--to-destination %s", addr_to_dotted(&a));
130 a.s_addr = r->max_ip;
131 printf("-%s ", addr_to_dotted(&a));
135 struct iptables_target balance
139 IPT_ALIGN(sizeof(struct ip_nat_multi_range)),
140 IPT_ALIGN(sizeof(struct ip_nat_multi_range)),
152 register_target(&balance);