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.7"
35 #define DEBUG_CLUSTERIP
37 #ifdef DEBUG_CLUSTERIP
43 #define ASSERT_READ_LOCK(x)
45 MODULE_LICENSE("GPL");
46 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
47 MODULE_DESCRIPTION("iptables target for CLUSTERIP");
49 struct clusterip_config {
50 struct list_head list; /* list of all configs */
51 atomic_t refcount; /* reference count */
52 atomic_t entries; /* number of entries/rules
55 u_int32_t clusterip; /* the IP address */
56 u_int8_t clustermac[ETH_ALEN]; /* the MAC address */
57 struct net_device *dev; /* device */
58 u_int16_t num_total_nodes; /* total number of nodes */
59 u_int16_t num_local_nodes; /* number of local nodes */
60 u_int16_t local_nodes[CLUSTERIP_MAX_NODES]; /* node number array */
63 struct proc_dir_entry *pde; /* proc dir entry */
65 enum clusterip_hashmode hash_mode; /* which hashing mode */
66 u_int32_t hash_initval; /* hash initialization */
69 static LIST_HEAD(clusterip_configs);
71 /* clusterip_lock protects the clusterip_configs list _AND_ the configurable
72 * data within all structurses (num_local_nodes, local_nodes[]) */
73 static DEFINE_RWLOCK(clusterip_lock);
76 static struct file_operations clusterip_proc_fops;
77 static struct proc_dir_entry *clusterip_procdir;
81 clusterip_config_get(struct clusterip_config *c)
83 atomic_inc(&c->refcount);
87 clusterip_config_put(struct clusterip_config *c)
89 if (atomic_dec_and_test(&c->refcount))
93 /* increase the count of entries(rules) using/referencing this config */
95 clusterip_config_entry_get(struct clusterip_config *c)
97 atomic_inc(&c->entries);
100 /* decrease the count of entries using/referencing this config. If last
101 * entry(rule) is removed, remove the config from lists, but don't free it
102 * yet, since proc-files could still be holding references */
104 clusterip_config_entry_put(struct clusterip_config *c)
106 if (atomic_dec_and_test(&c->entries)) {
107 write_lock_bh(&clusterip_lock);
109 write_unlock_bh(&clusterip_lock);
111 dev_mc_delete(c->dev, c->clustermac, ETH_ALEN, 0);
114 /* In case anyone still accesses the file, the open/close
115 * functions are also incrementing the refcount on their own,
116 * so it's safe to remove the entry even if it's in use. */
117 #ifdef CONFIG_PROC_FS
118 remove_proc_entry(c->pde->name, c->pde->parent);
123 static struct clusterip_config *
124 __clusterip_config_find(u_int32_t clusterip)
126 struct list_head *pos;
128 ASSERT_READ_LOCK(&clusterip_lock);
129 list_for_each(pos, &clusterip_configs) {
130 struct clusterip_config *c = list_entry(pos,
131 struct clusterip_config, list);
132 if (c->clusterip == clusterip) {
140 static inline struct clusterip_config *
141 clusterip_config_find_get(u_int32_t clusterip, int entry)
143 struct clusterip_config *c;
145 read_lock_bh(&clusterip_lock);
146 c = __clusterip_config_find(clusterip);
148 read_unlock_bh(&clusterip_lock);
151 atomic_inc(&c->refcount);
153 atomic_inc(&c->entries);
154 read_unlock_bh(&clusterip_lock);
159 static struct clusterip_config *
160 clusterip_config_init(struct ipt_clusterip_tgt_info *i, u_int32_t ip,
161 struct net_device *dev)
163 struct clusterip_config *c;
166 c = kmalloc(sizeof(*c), GFP_ATOMIC);
170 memset(c, 0, sizeof(*c));
173 memcpy(&c->clustermac, &i->clustermac, ETH_ALEN);
174 c->num_total_nodes = i->num_total_nodes;
175 c->num_local_nodes = i->num_local_nodes;
176 memcpy(&c->local_nodes, &i->local_nodes, sizeof(c->local_nodes));
177 c->hash_mode = i->hash_mode;
178 c->hash_initval = i->hash_initval;
179 atomic_set(&c->refcount, 1);
180 atomic_set(&c->entries, 1);
182 #ifdef CONFIG_PROC_FS
183 /* create proc dir entry */
184 sprintf(buffer, "%u.%u.%u.%u", NIPQUAD(ip));
185 c->pde = create_proc_entry(buffer, S_IWUSR|S_IRUSR, clusterip_procdir);
190 c->pde->proc_fops = &clusterip_proc_fops;
194 write_lock_bh(&clusterip_lock);
195 list_add(&c->list, &clusterip_configs);
196 write_unlock_bh(&clusterip_lock);
202 clusterip_add_node(struct clusterip_config *c, u_int16_t nodenum)
206 write_lock_bh(&clusterip_lock);
208 if (c->num_local_nodes >= CLUSTERIP_MAX_NODES
209 || nodenum > CLUSTERIP_MAX_NODES) {
210 write_unlock_bh(&clusterip_lock);
214 /* check if we alrady have this number in our array */
215 for (i = 0; i < c->num_local_nodes; i++) {
216 if (c->local_nodes[i] == nodenum) {
217 write_unlock_bh(&clusterip_lock);
222 c->local_nodes[c->num_local_nodes++] = nodenum;
224 write_unlock_bh(&clusterip_lock);
229 clusterip_del_node(struct clusterip_config *c, u_int16_t nodenum)
233 write_lock_bh(&clusterip_lock);
235 if (c->num_local_nodes <= 1 || nodenum > CLUSTERIP_MAX_NODES) {
236 write_unlock_bh(&clusterip_lock);
240 for (i = 0; i < c->num_local_nodes; i++) {
241 if (c->local_nodes[i] == nodenum) {
242 int size = sizeof(u_int16_t)*(c->num_local_nodes-(i+1));
243 memmove(&c->local_nodes[i], &c->local_nodes[i+1], size);
244 c->num_local_nodes--;
245 write_unlock_bh(&clusterip_lock);
250 write_unlock_bh(&clusterip_lock);
254 static inline u_int32_t
255 clusterip_hashfn(struct sk_buff *skb, struct clusterip_config *config)
257 struct iphdr *iph = skb->nh.iph;
258 unsigned long hashval;
259 u_int16_t sport, dport;
264 switch (iph->protocol) {
266 th = (void *)iph+iph->ihl*4;
267 sport = ntohs(th->source);
268 dport = ntohs(th->dest);
271 uh = (void *)iph+iph->ihl*4;
272 sport = ntohs(uh->source);
273 dport = ntohs(uh->dest);
276 ih = (void *)iph+iph->ihl*4;
277 sport = ntohs(ih->un.echo.id);
278 dport = (ih->type<<8)|ih->code;
281 if (net_ratelimit()) {
282 printk(KERN_NOTICE "CLUSTERIP: unknown protocol `%u'\n",
288 switch (config->hash_mode) {
289 case CLUSTERIP_HASHMODE_SIP:
290 hashval = jhash_1word(ntohl(iph->saddr),
291 config->hash_initval);
293 case CLUSTERIP_HASHMODE_SIP_SPT:
294 hashval = jhash_2words(ntohl(iph->saddr), sport,
295 config->hash_initval);
297 case CLUSTERIP_HASHMODE_SIP_SPT_DPT:
298 hashval = jhash_3words(ntohl(iph->saddr), sport, dport,
299 config->hash_initval);
302 /* to make gcc happy */
304 /* This cannot happen, unless the check function wasn't called
305 * at rule load time */
306 printk("CLUSTERIP: unknown mode `%u'\n", config->hash_mode);
311 /* node numbers are 1..n, not 0..n */
312 return ((hashval % config->num_total_nodes)+1);
316 clusterip_responsible(struct clusterip_config *config, u_int32_t hash)
320 read_lock_bh(&clusterip_lock);
322 if (config->num_local_nodes == 0) {
323 read_unlock_bh(&clusterip_lock);
327 for (i = 0; i < config->num_local_nodes; i++) {
328 if (config->local_nodes[i] == hash) {
329 read_unlock_bh(&clusterip_lock);
334 read_unlock_bh(&clusterip_lock);
339 /***********************************************************************
341 ***********************************************************************/
344 target(struct sk_buff **pskb,
345 const struct net_device *in,
346 const struct net_device *out,
347 unsigned int hooknum,
348 const void *targinfo,
351 const struct ipt_clusterip_tgt_info *cipinfo = targinfo;
352 enum ip_conntrack_info ctinfo;
353 struct ip_conntrack *ct = ip_conntrack_get((*pskb), &ctinfo);
356 /* don't need to clusterip_config_get() here, since refcount
357 * is only decremented by destroy() - and ip_tables guarantees
358 * that the ->target() function isn't called after ->destroy() */
361 printk(KERN_ERR "CLUSTERIP: no conntrack!\n");
362 /* FIXME: need to drop invalid ones, since replies
363 * to outgoing connections of other nodes will be
364 * marked as INVALID */
368 /* special case: ICMP error handling. conntrack distinguishes between
369 * error messages (RELATED) and information requests (see below) */
370 if ((*pskb)->nh.iph->protocol == IPPROTO_ICMP
371 && (ctinfo == IP_CT_RELATED
372 || ctinfo == IP_CT_RELATED+IP_CT_IS_REPLY))
375 /* ip_conntrack_icmp guarantees us that we only have ICMP_ECHO,
376 * TIMESTAMP, INFO_REQUEST or ADDRESS type icmp packets from here
377 * on, which all have an ID field [relevant for hashing]. */
379 hash = clusterip_hashfn(*pskb, cipinfo->config);
386 case IP_CT_RELATED+IP_CT_IS_REPLY:
387 /* FIXME: we don't handle expectations at the
388 * moment. they can arrive on a different node than
389 * the master connection (e.g. FTP passive mode) */
390 case IP_CT_ESTABLISHED:
391 case IP_CT_ESTABLISHED+IP_CT_IS_REPLY:
397 #ifdef DEBUG_CLUSTERP
398 DUMP_TUPLE(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
400 DEBUGP("hash=%u ct_hash=%u ", hash, ct->mark);
401 if (!clusterip_responsible(cipinfo->config, hash)) {
402 DEBUGP("not responsible\n");
405 DEBUGP("responsible\n");
407 /* despite being received via linklayer multicast, this is
408 * actually a unicast IP packet. TCP doesn't like PACKET_MULTICAST */
409 (*pskb)->pkt_type = PACKET_HOST;
415 checkentry(const char *tablename,
416 const struct ipt_entry *e,
418 unsigned int targinfosize,
419 unsigned int hook_mask)
421 struct ipt_clusterip_tgt_info *cipinfo = targinfo;
423 struct clusterip_config *config;
425 if (targinfosize != IPT_ALIGN(sizeof(struct ipt_clusterip_tgt_info))) {
426 printk(KERN_WARNING "CLUSTERIP: targinfosize %u != %Zu\n",
428 IPT_ALIGN(sizeof(struct ipt_clusterip_tgt_info)));
432 if (cipinfo->hash_mode != CLUSTERIP_HASHMODE_SIP &&
433 cipinfo->hash_mode != CLUSTERIP_HASHMODE_SIP_SPT &&
434 cipinfo->hash_mode != CLUSTERIP_HASHMODE_SIP_SPT_DPT) {
435 printk(KERN_WARNING "CLUSTERIP: unknown mode `%u'\n",
440 if (e->ip.dmsk.s_addr != 0xffffffff
441 || e->ip.dst.s_addr == 0) {
442 printk(KERN_ERR "CLUSTERIP: Please specify destination IP\n");
446 /* FIXME: further sanity checks */
448 config = clusterip_config_find_get(e->ip.dst.s_addr, 1);
450 if (cipinfo->config != NULL) {
451 /* Case A: This is an entry that gets reloaded, since
452 * it still has a cipinfo->config pointer. Simply
453 * increase the entry refcount and return */
454 if (cipinfo->config != config) {
455 printk(KERN_ERR "CLUSTERIP: Reloaded entry "
456 "has invalid config pointer!\n");
459 clusterip_config_entry_get(cipinfo->config);
461 /* Case B: This is a new rule referring to an existing
462 * clusterip config. */
463 cipinfo->config = config;
464 clusterip_config_entry_get(cipinfo->config);
467 /* Case C: This is a completely new clusterip config */
468 if (!(cipinfo->flags & CLUSTERIP_FLAG_NEW)) {
469 printk(KERN_WARNING "CLUSTERIP: no config found for %u.%u.%u.%u, need 'new'\n", NIPQUAD(e->ip.dst.s_addr));
472 struct net_device *dev;
474 if (e->ip.iniface[0] == '\0') {
475 printk(KERN_WARNING "CLUSTERIP: Please specify an interface name\n");
479 dev = dev_get_by_name(e->ip.iniface);
481 printk(KERN_WARNING "CLUSTERIP: no such interface %s\n", e->ip.iniface);
485 config = clusterip_config_init(cipinfo,
486 e->ip.dst.s_addr, dev);
488 printk(KERN_WARNING "CLUSTERIP: cannot allocate config\n");
492 dev_mc_add(config->dev,config->clustermac, ETH_ALEN, 0);
494 cipinfo->config = config;
500 /* drop reference count of cluster config when rule is deleted */
501 static void destroy(void *matchinfo, unsigned int matchinfosize)
503 struct ipt_clusterip_tgt_info *cipinfo = matchinfo;
505 /* if no more entries are referencing the config, remove it
506 * from the list and destroy the proc entry */
507 clusterip_config_entry_put(cipinfo->config);
509 clusterip_config_put(cipinfo->config);
512 static struct ipt_target clusterip_tgt = {
515 .checkentry = &checkentry,
521 /***********************************************************************
523 ***********************************************************************/
525 /* hardcoded for 48bit ethernet and 32bit ipv4 addresses */
527 u_int8_t src_hw[ETH_ALEN];
529 u_int8_t dst_hw[ETH_ALEN];
531 } __attribute__ ((packed));
533 #ifdef CLUSTERIP_DEBUG
534 static void arp_print(struct arp_payload *payload)
536 #define HBUFFERLEN 30
537 char hbuffer[HBUFFERLEN];
539 const char hexbuf[]= "0123456789abcdef";
541 for (k=0, j=0; k < HBUFFERLEN-3 && j < ETH_ALEN; j++) {
542 hbuffer[k++]=hexbuf[(payload->src_hw[j]>>4)&15];
543 hbuffer[k++]=hexbuf[payload->src_hw[j]&15];
548 printk("src %u.%u.%u.%u@%s, dst %u.%u.%u.%u\n",
549 NIPQUAD(payload->src_ip), hbuffer,
550 NIPQUAD(payload->dst_ip));
555 arp_mangle(unsigned int hook,
556 struct sk_buff **pskb,
557 const struct net_device *in,
558 const struct net_device *out,
559 int (*okfn)(struct sk_buff *))
561 struct arphdr *arp = (*pskb)->nh.arph;
562 struct arp_payload *payload;
563 struct clusterip_config *c;
565 /* we don't care about non-ethernet and non-ipv4 ARP */
566 if (arp->ar_hrd != htons(ARPHRD_ETHER)
567 || arp->ar_pro != htons(ETH_P_IP)
568 || arp->ar_pln != 4 || arp->ar_hln != ETH_ALEN)
571 /* we only want to mangle arp requests and replies */
572 if (arp->ar_op != htons(ARPOP_REPLY)
573 && arp->ar_op != htons(ARPOP_REQUEST))
576 payload = (void *)(arp+1);
578 /* if there is no clusterip configuration for the arp reply's
579 * source ip, we don't want to mangle it */
580 c = clusterip_config_find_get(payload->src_ip, 0);
584 /* normally the linux kernel always replies to arp queries of
585 * addresses on different interfacs. However, in the CLUSTERIP case
586 * this wouldn't work, since we didn't subscribe the mcast group on
587 * other interfaces */
589 DEBUGP("CLUSTERIP: not mangling arp reply on different "
590 "interface: cip'%s'-skb'%s'\n", c->dev->name, out->name);
591 clusterip_config_put(c);
595 /* mangle reply hardware address */
596 memcpy(payload->src_hw, c->clustermac, arp->ar_hln);
598 #ifdef CLUSTERIP_DEBUG
599 DEBUGP(KERN_DEBUG "CLUSTERIP mangled arp reply: ");
603 clusterip_config_put(c);
608 static struct nf_hook_ops cip_arp_ops = {
611 .hooknum = NF_ARP_OUT,
615 /***********************************************************************
617 ***********************************************************************/
619 #ifdef CONFIG_PROC_FS
621 static void *clusterip_seq_start(struct seq_file *s, loff_t *pos)
623 struct proc_dir_entry *pde = s->private;
624 struct clusterip_config *c = pde->data;
625 unsigned int *nodeidx;
627 read_lock_bh(&clusterip_lock);
628 if (*pos >= c->num_local_nodes)
631 nodeidx = kmalloc(sizeof(unsigned int), GFP_KERNEL);
633 return ERR_PTR(-ENOMEM);
639 static void *clusterip_seq_next(struct seq_file *s, void *v, loff_t *pos)
641 struct proc_dir_entry *pde = s->private;
642 struct clusterip_config *c = pde->data;
643 unsigned int *nodeidx = (unsigned int *)v;
646 if (*pos >= c->num_local_nodes) {
653 static void clusterip_seq_stop(struct seq_file *s, void *v)
657 read_unlock_bh(&clusterip_lock);
660 static int clusterip_seq_show(struct seq_file *s, void *v)
662 struct proc_dir_entry *pde = s->private;
663 struct clusterip_config *c = pde->data;
664 unsigned int *nodeidx = (unsigned int *)v;
668 seq_printf(s, "%u", c->local_nodes[*nodeidx]);
670 if (*nodeidx == c->num_local_nodes-1)
676 static struct seq_operations clusterip_seq_ops = {
677 .start = clusterip_seq_start,
678 .next = clusterip_seq_next,
679 .stop = clusterip_seq_stop,
680 .show = clusterip_seq_show,
683 static int clusterip_proc_open(struct inode *inode, struct file *file)
685 int ret = seq_open(file, &clusterip_seq_ops);
688 struct seq_file *sf = file->private_data;
689 struct proc_dir_entry *pde = PDE(inode);
690 struct clusterip_config *c = pde->data;
694 clusterip_config_get(c);
700 static int clusterip_proc_release(struct inode *inode, struct file *file)
702 struct proc_dir_entry *pde = PDE(inode);
703 struct clusterip_config *c = pde->data;
706 ret = seq_release(inode, file);
709 clusterip_config_put(c);
714 static ssize_t clusterip_proc_write(struct file *file, const char __user *input,
715 size_t size, loff_t *ofs)
717 #define PROC_WRITELEN 10
718 char buffer[PROC_WRITELEN+1];
719 struct proc_dir_entry *pde = PDE(file->f_dentry->d_inode);
720 struct clusterip_config *c = pde->data;
721 unsigned long nodenum;
723 if (copy_from_user(buffer, input, PROC_WRITELEN))
726 if (*buffer == '+') {
727 nodenum = simple_strtoul(buffer+1, NULL, 10);
728 if (clusterip_add_node(c, nodenum))
730 } else if (*buffer == '-') {
731 nodenum = simple_strtoul(buffer+1, NULL,10);
732 if (clusterip_del_node(c, nodenum))
740 static struct file_operations clusterip_proc_fops = {
741 .owner = THIS_MODULE,
742 .open = clusterip_proc_open,
744 .write = clusterip_proc_write,
746 .release = clusterip_proc_release,
749 #endif /* CONFIG_PROC_FS */
751 static int init_or_cleanup(int fini)
758 if (ipt_register_target(&clusterip_tgt)) {
763 if (nf_register_hook(&cip_arp_ops) < 0) {
768 #ifdef CONFIG_PROC_FS
769 clusterip_procdir = proc_mkdir("ipt_CLUSTERIP", proc_net);
770 if (!clusterip_procdir) {
771 printk(KERN_ERR "CLUSTERIP: Unable to proc dir entry\n");
775 #endif /* CONFIG_PROC_FS */
777 printk(KERN_NOTICE "ClusterIP Version %s loaded successfully\n",
783 printk(KERN_NOTICE "ClusterIP Version %s unloading\n",
785 #ifdef CONFIG_PROC_FS
786 remove_proc_entry(clusterip_procdir->name, clusterip_procdir->parent);
789 nf_unregister_hook(&cip_arp_ops);
791 ipt_unregister_target(&clusterip_tgt);
796 static int __init init(void)
798 return init_or_cleanup(0);
801 static void __exit fini(void)