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/proc_fs.h>
14 #include <linux/jhash.h>
15 #include <linux/bitops.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/seq_file.h>
23 #include <linux/netfilter_arp.h>
24 #include <linux/netfilter/x_tables.h>
25 #include <linux/netfilter_ipv4/ip_tables.h>
26 #include <linux/netfilter_ipv4/ipt_CLUSTERIP.h>
27 #include <net/netfilter/nf_conntrack.h>
28 #include <net/checksum.h>
30 #define CLUSTERIP_VERSION "0.8"
32 MODULE_LICENSE("GPL");
33 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
34 MODULE_DESCRIPTION("iptables target for CLUSTERIP");
36 struct clusterip_config
{
37 struct list_head list
; /* list of all configs */
38 atomic_t refcount
; /* reference count */
39 atomic_t entries
; /* number of entries/rules
42 __be32 clusterip
; /* the IP address */
43 u_int8_t clustermac
[ETH_ALEN
]; /* the MAC address */
44 struct net_device
*dev
; /* device */
45 u_int16_t num_total_nodes
; /* total number of nodes */
46 unsigned long local_nodes
; /* node number array */
49 struct proc_dir_entry
*pde
; /* proc dir entry */
51 enum clusterip_hashmode hash_mode
; /* which hashing mode */
52 u_int32_t hash_initval
; /* hash initialization */
55 static LIST_HEAD(clusterip_configs
);
57 /* clusterip_lock protects the clusterip_configs list */
58 static DEFINE_RWLOCK(clusterip_lock
);
61 static const struct file_operations clusterip_proc_fops
;
62 static struct proc_dir_entry
*clusterip_procdir
;
66 clusterip_config_get(struct clusterip_config
*c
)
68 atomic_inc(&c
->refcount
);
72 clusterip_config_put(struct clusterip_config
*c
)
74 if (atomic_dec_and_test(&c
->refcount
))
78 /* increase the count of entries(rules) using/referencing this config */
80 clusterip_config_entry_get(struct clusterip_config
*c
)
82 atomic_inc(&c
->entries
);
85 /* decrease the count of entries using/referencing this config. If last
86 * entry(rule) is removed, remove the config from lists, but don't free it
87 * yet, since proc-files could still be holding references */
89 clusterip_config_entry_put(struct clusterip_config
*c
)
91 if (atomic_dec_and_test(&c
->entries
)) {
92 write_lock_bh(&clusterip_lock
);
94 write_unlock_bh(&clusterip_lock
);
96 dev_mc_delete(c
->dev
, c
->clustermac
, ETH_ALEN
, 0);
99 /* In case anyone still accesses the file, the open/close
100 * functions are also incrementing the refcount on their own,
101 * so it's safe to remove the entry even if it's in use. */
102 #ifdef CONFIG_PROC_FS
103 remove_proc_entry(c
->pde
->name
, c
->pde
->parent
);
108 static struct clusterip_config
*
109 __clusterip_config_find(__be32 clusterip
)
111 struct list_head
*pos
;
113 list_for_each(pos
, &clusterip_configs
) {
114 struct clusterip_config
*c
= list_entry(pos
,
115 struct clusterip_config
, list
);
116 if (c
->clusterip
== clusterip
)
123 static inline struct clusterip_config
*
124 clusterip_config_find_get(__be32 clusterip
, int entry
)
126 struct clusterip_config
*c
;
128 read_lock_bh(&clusterip_lock
);
129 c
= __clusterip_config_find(clusterip
);
131 read_unlock_bh(&clusterip_lock
);
134 atomic_inc(&c
->refcount
);
136 atomic_inc(&c
->entries
);
137 read_unlock_bh(&clusterip_lock
);
143 clusterip_config_init_nodelist(struct clusterip_config
*c
,
144 const struct ipt_clusterip_tgt_info
*i
)
148 for (n
= 0; n
< i
->num_local_nodes
; n
++)
149 set_bit(i
->local_nodes
[n
] - 1, &c
->local_nodes
);
152 static struct clusterip_config
*
153 clusterip_config_init(struct ipt_clusterip_tgt_info
*i
, __be32 ip
,
154 struct net_device
*dev
)
156 struct clusterip_config
*c
;
158 c
= kzalloc(sizeof(*c
), GFP_ATOMIC
);
164 memcpy(&c
->clustermac
, &i
->clustermac
, ETH_ALEN
);
165 c
->num_total_nodes
= i
->num_total_nodes
;
166 clusterip_config_init_nodelist(c
, i
);
167 c
->hash_mode
= i
->hash_mode
;
168 c
->hash_initval
= i
->hash_initval
;
169 atomic_set(&c
->refcount
, 1);
170 atomic_set(&c
->entries
, 1);
172 #ifdef CONFIG_PROC_FS
176 /* create proc dir entry */
177 sprintf(buffer
, "%u.%u.%u.%u", NIPQUAD(ip
));
178 c
->pde
= create_proc_entry(buffer
, S_IWUSR
|S_IRUSR
,
185 c
->pde
->proc_fops
= &clusterip_proc_fops
;
189 write_lock_bh(&clusterip_lock
);
190 list_add(&c
->list
, &clusterip_configs
);
191 write_unlock_bh(&clusterip_lock
);
196 #ifdef CONFIG_PROC_FS
198 clusterip_add_node(struct clusterip_config
*c
, u_int16_t nodenum
)
202 nodenum
> c
->num_total_nodes
)
205 /* check if we already have this number in our bitfield */
206 if (test_and_set_bit(nodenum
- 1, &c
->local_nodes
))
213 clusterip_del_node(struct clusterip_config
*c
, u_int16_t nodenum
)
216 nodenum
> c
->num_total_nodes
)
219 if (test_and_clear_bit(nodenum
- 1, &c
->local_nodes
))
226 static inline u_int32_t
227 clusterip_hashfn(const struct sk_buff
*skb
,
228 const struct clusterip_config
*config
)
230 const struct iphdr
*iph
= ip_hdr(skb
);
231 unsigned long hashval
;
232 u_int16_t sport
, dport
;
233 const u_int16_t
*ports
;
235 switch (iph
->protocol
) {
238 case IPPROTO_UDPLITE
:
242 ports
= (const void *)iph
+iph
->ihl
*4;
248 printk(KERN_NOTICE
"CLUSTERIP: unknown protocol `%u'\n",
253 switch (config
->hash_mode
) {
254 case CLUSTERIP_HASHMODE_SIP
:
255 hashval
= jhash_1word(ntohl(iph
->saddr
),
256 config
->hash_initval
);
258 case CLUSTERIP_HASHMODE_SIP_SPT
:
259 hashval
= jhash_2words(ntohl(iph
->saddr
), sport
,
260 config
->hash_initval
);
262 case CLUSTERIP_HASHMODE_SIP_SPT_DPT
:
263 hashval
= jhash_3words(ntohl(iph
->saddr
), sport
, dport
,
264 config
->hash_initval
);
267 /* to make gcc happy */
269 /* This cannot happen, unless the check function wasn't called
270 * at rule load time */
271 printk("CLUSTERIP: unknown mode `%u'\n", config
->hash_mode
);
276 /* node numbers are 1..n, not 0..n */
277 return (hashval
% config
->num_total_nodes
) + 1;
281 clusterip_responsible(const struct clusterip_config
*config
, u_int32_t hash
)
283 return test_bit(hash
- 1, &config
->local_nodes
);
286 /***********************************************************************
288 ***********************************************************************/
291 target(struct sk_buff
**pskb
,
292 const struct net_device
*in
,
293 const struct net_device
*out
,
294 unsigned int hooknum
,
295 const struct xt_target
*target
,
296 const void *targinfo
)
298 const struct ipt_clusterip_tgt_info
*cipinfo
= targinfo
;
300 enum ip_conntrack_info ctinfo
;
303 /* don't need to clusterip_config_get() here, since refcount
304 * is only decremented by destroy() - and ip_tables guarantees
305 * that the ->target() function isn't called after ->destroy() */
307 ct
= nf_ct_get(*pskb
, &ctinfo
);
309 printk(KERN_ERR
"CLUSTERIP: no conntrack!\n");
310 /* FIXME: need to drop invalid ones, since replies
311 * to outgoing connections of other nodes will be
312 * marked as INVALID */
316 /* special case: ICMP error handling. conntrack distinguishes between
317 * error messages (RELATED) and information requests (see below) */
318 if (ip_hdr(*pskb
)->protocol
== IPPROTO_ICMP
319 && (ctinfo
== IP_CT_RELATED
320 || ctinfo
== IP_CT_RELATED
+IP_CT_IS_REPLY
))
323 /* ip_conntrack_icmp guarantees us that we only have ICMP_ECHO,
324 * TIMESTAMP, INFO_REQUEST or ADDRESS type icmp packets from here
325 * on, which all have an ID field [relevant for hashing]. */
327 hash
= clusterip_hashfn(*pskb
, cipinfo
->config
);
334 case IP_CT_RELATED
+IP_CT_IS_REPLY
:
335 /* FIXME: we don't handle expectations at the
336 * moment. they can arrive on a different node than
337 * the master connection (e.g. FTP passive mode) */
338 case IP_CT_ESTABLISHED
:
339 case IP_CT_ESTABLISHED
+IP_CT_IS_REPLY
:
346 DUMP_TUPLE(&ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
);
348 pr_debug("hash=%u ct_hash=%u ", hash
, ct
->mark
);
349 if (!clusterip_responsible(cipinfo
->config
, hash
)) {
350 pr_debug("not responsible\n");
353 pr_debug("responsible\n");
355 /* despite being received via linklayer multicast, this is
356 * actually a unicast IP packet. TCP doesn't like PACKET_MULTICAST */
357 (*pskb
)->pkt_type
= PACKET_HOST
;
363 checkentry(const char *tablename
,
365 const struct xt_target
*target
,
367 unsigned int hook_mask
)
369 struct ipt_clusterip_tgt_info
*cipinfo
= targinfo
;
370 const struct ipt_entry
*e
= e_void
;
372 struct clusterip_config
*config
;
374 if (cipinfo
->hash_mode
!= CLUSTERIP_HASHMODE_SIP
&&
375 cipinfo
->hash_mode
!= CLUSTERIP_HASHMODE_SIP_SPT
&&
376 cipinfo
->hash_mode
!= CLUSTERIP_HASHMODE_SIP_SPT_DPT
) {
377 printk(KERN_WARNING
"CLUSTERIP: unknown mode `%u'\n",
382 if (e
->ip
.dmsk
.s_addr
!= htonl(0xffffffff)
383 || e
->ip
.dst
.s_addr
== 0) {
384 printk(KERN_ERR
"CLUSTERIP: Please specify destination IP\n");
388 /* FIXME: further sanity checks */
390 config
= clusterip_config_find_get(e
->ip
.dst
.s_addr
, 1);
392 if (!(cipinfo
->flags
& CLUSTERIP_FLAG_NEW
)) {
393 printk(KERN_WARNING
"CLUSTERIP: no config found for %u.%u.%u.%u, need 'new'\n", NIPQUAD(e
->ip
.dst
.s_addr
));
396 struct net_device
*dev
;
398 if (e
->ip
.iniface
[0] == '\0') {
399 printk(KERN_WARNING
"CLUSTERIP: Please specify an interface name\n");
403 dev
= dev_get_by_name(e
->ip
.iniface
);
405 printk(KERN_WARNING
"CLUSTERIP: no such interface %s\n", e
->ip
.iniface
);
409 config
= clusterip_config_init(cipinfo
,
410 e
->ip
.dst
.s_addr
, dev
);
412 printk(KERN_WARNING
"CLUSTERIP: cannot allocate config\n");
416 dev_mc_add(config
->dev
,config
->clustermac
, ETH_ALEN
, 0);
419 cipinfo
->config
= config
;
421 if (nf_ct_l3proto_try_module_get(target
->family
) < 0) {
422 printk(KERN_WARNING
"can't load conntrack support for "
423 "proto=%d\n", target
->family
);
430 /* drop reference count of cluster config when rule is deleted */
431 static void destroy(const struct xt_target
*target
, void *targinfo
)
433 struct ipt_clusterip_tgt_info
*cipinfo
= targinfo
;
435 /* if no more entries are referencing the config, remove it
436 * from the list and destroy the proc entry */
437 clusterip_config_entry_put(cipinfo
->config
);
439 clusterip_config_put(cipinfo
->config
);
441 nf_ct_l3proto_module_put(target
->family
);
445 struct compat_ipt_clusterip_tgt_info
448 u_int8_t clustermac
[6];
449 u_int16_t num_total_nodes
;
450 u_int16_t num_local_nodes
;
451 u_int16_t local_nodes
[CLUSTERIP_MAX_NODES
];
453 u_int32_t hash_initval
;
454 compat_uptr_t config
;
456 #endif /* CONFIG_COMPAT */
458 static struct xt_target clusterip_tgt __read_mostly
= {
462 .checkentry
= checkentry
,
464 .targetsize
= sizeof(struct ipt_clusterip_tgt_info
),
466 .compatsize
= sizeof(struct compat_ipt_clusterip_tgt_info
),
467 #endif /* CONFIG_COMPAT */
472 /***********************************************************************
474 ***********************************************************************/
476 /* hardcoded for 48bit ethernet and 32bit ipv4 addresses */
478 u_int8_t src_hw
[ETH_ALEN
];
480 u_int8_t dst_hw
[ETH_ALEN
];
482 } __attribute__ ((packed
));
485 static void arp_print(struct arp_payload
*payload
)
487 #define HBUFFERLEN 30
488 char hbuffer
[HBUFFERLEN
];
490 const char hexbuf
[]= "0123456789abcdef";
492 for (k
=0, j
=0; k
< HBUFFERLEN
-3 && j
< ETH_ALEN
; j
++) {
493 hbuffer
[k
++]=hexbuf
[(payload
->src_hw
[j
]>>4)&15];
494 hbuffer
[k
++]=hexbuf
[payload
->src_hw
[j
]&15];
499 printk("src %u.%u.%u.%u@%s, dst %u.%u.%u.%u\n",
500 NIPQUAD(payload
->src_ip
), hbuffer
,
501 NIPQUAD(payload
->dst_ip
));
506 arp_mangle(unsigned int hook
,
507 struct sk_buff
**pskb
,
508 const struct net_device
*in
,
509 const struct net_device
*out
,
510 int (*okfn
)(struct sk_buff
*))
512 struct arphdr
*arp
= arp_hdr(*pskb
);
513 struct arp_payload
*payload
;
514 struct clusterip_config
*c
;
516 /* we don't care about non-ethernet and non-ipv4 ARP */
517 if (arp
->ar_hrd
!= htons(ARPHRD_ETHER
)
518 || arp
->ar_pro
!= htons(ETH_P_IP
)
519 || arp
->ar_pln
!= 4 || arp
->ar_hln
!= ETH_ALEN
)
522 /* we only want to mangle arp requests and replies */
523 if (arp
->ar_op
!= htons(ARPOP_REPLY
)
524 && arp
->ar_op
!= htons(ARPOP_REQUEST
))
527 payload
= (void *)(arp
+1);
529 /* if there is no clusterip configuration for the arp reply's
530 * source ip, we don't want to mangle it */
531 c
= clusterip_config_find_get(payload
->src_ip
, 0);
535 /* normally the linux kernel always replies to arp queries of
536 * addresses on different interfacs. However, in the CLUSTERIP case
537 * this wouldn't work, since we didn't subscribe the mcast group on
538 * other interfaces */
540 pr_debug("CLUSTERIP: not mangling arp reply on different "
541 "interface: cip'%s'-skb'%s'\n",
542 c
->dev
->name
, out
->name
);
543 clusterip_config_put(c
);
547 /* mangle reply hardware address */
548 memcpy(payload
->src_hw
, c
->clustermac
, arp
->ar_hln
);
551 pr_debug(KERN_DEBUG
"CLUSTERIP mangled arp reply: ");
555 clusterip_config_put(c
);
560 static struct nf_hook_ops cip_arp_ops
= {
563 .hooknum
= NF_ARP_OUT
,
567 /***********************************************************************
569 ***********************************************************************/
571 #ifdef CONFIG_PROC_FS
573 struct clusterip_seq_position
{
574 unsigned int pos
; /* position */
575 unsigned int weight
; /* number of bits set == size */
576 unsigned int bit
; /* current bit */
577 unsigned long val
; /* current value */
580 static void *clusterip_seq_start(struct seq_file
*s
, loff_t
*pos
)
582 struct proc_dir_entry
*pde
= s
->private;
583 struct clusterip_config
*c
= pde
->data
;
585 u_int32_t local_nodes
;
586 struct clusterip_seq_position
*idx
;
588 /* FIXME: possible race */
589 local_nodes
= c
->local_nodes
;
590 weight
= hweight32(local_nodes
);
594 idx
= kmalloc(sizeof(struct clusterip_seq_position
), GFP_KERNEL
);
596 return ERR_PTR(-ENOMEM
);
599 idx
->weight
= weight
;
600 idx
->bit
= ffs(local_nodes
);
601 idx
->val
= local_nodes
;
602 clear_bit(idx
->bit
- 1, &idx
->val
);
607 static void *clusterip_seq_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
609 struct clusterip_seq_position
*idx
= (struct clusterip_seq_position
*)v
;
612 if (*pos
>= idx
->weight
) {
616 idx
->bit
= ffs(idx
->val
);
617 clear_bit(idx
->bit
- 1, &idx
->val
);
621 static void clusterip_seq_stop(struct seq_file
*s
, void *v
)
626 static int clusterip_seq_show(struct seq_file
*s
, void *v
)
628 struct clusterip_seq_position
*idx
= (struct clusterip_seq_position
*)v
;
633 seq_printf(s
, "%u", idx
->bit
);
635 if (idx
->pos
== idx
->weight
- 1)
641 static const struct seq_operations clusterip_seq_ops
= {
642 .start
= clusterip_seq_start
,
643 .next
= clusterip_seq_next
,
644 .stop
= clusterip_seq_stop
,
645 .show
= clusterip_seq_show
,
648 static int clusterip_proc_open(struct inode
*inode
, struct file
*file
)
650 int ret
= seq_open(file
, &clusterip_seq_ops
);
653 struct seq_file
*sf
= file
->private_data
;
654 struct proc_dir_entry
*pde
= PDE(inode
);
655 struct clusterip_config
*c
= pde
->data
;
659 clusterip_config_get(c
);
665 static int clusterip_proc_release(struct inode
*inode
, struct file
*file
)
667 struct proc_dir_entry
*pde
= PDE(inode
);
668 struct clusterip_config
*c
= pde
->data
;
671 ret
= seq_release(inode
, file
);
674 clusterip_config_put(c
);
679 static ssize_t
clusterip_proc_write(struct file
*file
, const char __user
*input
,
680 size_t size
, loff_t
*ofs
)
682 #define PROC_WRITELEN 10
683 char buffer
[PROC_WRITELEN
+1];
684 struct proc_dir_entry
*pde
= PDE(file
->f_path
.dentry
->d_inode
);
685 struct clusterip_config
*c
= pde
->data
;
686 unsigned long nodenum
;
688 if (copy_from_user(buffer
, input
, PROC_WRITELEN
))
691 if (*buffer
== '+') {
692 nodenum
= simple_strtoul(buffer
+1, NULL
, 10);
693 if (clusterip_add_node(c
, nodenum
))
695 } else if (*buffer
== '-') {
696 nodenum
= simple_strtoul(buffer
+1, NULL
,10);
697 if (clusterip_del_node(c
, nodenum
))
705 static const struct file_operations clusterip_proc_fops
= {
706 .owner
= THIS_MODULE
,
707 .open
= clusterip_proc_open
,
709 .write
= clusterip_proc_write
,
711 .release
= clusterip_proc_release
,
714 #endif /* CONFIG_PROC_FS */
716 static int __init
ipt_clusterip_init(void)
720 ret
= xt_register_target(&clusterip_tgt
);
724 ret
= nf_register_hook(&cip_arp_ops
);
728 #ifdef CONFIG_PROC_FS
729 clusterip_procdir
= proc_mkdir("ipt_CLUSTERIP", proc_net
);
730 if (!clusterip_procdir
) {
731 printk(KERN_ERR
"CLUSTERIP: Unable to proc dir entry\n");
735 #endif /* CONFIG_PROC_FS */
737 printk(KERN_NOTICE
"ClusterIP Version %s loaded successfully\n",
741 #ifdef CONFIG_PROC_FS
743 nf_unregister_hook(&cip_arp_ops
);
744 #endif /* CONFIG_PROC_FS */
746 xt_unregister_target(&clusterip_tgt
);
750 static void __exit
ipt_clusterip_fini(void)
752 printk(KERN_NOTICE
"ClusterIP Version %s unloading\n",
754 #ifdef CONFIG_PROC_FS
755 remove_proc_entry(clusterip_procdir
->name
, clusterip_procdir
->parent
);
757 nf_unregister_hook(&cip_arp_ops
);
758 xt_unregister_target(&clusterip_tgt
);
761 module_init(ipt_clusterip_init
);
762 module_exit(ipt_clusterip_fini
);