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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/module.h>
14 #include <linux/proc_fs.h>
15 #include <linux/jhash.h>
16 #include <linux/bitops.h>
17 #include <linux/skbuff.h>
18 #include <linux/slab.h>
20 #include <linux/tcp.h>
21 #include <linux/udp.h>
22 #include <linux/icmp.h>
23 #include <linux/if_arp.h>
24 #include <linux/seq_file.h>
25 #include <linux/refcount.h>
26 #include <linux/netfilter_arp.h>
27 #include <linux/netfilter/x_tables.h>
28 #include <linux/netfilter_ipv4/ip_tables.h>
29 #include <linux/netfilter_ipv4/ipt_CLUSTERIP.h>
30 #include <net/netfilter/nf_conntrack.h>
31 #include <net/net_namespace.h>
32 #include <net/netns/generic.h>
33 #include <net/checksum.h>
36 #define CLUSTERIP_VERSION "0.8"
38 MODULE_LICENSE("GPL");
39 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
40 MODULE_DESCRIPTION("Xtables: CLUSTERIP target");
42 struct clusterip_config
{
43 struct list_head list
; /* list of all configs */
44 refcount_t refcount
; /* reference count */
45 refcount_t entries
; /* number of entries/rules
48 __be32 clusterip
; /* the IP address */
49 u_int8_t clustermac
[ETH_ALEN
]; /* the MAC address */
50 struct net_device
*dev
; /* device */
51 u_int16_t num_total_nodes
; /* total number of nodes */
52 unsigned long local_nodes
; /* node number array */
55 struct proc_dir_entry
*pde
; /* proc dir entry */
57 enum clusterip_hashmode hash_mode
; /* which hashing mode */
58 u_int32_t hash_initval
; /* hash initialization */
63 static const struct file_operations clusterip_proc_fops
;
66 static unsigned int clusterip_net_id __read_mostly
;
68 struct clusterip_net
{
69 struct list_head configs
;
70 /* lock protects the configs list */
74 struct proc_dir_entry
*procdir
;
79 clusterip_config_get(struct clusterip_config
*c
)
81 refcount_inc(&c
->refcount
);
85 static void clusterip_config_rcu_free(struct rcu_head
*head
)
87 kfree(container_of(head
, struct clusterip_config
, rcu
));
91 clusterip_config_put(struct clusterip_config
*c
)
93 if (refcount_dec_and_test(&c
->refcount
))
94 call_rcu_bh(&c
->rcu
, clusterip_config_rcu_free
);
97 /* decrease the count of entries using/referencing this config. If last
98 * entry(rule) is removed, remove the config from lists, but don't free it
99 * yet, since proc-files could still be holding references */
101 clusterip_config_entry_put(struct clusterip_config
*c
)
103 struct net
*net
= dev_net(c
->dev
);
104 struct clusterip_net
*cn
= net_generic(net
, clusterip_net_id
);
107 if (refcount_dec_and_lock(&c
->entries
, &cn
->lock
)) {
108 list_del_rcu(&c
->list
);
109 spin_unlock(&cn
->lock
);
112 dev_mc_del(c
->dev
, c
->clustermac
);
115 /* In case anyone still accesses the file, the open/close
116 * functions are also incrementing the refcount on their own,
117 * so it's safe to remove the entry even if it's in use. */
118 #ifdef CONFIG_PROC_FS
126 static struct clusterip_config
*
127 __clusterip_config_find(struct net
*net
, __be32 clusterip
)
129 struct clusterip_config
*c
;
130 struct clusterip_net
*cn
= net_generic(net
, clusterip_net_id
);
132 list_for_each_entry_rcu(c
, &cn
->configs
, list
) {
133 if (c
->clusterip
== clusterip
)
140 static inline struct clusterip_config
*
141 clusterip_config_find_get(struct net
*net
, __be32 clusterip
, int entry
)
143 struct clusterip_config
*c
;
146 c
= __clusterip_config_find(net
, clusterip
);
148 #ifdef CONFIG_PROC_FS
153 if (unlikely(!refcount_inc_not_zero(&c
->refcount
)))
156 refcount_inc(&c
->entries
);
158 rcu_read_unlock_bh();
164 clusterip_config_init_nodelist(struct clusterip_config
*c
,
165 const struct ipt_clusterip_tgt_info
*i
)
169 for (n
= 0; n
< i
->num_local_nodes
; n
++)
170 set_bit(i
->local_nodes
[n
] - 1, &c
->local_nodes
);
173 static struct clusterip_config
*
174 clusterip_config_init(const struct ipt_clusterip_tgt_info
*i
, __be32 ip
,
175 struct net_device
*dev
)
177 struct net
*net
= dev_net(dev
);
178 struct clusterip_config
*c
;
179 struct clusterip_net
*cn
= net_generic(net
, clusterip_net_id
);
181 c
= kzalloc(sizeof(*c
), GFP_ATOMIC
);
183 return ERR_PTR(-ENOMEM
);
187 memcpy(&c
->clustermac
, &i
->clustermac
, ETH_ALEN
);
188 c
->num_total_nodes
= i
->num_total_nodes
;
189 clusterip_config_init_nodelist(c
, i
);
190 c
->hash_mode
= i
->hash_mode
;
191 c
->hash_initval
= i
->hash_initval
;
192 refcount_set(&c
->refcount
, 1);
193 refcount_set(&c
->entries
, 1);
195 spin_lock_bh(&cn
->lock
);
196 if (__clusterip_config_find(net
, ip
)) {
197 spin_unlock_bh(&cn
->lock
);
200 return ERR_PTR(-EBUSY
);
203 list_add_rcu(&c
->list
, &cn
->configs
);
204 spin_unlock_bh(&cn
->lock
);
206 #ifdef CONFIG_PROC_FS
210 /* create proc dir entry */
211 sprintf(buffer
, "%pI4", &ip
);
212 c
->pde
= proc_create_data(buffer
, S_IWUSR
|S_IRUSR
,
214 &clusterip_proc_fops
, c
);
216 spin_lock_bh(&cn
->lock
);
217 list_del_rcu(&c
->list
);
218 spin_unlock_bh(&cn
->lock
);
221 return ERR_PTR(-ENOMEM
);
229 #ifdef CONFIG_PROC_FS
231 clusterip_add_node(struct clusterip_config
*c
, u_int16_t nodenum
)
235 nodenum
> c
->num_total_nodes
)
238 /* check if we already have this number in our bitfield */
239 if (test_and_set_bit(nodenum
- 1, &c
->local_nodes
))
246 clusterip_del_node(struct clusterip_config
*c
, u_int16_t nodenum
)
249 nodenum
> c
->num_total_nodes
)
252 if (test_and_clear_bit(nodenum
- 1, &c
->local_nodes
))
259 static inline u_int32_t
260 clusterip_hashfn(const struct sk_buff
*skb
,
261 const struct clusterip_config
*config
)
263 const struct iphdr
*iph
= ip_hdr(skb
);
264 unsigned long hashval
;
265 u_int16_t sport
= 0, dport
= 0;
268 poff
= proto_ports_offset(iph
->protocol
);
270 const u_int16_t
*ports
;
273 ports
= skb_header_pointer(skb
, iph
->ihl
* 4 + poff
, 4, _ports
);
279 net_info_ratelimited("unknown protocol %u\n", iph
->protocol
);
282 switch (config
->hash_mode
) {
283 case CLUSTERIP_HASHMODE_SIP
:
284 hashval
= jhash_1word(ntohl(iph
->saddr
),
285 config
->hash_initval
);
287 case CLUSTERIP_HASHMODE_SIP_SPT
:
288 hashval
= jhash_2words(ntohl(iph
->saddr
), sport
,
289 config
->hash_initval
);
291 case CLUSTERIP_HASHMODE_SIP_SPT_DPT
:
292 hashval
= jhash_3words(ntohl(iph
->saddr
), sport
, dport
,
293 config
->hash_initval
);
296 /* to make gcc happy */
298 /* This cannot happen, unless the check function wasn't called
299 * at rule load time */
300 pr_info("unknown mode %u\n", config
->hash_mode
);
305 /* node numbers are 1..n, not 0..n */
306 return reciprocal_scale(hashval
, config
->num_total_nodes
) + 1;
310 clusterip_responsible(const struct clusterip_config
*config
, u_int32_t hash
)
312 return test_bit(hash
- 1, &config
->local_nodes
);
315 /***********************************************************************
317 ***********************************************************************/
320 clusterip_tg(struct sk_buff
*skb
, const struct xt_action_param
*par
)
322 const struct ipt_clusterip_tgt_info
*cipinfo
= par
->targinfo
;
324 enum ip_conntrack_info ctinfo
;
327 /* don't need to clusterip_config_get() here, since refcount
328 * is only decremented by destroy() - and ip_tables guarantees
329 * that the ->target() function isn't called after ->destroy() */
331 ct
= nf_ct_get(skb
, &ctinfo
);
335 /* special case: ICMP error handling. conntrack distinguishes between
336 * error messages (RELATED) and information requests (see below) */
337 if (ip_hdr(skb
)->protocol
== IPPROTO_ICMP
&&
338 (ctinfo
== IP_CT_RELATED
||
339 ctinfo
== IP_CT_RELATED_REPLY
))
342 /* ip_conntrack_icmp guarantees us that we only have ICMP_ECHO,
343 * TIMESTAMP, INFO_REQUEST or ADDRESS type icmp packets from here
344 * on, which all have an ID field [relevant for hashing]. */
346 hash
= clusterip_hashfn(skb
, cipinfo
->config
);
353 case IP_CT_RELATED_REPLY
:
354 /* FIXME: we don't handle expectations at the moment.
355 * They can arrive on a different node than
356 * the master connection (e.g. FTP passive mode) */
357 case IP_CT_ESTABLISHED
:
358 case IP_CT_ESTABLISHED_REPLY
:
360 default: /* Prevent gcc warnings */
365 nf_ct_dump_tuple_ip(&ct
->tuplehash
[IP_CT_DIR_ORIGINAL
].tuple
);
367 pr_debug("hash=%u ct_hash=%u ", hash
, ct
->mark
);
368 if (!clusterip_responsible(cipinfo
->config
, hash
)) {
369 pr_debug("not responsible\n");
372 pr_debug("responsible\n");
374 /* despite being received via linklayer multicast, this is
375 * actually a unicast IP packet. TCP doesn't like PACKET_MULTICAST */
376 skb
->pkt_type
= PACKET_HOST
;
381 static int clusterip_tg_check(const struct xt_tgchk_param
*par
)
383 struct ipt_clusterip_tgt_info
*cipinfo
= par
->targinfo
;
384 const struct ipt_entry
*e
= par
->entryinfo
;
385 struct clusterip_config
*config
;
388 if (par
->nft_compat
) {
389 pr_err("cannot use CLUSTERIP target from nftables compat\n");
393 if (cipinfo
->hash_mode
!= CLUSTERIP_HASHMODE_SIP
&&
394 cipinfo
->hash_mode
!= CLUSTERIP_HASHMODE_SIP_SPT
&&
395 cipinfo
->hash_mode
!= CLUSTERIP_HASHMODE_SIP_SPT_DPT
) {
396 pr_info("unknown mode %u\n", cipinfo
->hash_mode
);
400 if (e
->ip
.dmsk
.s_addr
!= htonl(0xffffffff) ||
401 e
->ip
.dst
.s_addr
== 0) {
402 pr_info("Please specify destination IP\n");
406 /* FIXME: further sanity checks */
408 config
= clusterip_config_find_get(par
->net
, e
->ip
.dst
.s_addr
, 1);
410 if (!(cipinfo
->flags
& CLUSTERIP_FLAG_NEW
)) {
411 pr_info("no config found for %pI4, need 'new'\n",
415 struct net_device
*dev
;
417 if (e
->ip
.iniface
[0] == '\0') {
418 pr_info("Please specify an interface name\n");
422 dev
= dev_get_by_name(par
->net
, e
->ip
.iniface
);
424 pr_info("no such interface %s\n",
429 config
= clusterip_config_init(cipinfo
,
430 e
->ip
.dst
.s_addr
, dev
);
431 if (IS_ERR(config
)) {
433 return PTR_ERR(config
);
435 dev_mc_add(config
->dev
, config
->clustermac
);
438 cipinfo
->config
= config
;
440 ret
= nf_ct_netns_get(par
->net
, par
->family
);
442 pr_info("cannot load conntrack support for proto=%u\n",
445 if (!par
->net
->xt
.clusterip_deprecated_warning
) {
446 pr_info("ipt_CLUSTERIP is deprecated and it will removed soon, "
447 "use xt_cluster instead\n");
448 par
->net
->xt
.clusterip_deprecated_warning
= true;
454 /* drop reference count of cluster config when rule is deleted */
455 static void clusterip_tg_destroy(const struct xt_tgdtor_param
*par
)
457 const struct ipt_clusterip_tgt_info
*cipinfo
= par
->targinfo
;
459 /* if no more entries are referencing the config, remove it
460 * from the list and destroy the proc entry */
461 clusterip_config_entry_put(cipinfo
->config
);
463 clusterip_config_put(cipinfo
->config
);
465 nf_ct_netns_put(par
->net
, par
->family
);
469 struct compat_ipt_clusterip_tgt_info
472 u_int8_t clustermac
[6];
473 u_int16_t num_total_nodes
;
474 u_int16_t num_local_nodes
;
475 u_int16_t local_nodes
[CLUSTERIP_MAX_NODES
];
477 u_int32_t hash_initval
;
478 compat_uptr_t config
;
480 #endif /* CONFIG_COMPAT */
482 static struct xt_target clusterip_tg_reg __read_mostly
= {
484 .family
= NFPROTO_IPV4
,
485 .target
= clusterip_tg
,
486 .checkentry
= clusterip_tg_check
,
487 .destroy
= clusterip_tg_destroy
,
488 .targetsize
= sizeof(struct ipt_clusterip_tgt_info
),
489 .usersize
= offsetof(struct ipt_clusterip_tgt_info
, config
),
491 .compatsize
= sizeof(struct compat_ipt_clusterip_tgt_info
),
492 #endif /* CONFIG_COMPAT */
497 /***********************************************************************
499 ***********************************************************************/
501 /* hardcoded for 48bit ethernet and 32bit ipv4 addresses */
503 u_int8_t src_hw
[ETH_ALEN
];
505 u_int8_t dst_hw
[ETH_ALEN
];
510 static void arp_print(struct arp_payload
*payload
)
512 #define HBUFFERLEN 30
513 char hbuffer
[HBUFFERLEN
];
516 for (k
= 0, j
= 0; k
< HBUFFERLEN
- 3 && j
< ETH_ALEN
; j
++) {
517 hbuffer
[k
++] = hex_asc_hi(payload
->src_hw
[j
]);
518 hbuffer
[k
++] = hex_asc_lo(payload
->src_hw
[j
]);
523 pr_debug("src %pI4@%s, dst %pI4\n",
524 &payload
->src_ip
, hbuffer
, &payload
->dst_ip
);
529 arp_mangle(void *priv
,
531 const struct nf_hook_state
*state
)
533 struct arphdr
*arp
= arp_hdr(skb
);
534 struct arp_payload
*payload
;
535 struct clusterip_config
*c
;
536 struct net
*net
= state
->net
;
538 /* we don't care about non-ethernet and non-ipv4 ARP */
539 if (arp
->ar_hrd
!= htons(ARPHRD_ETHER
) ||
540 arp
->ar_pro
!= htons(ETH_P_IP
) ||
541 arp
->ar_pln
!= 4 || arp
->ar_hln
!= ETH_ALEN
)
544 /* we only want to mangle arp requests and replies */
545 if (arp
->ar_op
!= htons(ARPOP_REPLY
) &&
546 arp
->ar_op
!= htons(ARPOP_REQUEST
))
549 payload
= (void *)(arp
+1);
551 /* if there is no clusterip configuration for the arp reply's
552 * source ip, we don't want to mangle it */
553 c
= clusterip_config_find_get(net
, payload
->src_ip
, 0);
557 /* normally the linux kernel always replies to arp queries of
558 * addresses on different interfacs. However, in the CLUSTERIP case
559 * this wouldn't work, since we didn't subscribe the mcast group on
560 * other interfaces */
561 if (c
->dev
!= state
->out
) {
562 pr_debug("not mangling arp reply on different "
563 "interface: cip'%s'-skb'%s'\n",
564 c
->dev
->name
, state
->out
->name
);
565 clusterip_config_put(c
);
569 /* mangle reply hardware address */
570 memcpy(payload
->src_hw
, c
->clustermac
, arp
->ar_hln
);
573 pr_debug("mangled arp reply: ");
577 clusterip_config_put(c
);
582 static struct nf_hook_ops cip_arp_ops __read_mostly
= {
585 .hooknum
= NF_ARP_OUT
,
589 /***********************************************************************
591 ***********************************************************************/
593 #ifdef CONFIG_PROC_FS
595 struct clusterip_seq_position
{
596 unsigned int pos
; /* position */
597 unsigned int weight
; /* number of bits set == size */
598 unsigned int bit
; /* current bit */
599 unsigned long val
; /* current value */
602 static void *clusterip_seq_start(struct seq_file
*s
, loff_t
*pos
)
604 struct clusterip_config
*c
= s
->private;
606 u_int32_t local_nodes
;
607 struct clusterip_seq_position
*idx
;
609 /* FIXME: possible race */
610 local_nodes
= c
->local_nodes
;
611 weight
= hweight32(local_nodes
);
615 idx
= kmalloc(sizeof(struct clusterip_seq_position
), GFP_KERNEL
);
617 return ERR_PTR(-ENOMEM
);
620 idx
->weight
= weight
;
621 idx
->bit
= ffs(local_nodes
);
622 idx
->val
= local_nodes
;
623 clear_bit(idx
->bit
- 1, &idx
->val
);
628 static void *clusterip_seq_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
630 struct clusterip_seq_position
*idx
= v
;
633 if (*pos
>= idx
->weight
) {
637 idx
->bit
= ffs(idx
->val
);
638 clear_bit(idx
->bit
- 1, &idx
->val
);
642 static void clusterip_seq_stop(struct seq_file
*s
, void *v
)
648 static int clusterip_seq_show(struct seq_file
*s
, void *v
)
650 struct clusterip_seq_position
*idx
= v
;
655 seq_printf(s
, "%u", idx
->bit
);
657 if (idx
->pos
== idx
->weight
- 1)
663 static const struct seq_operations clusterip_seq_ops
= {
664 .start
= clusterip_seq_start
,
665 .next
= clusterip_seq_next
,
666 .stop
= clusterip_seq_stop
,
667 .show
= clusterip_seq_show
,
670 static int clusterip_proc_open(struct inode
*inode
, struct file
*file
)
672 int ret
= seq_open(file
, &clusterip_seq_ops
);
675 struct seq_file
*sf
= file
->private_data
;
676 struct clusterip_config
*c
= PDE_DATA(inode
);
680 clusterip_config_get(c
);
686 static int clusterip_proc_release(struct inode
*inode
, struct file
*file
)
688 struct clusterip_config
*c
= PDE_DATA(inode
);
691 ret
= seq_release(inode
, file
);
694 clusterip_config_put(c
);
699 static ssize_t
clusterip_proc_write(struct file
*file
, const char __user
*input
,
700 size_t size
, loff_t
*ofs
)
702 struct clusterip_config
*c
= PDE_DATA(file_inode(file
));
703 #define PROC_WRITELEN 10
704 char buffer
[PROC_WRITELEN
+1];
705 unsigned long nodenum
;
708 if (size
> PROC_WRITELEN
)
710 if (copy_from_user(buffer
, input
, size
))
714 if (*buffer
== '+') {
715 rc
= kstrtoul(buffer
+1, 10, &nodenum
);
718 if (clusterip_add_node(c
, nodenum
))
720 } else if (*buffer
== '-') {
721 rc
= kstrtoul(buffer
+1, 10, &nodenum
);
724 if (clusterip_del_node(c
, nodenum
))
732 static const struct file_operations clusterip_proc_fops
= {
733 .owner
= THIS_MODULE
,
734 .open
= clusterip_proc_open
,
736 .write
= clusterip_proc_write
,
738 .release
= clusterip_proc_release
,
741 #endif /* CONFIG_PROC_FS */
743 static int clusterip_net_init(struct net
*net
)
745 struct clusterip_net
*cn
= net_generic(net
, clusterip_net_id
);
748 INIT_LIST_HEAD(&cn
->configs
);
750 spin_lock_init(&cn
->lock
);
752 ret
= nf_register_net_hook(net
, &cip_arp_ops
);
756 #ifdef CONFIG_PROC_FS
757 cn
->procdir
= proc_mkdir("ipt_CLUSTERIP", net
->proc_net
);
759 nf_unregister_net_hook(net
, &cip_arp_ops
);
760 pr_err("Unable to proc dir entry\n");
763 #endif /* CONFIG_PROC_FS */
768 static void clusterip_net_exit(struct net
*net
)
770 #ifdef CONFIG_PROC_FS
771 struct clusterip_net
*cn
= net_generic(net
, clusterip_net_id
);
772 proc_remove(cn
->procdir
);
774 nf_unregister_net_hook(net
, &cip_arp_ops
);
777 static struct pernet_operations clusterip_net_ops
= {
778 .init
= clusterip_net_init
,
779 .exit
= clusterip_net_exit
,
780 .id
= &clusterip_net_id
,
781 .size
= sizeof(struct clusterip_net
),
784 static int __init
clusterip_tg_init(void)
788 ret
= register_pernet_subsys(&clusterip_net_ops
);
792 ret
= xt_register_target(&clusterip_tg_reg
);
796 pr_info("ClusterIP Version %s loaded successfully\n",
802 unregister_pernet_subsys(&clusterip_net_ops
);
806 static void __exit
clusterip_tg_exit(void)
808 pr_info("ClusterIP Version %s unloading\n", CLUSTERIP_VERSION
);
810 xt_unregister_target(&clusterip_tg_reg
);
811 unregister_pernet_subsys(&clusterip_net_ops
);
813 /* Wait for completion of call_rcu_bh()'s (clusterip_config_rcu_free) */
817 module_init(clusterip_tg_init
);
818 module_exit(clusterip_tg_exit
);