1 /* Cluster IP hashmark target
2 * (C) 2003-2004 by Harald Welte <laforge@netfilter.org>
3 * based on ideas of Fabio Olive Leite <olive@unixforge.org>
5 * Development of this code funded by SuSE Linux AG, http://www.suse.com/
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/module.h>
13 #include <linux/config.h>
14 #include <linux/proc_fs.h>
15 #include <linux/jhash.h>
16 #include <linux/skbuff.h>
18 #include <linux/tcp.h>
19 #include <linux/udp.h>
20 #include <linux/icmp.h>
21 #include <linux/if_arp.h>
22 #include <linux/proc_fs.h>
23 #include <linux/seq_file.h>
25 #include <net/checksum.h>
27 #include <linux/netfilter_arp.h>
29 #include <linux/netfilter_ipv4/ip_tables.h>
30 #include <linux/netfilter_ipv4/ipt_CLUSTERIP.h>
31 #include <linux/netfilter_ipv4/ip_conntrack.h>
33 #define CLUSTERIP_VERSION "0.6"
35 #define DEBUG_CLUSTERIP
37 #ifdef DEBUG_CLUSTERIP
43 MODULE_LICENSE("GPL");
44 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
45 MODULE_DESCRIPTION("iptables target for CLUSTERIP");
47 struct clusterip_config {
48 struct list_head list; /* list of all configs */
49 atomic_t refcount; /* reference count */
51 u_int32_t clusterip; /* the IP address */
52 u_int8_t clustermac[ETH_ALEN]; /* the MAC address */
53 struct net_device *dev; /* device */
54 u_int16_t num_total_nodes; /* total number of nodes */
55 u_int16_t num_local_nodes; /* number of local nodes */
56 u_int16_t local_nodes[CLUSTERIP_MAX_NODES]; /* node number array */
59 struct proc_dir_entry *pde; /* proc dir entry */
61 enum clusterip_hashmode hash_mode; /* which hashing mode */
62 u_int32_t hash_initval; /* hash initialization */
65 static LIST_HEAD(clusterip_configs);
67 /* clusterip_lock protects the clusterip_configs list _AND_ the configurable
68 * data within all structurses (num_local_nodes, local_nodes[]) */
69 DECLARE_RWLOCK(clusterip_lock);
72 static struct file_operations clusterip_proc_fops;
73 static struct proc_dir_entry *clusterip_procdir;
77 clusterip_config_get(struct clusterip_config *c) {
78 atomic_inc(&c->refcount);
82 clusterip_config_put(struct clusterip_config *c) {
83 if (atomic_dec_and_test(&c->refcount)) {
84 WRITE_LOCK(&clusterip_lock);
86 WRITE_UNLOCK(&clusterip_lock);
87 dev_mc_delete(c->dev, c->clustermac, ETH_ALEN, 0);
94 static struct clusterip_config *
95 __clusterip_config_find(u_int32_t clusterip)
97 struct list_head *pos;
99 MUST_BE_READ_LOCKED(&clusterip_lock);
100 list_for_each(pos, &clusterip_configs) {
101 struct clusterip_config *c = list_entry(pos,
102 struct clusterip_config, list);
103 if (c->clusterip == clusterip) {
111 static inline struct clusterip_config *
112 clusterip_config_find_get(u_int32_t clusterip)
114 struct clusterip_config *c;
116 READ_LOCK(&clusterip_lock);
117 c = __clusterip_config_find(clusterip);
119 READ_UNLOCK(&clusterip_lock);
122 atomic_inc(&c->refcount);
123 READ_UNLOCK(&clusterip_lock);
128 static struct clusterip_config *
129 clusterip_config_init(struct ipt_clusterip_tgt_info *i, u_int32_t ip,
130 struct net_device *dev)
132 struct clusterip_config *c;
135 c = kmalloc(sizeof(*c), GFP_ATOMIC);
139 memset(c, 0, sizeof(*c));
142 memcpy(&c->clustermac, &i->clustermac, ETH_ALEN);
143 c->num_total_nodes = i->num_total_nodes;
144 c->num_local_nodes = i->num_local_nodes;
145 memcpy(&c->local_nodes, &i->local_nodes, sizeof(&c->local_nodes));
146 c->hash_mode = i->hash_mode;
147 c->hash_initval = i->hash_initval;
148 atomic_set(&c->refcount, 1);
150 #ifdef CONFIG_PROC_FS
151 /* create proc dir entry */
152 sprintf(buffer, "%u.%u.%u.%u", NIPQUAD(ip));
153 c->pde = create_proc_entry(buffer, S_IWUSR|S_IRUSR, clusterip_procdir);
158 c->pde->proc_fops = &clusterip_proc_fops;
162 WRITE_LOCK(&clusterip_lock);
163 list_add(&c->list, &clusterip_configs);
164 WRITE_UNLOCK(&clusterip_lock);
170 clusterip_add_node(struct clusterip_config *c, u_int16_t nodenum)
174 WRITE_LOCK(&clusterip_lock);
176 if (c->num_local_nodes >= CLUSTERIP_MAX_NODES
177 || nodenum > CLUSTERIP_MAX_NODES) {
178 WRITE_UNLOCK(&clusterip_lock);
182 /* check if we alrady have this number in our array */
183 for (i = 0; i < c->num_local_nodes; i++) {
184 if (c->local_nodes[i] == nodenum) {
185 WRITE_UNLOCK(&clusterip_lock);
190 c->local_nodes[c->num_local_nodes++] = nodenum;
192 WRITE_UNLOCK(&clusterip_lock);
197 clusterip_del_node(struct clusterip_config *c, u_int16_t nodenum)
201 WRITE_LOCK(&clusterip_lock);
203 if (c->num_local_nodes <= 1 || nodenum > CLUSTERIP_MAX_NODES) {
204 WRITE_UNLOCK(&clusterip_lock);
208 for (i = 0; i < c->num_local_nodes; i++) {
209 if (c->local_nodes[i] == nodenum) {
210 int size = sizeof(u_int16_t)*(c->num_local_nodes-(i+1));
211 memmove(&c->local_nodes[i], &c->local_nodes[i+1], size);
212 c->num_local_nodes--;
213 WRITE_UNLOCK(&clusterip_lock);
218 WRITE_UNLOCK(&clusterip_lock);
222 static inline u_int32_t
223 clusterip_hashfn(struct sk_buff *skb, struct clusterip_config *config)
225 struct iphdr *iph = skb->nh.iph;
226 unsigned long hashval;
227 u_int16_t sport, dport;
232 switch (iph->protocol) {
234 th = (void *)iph+iph->ihl*4;
235 sport = ntohs(th->source);
236 dport = ntohs(th->dest);
239 uh = (void *)iph+iph->ihl*4;
240 sport = ntohs(uh->source);
241 dport = ntohs(uh->dest);
244 ih = (void *)iph+iph->ihl*4;
245 sport = ntohs(ih->un.echo.id);
246 dport = (ih->type<<8)|ih->code;
249 if (net_ratelimit()) {
250 printk(KERN_NOTICE "CLUSTERIP: unknown protocol `%u'\n",
256 switch (config->hash_mode) {
257 case CLUSTERIP_HASHMODE_SIP:
258 hashval = jhash_1word(ntohl(iph->saddr),
259 config->hash_initval);
261 case CLUSTERIP_HASHMODE_SIP_SPT:
262 hashval = jhash_2words(ntohl(iph->saddr), sport,
263 config->hash_initval);
265 case CLUSTERIP_HASHMODE_SIP_SPT_DPT:
266 hashval = jhash_3words(ntohl(iph->saddr), sport, dport,
267 config->hash_initval);
270 /* to make gcc happy */
272 /* This cannot happen, unless the check function wasn't called
273 * at rule load time */
274 printk("CLUSTERIP: unknown mode `%u'\n", config->hash_mode);
279 /* node numbers are 1..n, not 0..n */
280 return ((hashval % config->num_total_nodes)+1);
284 clusterip_responsible(struct clusterip_config *config, u_int32_t hash)
288 READ_LOCK(&clusterip_lock);
290 if (config->num_local_nodes == 0) {
291 READ_UNLOCK(&clusterip_lock);
295 for (i = 0; i < config->num_local_nodes; i++) {
296 if (config->local_nodes[i] == hash) {
297 READ_UNLOCK(&clusterip_lock);
302 READ_UNLOCK(&clusterip_lock);
307 /***********************************************************************
309 ***********************************************************************/
312 target(struct sk_buff **pskb,
313 const struct net_device *in,
314 const struct net_device *out,
315 unsigned int hooknum,
316 const void *targinfo,
319 const struct ipt_clusterip_tgt_info *cipinfo = targinfo;
320 enum ip_conntrack_info ctinfo;
321 struct ip_conntrack *ct = ip_conntrack_get((*pskb), &ctinfo);
324 /* don't need to clusterip_config_get() here, since refcount
325 * is only decremented by destroy() - and ip_tables guarantees
326 * that the ->target() function isn't called after ->destroy() */
329 printk(KERN_ERR "CLUSTERIP: no conntrack!\n");
330 /* FIXME: need to drop invalid ones, since replies
331 * to outgoing connections of other nodes will be
332 * marked as INVALID */
336 /* special case: ICMP error handling. conntrack distinguishes between
337 * error messages (RELATED) and information requests (see below) */
338 if ((*pskb)->nh.iph->protocol == IPPROTO_ICMP
339 && (ctinfo == IP_CT_RELATED
340 || ctinfo == IP_CT_IS_REPLY+IP_CT_IS_REPLY))
343 /* ip_conntrack_icmp guarantees us that we only have ICMP_ECHO,
344 * TIMESTAMP, INFO_REQUEST or ADDRESS type icmp packets from here
345 * on, which all have an ID field [relevant for hashing]. */
347 hash = clusterip_hashfn(*pskb, cipinfo->config);
354 case IP_CT_RELATED+IP_CT_IS_REPLY:
355 /* FIXME: we don't handle expectations at the
356 * moment. they can arrive on a different node than
357 * the master connection (e.g. FTP passive mode) */
358 case IP_CT_ESTABLISHED:
359 case IP_CT_ESTABLISHED+IP_CT_IS_REPLY:
365 #ifdef DEBUG_CLUSTERP
366 DUMP_TUPLE(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
368 DEBUGP("hash=%u ct_hash=%lu ", hash, ct->mark);
369 if (!clusterip_responsible(cipinfo->config, hash)) {
370 DEBUGP("not responsible\n");
373 DEBUGP("responsible\n");
375 /* despite being received via linklayer multicast, this is
376 * actually a unicast IP packet. TCP doesn't like PACKET_MULTICAST */
377 (*pskb)->pkt_type = PACKET_HOST;
383 checkentry(const char *tablename,
384 const struct ipt_entry *e,
386 unsigned int targinfosize,
387 unsigned int hook_mask)
389 struct ipt_clusterip_tgt_info *cipinfo = targinfo;
391 struct clusterip_config *config;
393 if (targinfosize != IPT_ALIGN(sizeof(struct ipt_clusterip_tgt_info))) {
394 printk(KERN_WARNING "CLUSTERIP: targinfosize %u != %Zu\n",
396 IPT_ALIGN(sizeof(struct ipt_clusterip_tgt_info)));
400 if (cipinfo->hash_mode != CLUSTERIP_HASHMODE_SIP &&
401 cipinfo->hash_mode != CLUSTERIP_HASHMODE_SIP_SPT &&
402 cipinfo->hash_mode != CLUSTERIP_HASHMODE_SIP_SPT_DPT) {
403 printk(KERN_WARNING "CLUSTERIP: unknown mode `%u'\n",
408 if (e->ip.dmsk.s_addr != 0xffffffff
409 || e->ip.dst.s_addr == 0) {
410 printk(KERN_ERR "CLUSTERIP: Please specify destination IP\n");
414 /* FIXME: further sanity checks */
416 config = clusterip_config_find_get(e->ip.dst.s_addr);
418 if (!(cipinfo->flags & CLUSTERIP_FLAG_NEW)) {
419 printk(KERN_WARNING "CLUSTERIP: no config found for %u.%u.%u.%u, need 'new'\n", NIPQUAD(e->ip.dst.s_addr));
422 struct net_device *dev;
424 if (e->ip.iniface[0] == '\0') {
425 printk(KERN_WARNING "CLUSTERIP: Please specify an interface name\n");
429 dev = dev_get_by_name(e->ip.iniface);
431 printk(KERN_WARNING "CLUSTERIP: no such interface %s\n", e->ip.iniface);
435 config = clusterip_config_init(cipinfo,
436 e->ip.dst.s_addr, dev);
438 printk(KERN_WARNING "CLUSTERIP: cannot allocate config\n");
442 dev_mc_add(config->dev,config->clustermac, ETH_ALEN, 0);
446 cipinfo->config = config;
451 /* drop reference count of cluster config when rule is deleted */
452 static void destroy(void *matchinfo, unsigned int matchinfosize)
454 struct ipt_clusterip_tgt_info *cipinfo = matchinfo;
456 /* we first remove the proc entry and then drop the reference
457 * count. In case anyone still accesses the file, the open/close
458 * functions are also incrementing the refcount on their own */
459 #ifdef CONFIG_PROC_FS
460 remove_proc_entry(cipinfo->config->pde->name,
461 cipinfo->config->pde->parent);
463 clusterip_config_put(cipinfo->config);
466 static struct ipt_target clusterip_tgt = {
469 .checkentry = &checkentry,
475 /***********************************************************************
477 ***********************************************************************/
479 /* hardcoded for 48bit ethernet and 32bit ipv4 addresses */
481 u_int8_t src_hw[ETH_ALEN];
483 u_int8_t dst_hw[ETH_ALEN];
485 } __attribute__ ((packed));
487 #ifdef CLUSTERIP_DEBUG
488 static void arp_print(struct arp_payload *payload)
490 #define HBUFFERLEN 30
491 char hbuffer[HBUFFERLEN];
493 const char hexbuf[]= "0123456789abcdef";
495 for (k=0, j=0; k < HBUFFERLEN-3 && j < ETH_ALEN; j++) {
496 hbuffer[k++]=hexbuf[(payload->src_hw[j]>>4)&15];
497 hbuffer[k++]=hexbuf[payload->src_hw[j]&15];
502 printk("src %u.%u.%u.%u@%s, dst %u.%u.%u.%u\n",
503 NIPQUAD(payload->src_ip), hbuffer,
504 NIPQUAD(payload->dst_ip));
509 arp_mangle(unsigned int hook,
510 struct sk_buff **pskb,
511 const struct net_device *in,
512 const struct net_device *out,
513 int (*okfn)(struct sk_buff *))
515 struct arphdr *arp = (*pskb)->nh.arph;
516 struct arp_payload *payload;
517 struct clusterip_config *c;
519 /* we don't care about non-ethernet and non-ipv4 ARP */
520 if (arp->ar_hrd != htons(ARPHRD_ETHER)
521 || arp->ar_pro != htons(ETH_P_IP)
522 || arp->ar_pln != 4 || arp->ar_hln != ETH_ALEN)
525 /* we only want to mangle arp replies */
526 if (arp->ar_op != htons(ARPOP_REPLY))
529 payload = (void *)(arp+1);
531 /* if there is no clusterip configuration for the arp reply's
532 * source ip, we don't want to mangle it */
533 c = clusterip_config_find_get(payload->src_ip);
537 /* normally the linux kernel always replies to arp queries of
538 * addresses on different interfacs. However, in the CLUSTERIP case
539 * this wouldn't work, since we didn't subscribe the mcast group on
540 * other interfaces */
542 DEBUGP("CLUSTERIP: not mangling arp reply on different "
543 "interface: cip'%s'-skb'%s'\n", c->dev->name, out->name);
544 clusterip_config_put(c);
548 /* mangle reply hardware address */
549 memcpy(payload->src_hw, c->clustermac, arp->ar_hln);
551 #ifdef CLUSTERIP_DEBUG
552 DEBUGP(KERN_DEBUG "CLUSTERIP mangled arp reply: ");
556 clusterip_config_put(c);
561 static struct nf_hook_ops cip_arp_ops = {
564 .hooknum = NF_ARP_OUT,
568 /***********************************************************************
570 ***********************************************************************/
572 #ifdef CONFIG_PROC_FS
574 static void *clusterip_seq_start(struct seq_file *s, loff_t *pos)
576 struct proc_dir_entry *pde = s->private;
577 struct clusterip_config *c = pde->data;
578 unsigned int *nodeidx;
580 READ_LOCK(&clusterip_lock);
581 if (*pos >= c->num_local_nodes)
584 nodeidx = kmalloc(sizeof(unsigned int), GFP_KERNEL);
586 return ERR_PTR(-ENOMEM);
592 static void *clusterip_seq_next(struct seq_file *s, void *v, loff_t *pos)
594 struct proc_dir_entry *pde = s->private;
595 struct clusterip_config *c = pde->data;
596 unsigned int *nodeidx = (unsigned int *)v;
599 if (*pos >= c->num_local_nodes) {
606 static void clusterip_seq_stop(struct seq_file *s, void *v)
610 READ_UNLOCK(&clusterip_lock);
613 static int clusterip_seq_show(struct seq_file *s, void *v)
615 struct proc_dir_entry *pde = s->private;
616 struct clusterip_config *c = pde->data;
617 unsigned int *nodeidx = (unsigned int *)v;
621 seq_printf(s, "%u", c->local_nodes[*nodeidx]);
623 if (*nodeidx == c->num_local_nodes-1)
629 static struct seq_operations clusterip_seq_ops = {
630 .start = clusterip_seq_start,
631 .next = clusterip_seq_next,
632 .stop = clusterip_seq_stop,
633 .show = clusterip_seq_show,
636 static int clusterip_proc_open(struct inode *inode, struct file *file)
638 int ret = seq_open(file, &clusterip_seq_ops);
641 struct seq_file *sf = file->private_data;
642 struct proc_dir_entry *pde = PDE(inode);
643 struct clusterip_config *c = pde->data;
647 clusterip_config_get(c);
653 static int clusterip_proc_release(struct inode *inode, struct file *file)
655 struct proc_dir_entry *pde = PDE(inode);
656 struct clusterip_config *c = pde->data;
659 ret = seq_release(inode, file);
662 clusterip_config_put(c);
667 static ssize_t clusterip_proc_write(struct file *file, const char __user *input,
668 size_t size, loff_t *ofs)
670 #define PROC_WRITELEN 10
671 char buffer[PROC_WRITELEN+1];
672 struct proc_dir_entry *pde = PDE(file->f_dentry->d_inode);
673 struct clusterip_config *c = pde->data;
674 unsigned long nodenum;
676 if (copy_from_user(buffer, input, PROC_WRITELEN))
679 if (*buffer == '+') {
680 nodenum = simple_strtoul(buffer+1, NULL, 10);
681 if (clusterip_add_node(c, nodenum))
683 } else if (*buffer == '-') {
684 nodenum = simple_strtoul(buffer+1, NULL,10);
685 if (clusterip_del_node(c, nodenum))
693 static struct file_operations clusterip_proc_fops = {
694 .owner = THIS_MODULE,
695 .open = clusterip_proc_open,
697 .write = clusterip_proc_write,
699 .release = clusterip_proc_release,
702 #endif /* CONFIG_PROC_FS */
704 static int init_or_cleanup(int fini)
711 if (ipt_register_target(&clusterip_tgt)) {
716 if (nf_register_hook(&cip_arp_ops) < 0) {
721 #ifdef CONFIG_PROC_FS
722 clusterip_procdir = proc_mkdir("ipt_CLUSTERIP", proc_net);
723 if (!clusterip_procdir) {
724 printk(KERN_ERR "CLUSTERIP: Unable to proc dir entry\n");
728 #endif /* CONFIG_PROC_FS */
730 printk(KERN_NOTICE "ClusterIP Version %s loaded successfully\n",
736 printk(KERN_NOTICE "ClusterIP Version %s unloading\n",
738 #ifdef CONFIG_PROC_FS
739 remove_proc_entry(clusterip_procdir->name, clusterip_procdir->parent);
742 nf_unregister_hook(&cip_arp_ops);
744 ipt_unregister_target(&clusterip_tgt);
749 static int __init init(void)
751 return init_or_cleanup(0);
754 static void __exit fini(void)