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>
32 #include <linux/netfilter_ipv4/lockhelp.h>
34 #define CLUSTERIP_VERSION "0.6"
36 #define DEBUG_CLUSTERIP
38 #ifdef DEBUG_CLUSTERIP
44 MODULE_LICENSE("GPL");
45 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
46 MODULE_DESCRIPTION("iptables target for CLUSTERIP");
48 struct clusterip_config
{
49 struct list_head list
; /* list of all configs */
50 atomic_t refcount
; /* reference count */
52 u_int32_t clusterip
; /* the IP address */
53 u_int8_t clustermac
[ETH_ALEN
]; /* the MAC address */
54 struct net_device
*dev
; /* device */
55 u_int16_t num_total_nodes
; /* total number of nodes */
56 u_int16_t num_local_nodes
; /* number of local nodes */
57 u_int16_t local_nodes
[CLUSTERIP_MAX_NODES
]; /* node number array */
60 struct proc_dir_entry
*pde
; /* proc dir entry */
62 enum clusterip_hashmode hash_mode
; /* which hashing mode */
63 u_int32_t hash_initval
; /* hash initialization */
66 static LIST_HEAD(clusterip_configs
);
68 /* clusterip_lock protects the clusterip_configs list _AND_ the configurable
69 * data within all structurses (num_local_nodes, local_nodes[]) */
70 static DECLARE_RWLOCK(clusterip_lock
);
73 static struct file_operations clusterip_proc_fops
;
74 static struct proc_dir_entry
*clusterip_procdir
;
78 clusterip_config_get(struct clusterip_config
*c
) {
79 atomic_inc(&c
->refcount
);
83 clusterip_config_put(struct clusterip_config
*c
) {
84 if (atomic_dec_and_test(&c
->refcount
)) {
85 WRITE_LOCK(&clusterip_lock
);
87 WRITE_UNLOCK(&clusterip_lock
);
88 dev_mc_delete(c
->dev
, c
->clustermac
, ETH_ALEN
, 0);
95 static struct clusterip_config
*
96 __clusterip_config_find(u_int32_t clusterip
)
98 struct list_head
*pos
;
100 MUST_BE_READ_LOCKED(&clusterip_lock
);
101 list_for_each(pos
, &clusterip_configs
) {
102 struct clusterip_config
*c
= list_entry(pos
,
103 struct clusterip_config
, list
);
104 if (c
->clusterip
== clusterip
) {
112 static inline struct clusterip_config
*
113 clusterip_config_find_get(u_int32_t clusterip
)
115 struct clusterip_config
*c
;
117 READ_LOCK(&clusterip_lock
);
118 c
= __clusterip_config_find(clusterip
);
120 READ_UNLOCK(&clusterip_lock
);
123 atomic_inc(&c
->refcount
);
124 READ_UNLOCK(&clusterip_lock
);
129 static struct clusterip_config
*
130 clusterip_config_init(struct ipt_clusterip_tgt_info
*i
, u_int32_t ip
,
131 struct net_device
*dev
)
133 struct clusterip_config
*c
;
136 c
= kmalloc(sizeof(*c
), GFP_ATOMIC
);
140 memset(c
, 0, sizeof(*c
));
143 memcpy(&c
->clustermac
, &i
->clustermac
, ETH_ALEN
);
144 c
->num_total_nodes
= i
->num_total_nodes
;
145 c
->num_local_nodes
= i
->num_local_nodes
;
146 memcpy(&c
->local_nodes
, &i
->local_nodes
, sizeof(&c
->local_nodes
));
147 c
->hash_mode
= i
->hash_mode
;
148 c
->hash_initval
= i
->hash_initval
;
149 atomic_set(&c
->refcount
, 1);
151 #ifdef CONFIG_PROC_FS
152 /* create proc dir entry */
153 sprintf(buffer
, "%u.%u.%u.%u", NIPQUAD(ip
));
154 c
->pde
= create_proc_entry(buffer
, S_IWUSR
|S_IRUSR
, clusterip_procdir
);
159 c
->pde
->proc_fops
= &clusterip_proc_fops
;
163 WRITE_LOCK(&clusterip_lock
);
164 list_add(&c
->list
, &clusterip_configs
);
165 WRITE_UNLOCK(&clusterip_lock
);
171 clusterip_add_node(struct clusterip_config
*c
, u_int16_t nodenum
)
175 WRITE_LOCK(&clusterip_lock
);
177 if (c
->num_local_nodes
>= CLUSTERIP_MAX_NODES
178 || nodenum
> CLUSTERIP_MAX_NODES
) {
179 WRITE_UNLOCK(&clusterip_lock
);
183 /* check if we alrady have this number in our array */
184 for (i
= 0; i
< c
->num_local_nodes
; i
++) {
185 if (c
->local_nodes
[i
] == nodenum
) {
186 WRITE_UNLOCK(&clusterip_lock
);
191 c
->local_nodes
[c
->num_local_nodes
++] = nodenum
;
193 WRITE_UNLOCK(&clusterip_lock
);
198 clusterip_del_node(struct clusterip_config
*c
, u_int16_t nodenum
)
202 WRITE_LOCK(&clusterip_lock
);
204 if (c
->num_local_nodes
<= 1 || nodenum
> CLUSTERIP_MAX_NODES
) {
205 WRITE_UNLOCK(&clusterip_lock
);
209 for (i
= 0; i
< c
->num_local_nodes
; i
++) {
210 if (c
->local_nodes
[i
] == nodenum
) {
211 int size
= sizeof(u_int16_t
)*(c
->num_local_nodes
-(i
+1));
212 memmove(&c
->local_nodes
[i
], &c
->local_nodes
[i
+1], size
);
213 c
->num_local_nodes
--;
214 WRITE_UNLOCK(&clusterip_lock
);
219 WRITE_UNLOCK(&clusterip_lock
);
223 static inline u_int32_t
224 clusterip_hashfn(struct sk_buff
*skb
, struct clusterip_config
*config
)
226 struct iphdr
*iph
= skb
->nh
.iph
;
227 unsigned long hashval
;
228 u_int16_t sport
, dport
;
233 switch (iph
->protocol
) {
235 th
= (void *)iph
+iph
->ihl
*4;
236 sport
= ntohs(th
->source
);
237 dport
= ntohs(th
->dest
);
240 uh
= (void *)iph
+iph
->ihl
*4;
241 sport
= ntohs(uh
->source
);
242 dport
= ntohs(uh
->dest
);
245 ih
= (void *)iph
+iph
->ihl
*4;
246 sport
= ntohs(ih
->un
.echo
.id
);
247 dport
= (ih
->type
<<8)|ih
->code
;
250 if (net_ratelimit()) {
251 printk(KERN_NOTICE
"CLUSTERIP: unknown protocol `%u'\n",
257 switch (config
->hash_mode
) {
258 case CLUSTERIP_HASHMODE_SIP
:
259 hashval
= jhash_1word(ntohl(iph
->saddr
),
260 config
->hash_initval
);
262 case CLUSTERIP_HASHMODE_SIP_SPT
:
263 hashval
= jhash_2words(ntohl(iph
->saddr
), sport
,
264 config
->hash_initval
);
266 case CLUSTERIP_HASHMODE_SIP_SPT_DPT
:
267 hashval
= jhash_3words(ntohl(iph
->saddr
), sport
, dport
,
268 config
->hash_initval
);
271 /* to make gcc happy */
273 /* This cannot happen, unless the check function wasn't called
274 * at rule load time */
275 printk("CLUSTERIP: unknown mode `%u'\n", config
->hash_mode
);
280 /* node numbers are 1..n, not 0..n */
281 return ((hashval
% config
->num_total_nodes
)+1);
285 clusterip_responsible(struct clusterip_config
*config
, u_int32_t hash
)
289 READ_LOCK(&clusterip_lock
);
291 if (config
->num_local_nodes
== 0) {
292 READ_UNLOCK(&clusterip_lock
);
296 for (i
= 0; i
< config
->num_local_nodes
; i
++) {
297 if (config
->local_nodes
[i
] == hash
) {
298 READ_UNLOCK(&clusterip_lock
);
303 READ_UNLOCK(&clusterip_lock
);
308 /***********************************************************************
310 ***********************************************************************/
313 target(struct sk_buff
**pskb
,
314 const struct net_device
*in
,
315 const struct net_device
*out
,
316 unsigned int hooknum
,
317 const void *targinfo
,
320 const struct ipt_clusterip_tgt_info
*cipinfo
= targinfo
;
321 enum ip_conntrack_info ctinfo
;
322 struct ip_conntrack
*ct
= ip_conntrack_get((*pskb
), &ctinfo
);
325 /* don't need to clusterip_config_get() here, since refcount
326 * is only decremented by destroy() - and ip_tables guarantees
327 * that the ->target() function isn't called after ->destroy() */
330 printk(KERN_ERR
"CLUSTERIP: no conntrack!\n");
331 /* FIXME: need to drop invalid ones, since replies
332 * to outgoing connections of other nodes will be
333 * marked as INVALID */
337 /* special case: ICMP error handling. conntrack distinguishes between
338 * error messages (RELATED) and information requests (see below) */
339 if ((*pskb
)->nh
.iph
->protocol
== IPPROTO_ICMP
340 && (ctinfo
== IP_CT_RELATED
341 || ctinfo
== IP_CT_IS_REPLY
+IP_CT_IS_REPLY
))
344 /* ip_conntrack_icmp guarantees us that we only have ICMP_ECHO,
345 * TIMESTAMP, INFO_REQUEST or ADDRESS type icmp packets from here
346 * on, which all have an ID field [relevant for hashing]. */
348 hash
= clusterip_hashfn(*pskb
, cipinfo
->config
);
355 case IP_CT_RELATED
+IP_CT_IS_REPLY
:
356 /* FIXME: we don't handle expectations at the
357 * moment. they can arrive on a different node than
358 * the master connection (e.g. FTP passive mode) */
359 case IP_CT_ESTABLISHED
:
360 case IP_CT_ESTABLISHED
+IP_CT_IS_REPLY
:
366 #ifdef DEBUG_CLUSTERP
367 DUMP_TUPLE(&ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
);
369 DEBUGP("hash=%u ct_hash=%lu ", hash
, ct
->mark
);
370 if (!clusterip_responsible(cipinfo
->config
, hash
)) {
371 DEBUGP("not responsible\n");
374 DEBUGP("responsible\n");
376 /* despite being received via linklayer multicast, this is
377 * actually a unicast IP packet. TCP doesn't like PACKET_MULTICAST */
378 (*pskb
)->pkt_type
= PACKET_HOST
;
384 checkentry(const char *tablename
,
385 const struct ipt_entry
*e
,
387 unsigned int targinfosize
,
388 unsigned int hook_mask
)
390 struct ipt_clusterip_tgt_info
*cipinfo
= targinfo
;
392 struct clusterip_config
*config
;
394 if (targinfosize
!= IPT_ALIGN(sizeof(struct ipt_clusterip_tgt_info
))) {
395 printk(KERN_WARNING
"CLUSTERIP: targinfosize %u != %Zu\n",
397 IPT_ALIGN(sizeof(struct ipt_clusterip_tgt_info
)));
401 if (cipinfo
->hash_mode
!= CLUSTERIP_HASHMODE_SIP
&&
402 cipinfo
->hash_mode
!= CLUSTERIP_HASHMODE_SIP_SPT
&&
403 cipinfo
->hash_mode
!= CLUSTERIP_HASHMODE_SIP_SPT_DPT
) {
404 printk(KERN_WARNING
"CLUSTERIP: unknown mode `%u'\n",
409 if (e
->ip
.dmsk
.s_addr
!= 0xffffffff
410 || e
->ip
.dst
.s_addr
== 0) {
411 printk(KERN_ERR
"CLUSTERIP: Please specify destination IP\n");
415 /* FIXME: further sanity checks */
417 config
= clusterip_config_find_get(e
->ip
.dst
.s_addr
);
419 if (!(cipinfo
->flags
& CLUSTERIP_FLAG_NEW
)) {
420 printk(KERN_WARNING
"CLUSTERIP: no config found for %u.%u.%u.%u, need 'new'\n", NIPQUAD(e
->ip
.dst
.s_addr
));
423 struct net_device
*dev
;
425 if (e
->ip
.iniface
[0] == '\0') {
426 printk(KERN_WARNING
"CLUSTERIP: Please specify an interface name\n");
430 dev
= dev_get_by_name(e
->ip
.iniface
);
432 printk(KERN_WARNING
"CLUSTERIP: no such interface %s\n", e
->ip
.iniface
);
436 config
= clusterip_config_init(cipinfo
,
437 e
->ip
.dst
.s_addr
, dev
);
439 printk(KERN_WARNING
"CLUSTERIP: cannot allocate config\n");
443 dev_mc_add(config
->dev
,config
->clustermac
, ETH_ALEN
, 0);
447 cipinfo
->config
= config
;
452 /* drop reference count of cluster config when rule is deleted */
453 static void destroy(void *matchinfo
, unsigned int matchinfosize
)
455 struct ipt_clusterip_tgt_info
*cipinfo
= matchinfo
;
457 /* we first remove the proc entry and then drop the reference
458 * count. In case anyone still accesses the file, the open/close
459 * functions are also incrementing the refcount on their own */
460 #ifdef CONFIG_PROC_FS
461 remove_proc_entry(cipinfo
->config
->pde
->name
,
462 cipinfo
->config
->pde
->parent
);
464 clusterip_config_put(cipinfo
->config
);
467 static struct ipt_target clusterip_tgt
= {
470 .checkentry
= &checkentry
,
476 /***********************************************************************
478 ***********************************************************************/
480 /* hardcoded for 48bit ethernet and 32bit ipv4 addresses */
482 u_int8_t src_hw
[ETH_ALEN
];
484 u_int8_t dst_hw
[ETH_ALEN
];
486 } __attribute__ ((packed
));
488 #ifdef CLUSTERIP_DEBUG
489 static void arp_print(struct arp_payload
*payload
)
491 #define HBUFFERLEN 30
492 char hbuffer
[HBUFFERLEN
];
494 const char hexbuf
[]= "0123456789abcdef";
496 for (k
=0, j
=0; k
< HBUFFERLEN
-3 && j
< ETH_ALEN
; j
++) {
497 hbuffer
[k
++]=hexbuf
[(payload
->src_hw
[j
]>>4)&15];
498 hbuffer
[k
++]=hexbuf
[payload
->src_hw
[j
]&15];
503 printk("src %u.%u.%u.%u@%s, dst %u.%u.%u.%u\n",
504 NIPQUAD(payload
->src_ip
), hbuffer
,
505 NIPQUAD(payload
->dst_ip
));
510 arp_mangle(unsigned int hook
,
511 struct sk_buff
**pskb
,
512 const struct net_device
*in
,
513 const struct net_device
*out
,
514 int (*okfn
)(struct sk_buff
*))
516 struct arphdr
*arp
= (*pskb
)->nh
.arph
;
517 struct arp_payload
*payload
;
518 struct clusterip_config
*c
;
520 /* we don't care about non-ethernet and non-ipv4 ARP */
521 if (arp
->ar_hrd
!= htons(ARPHRD_ETHER
)
522 || arp
->ar_pro
!= htons(ETH_P_IP
)
523 || arp
->ar_pln
!= 4 || arp
->ar_hln
!= ETH_ALEN
)
526 /* we only want to mangle arp replies */
527 if (arp
->ar_op
!= htons(ARPOP_REPLY
))
530 payload
= (void *)(arp
+1);
532 /* if there is no clusterip configuration for the arp reply's
533 * source ip, we don't want to mangle it */
534 c
= clusterip_config_find_get(payload
->src_ip
);
538 /* normally the linux kernel always replies to arp queries of
539 * addresses on different interfacs. However, in the CLUSTERIP case
540 * this wouldn't work, since we didn't subscribe the mcast group on
541 * other interfaces */
543 DEBUGP("CLUSTERIP: not mangling arp reply on different "
544 "interface: cip'%s'-skb'%s'\n", c
->dev
->name
, out
->name
);
545 clusterip_config_put(c
);
549 /* mangle reply hardware address */
550 memcpy(payload
->src_hw
, c
->clustermac
, arp
->ar_hln
);
552 #ifdef CLUSTERIP_DEBUG
553 DEBUGP(KERN_DEBUG
"CLUSTERIP mangled arp reply: ");
557 clusterip_config_put(c
);
562 static struct nf_hook_ops cip_arp_ops
= {
565 .hooknum
= NF_ARP_OUT
,
569 /***********************************************************************
571 ***********************************************************************/
573 #ifdef CONFIG_PROC_FS
575 static void *clusterip_seq_start(struct seq_file
*s
, loff_t
*pos
)
577 struct proc_dir_entry
*pde
= s
->private;
578 struct clusterip_config
*c
= pde
->data
;
579 unsigned int *nodeidx
;
581 READ_LOCK(&clusterip_lock
);
582 if (*pos
>= c
->num_local_nodes
)
585 nodeidx
= kmalloc(sizeof(unsigned int), GFP_KERNEL
);
587 return ERR_PTR(-ENOMEM
);
593 static void *clusterip_seq_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
595 struct proc_dir_entry
*pde
= s
->private;
596 struct clusterip_config
*c
= pde
->data
;
597 unsigned int *nodeidx
= (unsigned int *)v
;
600 if (*pos
>= c
->num_local_nodes
) {
607 static void clusterip_seq_stop(struct seq_file
*s
, void *v
)
611 READ_UNLOCK(&clusterip_lock
);
614 static int clusterip_seq_show(struct seq_file
*s
, void *v
)
616 struct proc_dir_entry
*pde
= s
->private;
617 struct clusterip_config
*c
= pde
->data
;
618 unsigned int *nodeidx
= (unsigned int *)v
;
622 seq_printf(s
, "%u", c
->local_nodes
[*nodeidx
]);
624 if (*nodeidx
== c
->num_local_nodes
-1)
630 static struct seq_operations clusterip_seq_ops
= {
631 .start
= clusterip_seq_start
,
632 .next
= clusterip_seq_next
,
633 .stop
= clusterip_seq_stop
,
634 .show
= clusterip_seq_show
,
637 static int clusterip_proc_open(struct inode
*inode
, struct file
*file
)
639 int ret
= seq_open(file
, &clusterip_seq_ops
);
642 struct seq_file
*sf
= file
->private_data
;
643 struct proc_dir_entry
*pde
= PDE(inode
);
644 struct clusterip_config
*c
= pde
->data
;
648 clusterip_config_get(c
);
654 static int clusterip_proc_release(struct inode
*inode
, struct file
*file
)
656 struct proc_dir_entry
*pde
= PDE(inode
);
657 struct clusterip_config
*c
= pde
->data
;
660 ret
= seq_release(inode
, file
);
663 clusterip_config_put(c
);
668 static ssize_t
clusterip_proc_write(struct file
*file
, const char __user
*input
,
669 size_t size
, loff_t
*ofs
)
671 #define PROC_WRITELEN 10
672 char buffer
[PROC_WRITELEN
+1];
673 struct proc_dir_entry
*pde
= PDE(file
->f_dentry
->d_inode
);
674 struct clusterip_config
*c
= pde
->data
;
675 unsigned long nodenum
;
677 if (copy_from_user(buffer
, input
, PROC_WRITELEN
))
680 if (*buffer
== '+') {
681 nodenum
= simple_strtoul(buffer
+1, NULL
, 10);
682 if (clusterip_add_node(c
, nodenum
))
684 } else if (*buffer
== '-') {
685 nodenum
= simple_strtoul(buffer
+1, NULL
,10);
686 if (clusterip_del_node(c
, nodenum
))
694 static struct file_operations clusterip_proc_fops
= {
695 .owner
= THIS_MODULE
,
696 .open
= clusterip_proc_open
,
698 .write
= clusterip_proc_write
,
700 .release
= clusterip_proc_release
,
703 #endif /* CONFIG_PROC_FS */
705 static int init_or_cleanup(int fini
)
712 if (ipt_register_target(&clusterip_tgt
)) {
717 if (nf_register_hook(&cip_arp_ops
) < 0) {
722 #ifdef CONFIG_PROC_FS
723 clusterip_procdir
= proc_mkdir("ipt_CLUSTERIP", proc_net
);
724 if (!clusterip_procdir
) {
725 printk(KERN_ERR
"CLUSTERIP: Unable to proc dir entry\n");
729 #endif /* CONFIG_PROC_FS */
731 printk(KERN_NOTICE
"ClusterIP Version %s loaded successfully\n",
737 printk(KERN_NOTICE
"ClusterIP Version %s unloading\n",
739 #ifdef CONFIG_PROC_FS
740 remove_proc_entry(clusterip_procdir
->name
, clusterip_procdir
->parent
);
743 nf_unregister_hook(&cip_arp_ops
);
745 ipt_unregister_target(&clusterip_tgt
);
750 static int __init
init(void)
752 return init_or_cleanup(0);
755 static void __exit
fini(void)