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 int ifindex; /* device ifindex */
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 */
61 char ifname[IFNAMSIZ]; /* device ifname */
62 struct notifier_block notifier; /* refresh c->ifindex in it */
66 static const struct file_operations clusterip_proc_fops;
69 static unsigned int clusterip_net_id __read_mostly;
71 struct clusterip_net {
72 struct list_head configs;
73 /* lock protects the configs list */
77 struct proc_dir_entry *procdir;
82 clusterip_config_get(struct clusterip_config *c)
84 refcount_inc(&c->refcount);
88 static void clusterip_config_rcu_free(struct rcu_head *head)
90 kfree(container_of(head, struct clusterip_config, rcu));
94 clusterip_config_put(struct clusterip_config *c)
96 if (refcount_dec_and_test(&c->refcount))
97 call_rcu_bh(&c->rcu, clusterip_config_rcu_free);
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 net *net, struct clusterip_config *c)
106 struct clusterip_net *cn = net_generic(net, clusterip_net_id);
109 if (refcount_dec_and_lock(&c->entries, &cn->lock)) {
110 list_del_rcu(&c->list);
111 spin_unlock(&cn->lock);
114 unregister_netdevice_notifier(&c->notifier);
116 /* In case anyone still accesses the file, the open/close
117 * functions are also incrementing the refcount on their own,
118 * so it's safe to remove the entry even if it's in use. */
119 #ifdef CONFIG_PROC_FS
128 static struct clusterip_config *
129 __clusterip_config_find(struct net *net, __be32 clusterip)
131 struct clusterip_config *c;
132 struct clusterip_net *cn = net_generic(net, clusterip_net_id);
134 list_for_each_entry_rcu(c, &cn->configs, list) {
135 if (c->clusterip == clusterip)
142 static inline struct clusterip_config *
143 clusterip_config_find_get(struct net *net, __be32 clusterip, int entry)
145 struct clusterip_config *c;
148 c = __clusterip_config_find(net, clusterip);
150 #ifdef CONFIG_PROC_FS
155 if (unlikely(!refcount_inc_not_zero(&c->refcount)))
158 refcount_inc(&c->entries);
160 rcu_read_unlock_bh();
166 clusterip_config_init_nodelist(struct clusterip_config *c,
167 const struct ipt_clusterip_tgt_info *i)
171 for (n = 0; n < i->num_local_nodes; n++)
172 set_bit(i->local_nodes[n] - 1, &c->local_nodes);
176 clusterip_netdev_event(struct notifier_block *this, unsigned long event,
179 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
180 struct clusterip_config *c;
182 c = container_of(this, struct clusterip_config, notifier);
184 case NETDEV_REGISTER:
185 if (!strcmp(dev->name, c->ifname)) {
186 c->ifindex = dev->ifindex;
187 dev_mc_add(dev, c->clustermac);
190 case NETDEV_UNREGISTER:
191 if (dev->ifindex == c->ifindex) {
192 dev_mc_del(dev, c->clustermac);
196 case NETDEV_CHANGENAME:
197 if (!strcmp(dev->name, c->ifname)) {
198 c->ifindex = dev->ifindex;
199 dev_mc_add(dev, c->clustermac);
200 } else if (dev->ifindex == c->ifindex) {
201 dev_mc_del(dev, c->clustermac);
210 static struct clusterip_config *
211 clusterip_config_init(struct net *net, const struct ipt_clusterip_tgt_info *i,
212 __be32 ip, const char *iniface)
214 struct clusterip_net *cn = net_generic(net, clusterip_net_id);
215 struct clusterip_config *c;
218 c = kzalloc(sizeof(*c), GFP_ATOMIC);
220 return ERR_PTR(-ENOMEM);
222 strcpy(c->ifname, iniface);
225 memcpy(&c->clustermac, &i->clustermac, ETH_ALEN);
226 c->num_total_nodes = i->num_total_nodes;
227 clusterip_config_init_nodelist(c, i);
228 c->hash_mode = i->hash_mode;
229 c->hash_initval = i->hash_initval;
230 refcount_set(&c->refcount, 1);
231 refcount_set(&c->entries, 1);
233 spin_lock_bh(&cn->lock);
234 if (__clusterip_config_find(net, ip)) {
235 spin_unlock_bh(&cn->lock);
238 return ERR_PTR(-EBUSY);
241 list_add_rcu(&c->list, &cn->configs);
242 spin_unlock_bh(&cn->lock);
244 #ifdef CONFIG_PROC_FS
248 /* create proc dir entry */
249 sprintf(buffer, "%pI4", &ip);
250 c->pde = proc_create_data(buffer, S_IWUSR|S_IRUSR,
252 &clusterip_proc_fops, c);
260 c->notifier.notifier_call = clusterip_netdev_event;
261 err = register_netdevice_notifier(&c->notifier);
265 #ifdef CONFIG_PROC_FS
269 spin_lock_bh(&cn->lock);
270 list_del_rcu(&c->list);
271 spin_unlock_bh(&cn->lock);
277 #ifdef CONFIG_PROC_FS
279 clusterip_add_node(struct clusterip_config *c, u_int16_t nodenum)
283 nodenum > c->num_total_nodes)
286 /* check if we already have this number in our bitfield */
287 if (test_and_set_bit(nodenum - 1, &c->local_nodes))
294 clusterip_del_node(struct clusterip_config *c, u_int16_t nodenum)
297 nodenum > c->num_total_nodes)
300 if (test_and_clear_bit(nodenum - 1, &c->local_nodes))
307 static inline u_int32_t
308 clusterip_hashfn(const struct sk_buff *skb,
309 const struct clusterip_config *config)
311 const struct iphdr *iph = ip_hdr(skb);
312 unsigned long hashval;
313 u_int16_t sport = 0, dport = 0;
316 poff = proto_ports_offset(iph->protocol);
318 const u_int16_t *ports;
321 ports = skb_header_pointer(skb, iph->ihl * 4 + poff, 4, _ports);
327 net_info_ratelimited("unknown protocol %u\n", iph->protocol);
330 switch (config->hash_mode) {
331 case CLUSTERIP_HASHMODE_SIP:
332 hashval = jhash_1word(ntohl(iph->saddr),
333 config->hash_initval);
335 case CLUSTERIP_HASHMODE_SIP_SPT:
336 hashval = jhash_2words(ntohl(iph->saddr), sport,
337 config->hash_initval);
339 case CLUSTERIP_HASHMODE_SIP_SPT_DPT:
340 hashval = jhash_3words(ntohl(iph->saddr), sport, dport,
341 config->hash_initval);
344 /* to make gcc happy */
346 /* This cannot happen, unless the check function wasn't called
347 * at rule load time */
348 pr_info("unknown mode %u\n", config->hash_mode);
353 /* node numbers are 1..n, not 0..n */
354 return reciprocal_scale(hashval, config->num_total_nodes) + 1;
358 clusterip_responsible(const struct clusterip_config *config, u_int32_t hash)
360 return test_bit(hash - 1, &config->local_nodes);
363 /***********************************************************************
365 ***********************************************************************/
368 clusterip_tg(struct sk_buff *skb, const struct xt_action_param *par)
370 const struct ipt_clusterip_tgt_info *cipinfo = par->targinfo;
372 enum ip_conntrack_info ctinfo;
375 /* don't need to clusterip_config_get() here, since refcount
376 * is only decremented by destroy() - and ip_tables guarantees
377 * that the ->target() function isn't called after ->destroy() */
379 ct = nf_ct_get(skb, &ctinfo);
383 /* special case: ICMP error handling. conntrack distinguishes between
384 * error messages (RELATED) and information requests (see below) */
385 if (ip_hdr(skb)->protocol == IPPROTO_ICMP &&
386 (ctinfo == IP_CT_RELATED ||
387 ctinfo == IP_CT_RELATED_REPLY))
390 /* ip_conntrack_icmp guarantees us that we only have ICMP_ECHO,
391 * TIMESTAMP, INFO_REQUEST or ADDRESS type icmp packets from here
392 * on, which all have an ID field [relevant for hashing]. */
394 hash = clusterip_hashfn(skb, cipinfo->config);
401 case IP_CT_RELATED_REPLY:
402 /* FIXME: we don't handle expectations at the moment.
403 * They can arrive on a different node than
404 * the master connection (e.g. FTP passive mode) */
405 case IP_CT_ESTABLISHED:
406 case IP_CT_ESTABLISHED_REPLY:
408 default: /* Prevent gcc warnings */
413 nf_ct_dump_tuple_ip(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
415 pr_debug("hash=%u ct_hash=%u ", hash, ct->mark);
416 if (!clusterip_responsible(cipinfo->config, hash)) {
417 pr_debug("not responsible\n");
420 pr_debug("responsible\n");
422 /* despite being received via linklayer multicast, this is
423 * actually a unicast IP packet. TCP doesn't like PACKET_MULTICAST */
424 skb->pkt_type = PACKET_HOST;
429 static int clusterip_tg_check(const struct xt_tgchk_param *par)
431 struct ipt_clusterip_tgt_info *cipinfo = par->targinfo;
432 const struct ipt_entry *e = par->entryinfo;
433 struct clusterip_config *config;
436 if (par->nft_compat) {
437 pr_err("cannot use CLUSTERIP target from nftables compat\n");
441 if (cipinfo->hash_mode != CLUSTERIP_HASHMODE_SIP &&
442 cipinfo->hash_mode != CLUSTERIP_HASHMODE_SIP_SPT &&
443 cipinfo->hash_mode != CLUSTERIP_HASHMODE_SIP_SPT_DPT) {
444 pr_info("unknown mode %u\n", cipinfo->hash_mode);
448 if (e->ip.dmsk.s_addr != htonl(0xffffffff) ||
449 e->ip.dst.s_addr == 0) {
450 pr_info("Please specify destination IP\n");
453 if (cipinfo->num_local_nodes > ARRAY_SIZE(cipinfo->local_nodes)) {
454 pr_info("bad num_local_nodes %u\n", cipinfo->num_local_nodes);
457 for (i = 0; i < cipinfo->num_local_nodes; i++) {
458 if (cipinfo->local_nodes[i] - 1 >=
459 sizeof(config->local_nodes) * 8) {
460 pr_info("bad local_nodes[%d] %u\n",
461 i, cipinfo->local_nodes[i]);
466 config = clusterip_config_find_get(par->net, e->ip.dst.s_addr, 1);
468 if (!(cipinfo->flags & CLUSTERIP_FLAG_NEW)) {
469 pr_info("no config found for %pI4, need 'new'\n",
473 struct net_device *dev;
475 if (e->ip.iniface[0] == '\0') {
476 pr_info("Please specify an interface name\n");
480 dev = dev_get_by_name(par->net, e->ip.iniface);
482 pr_info("no such interface %s\n",
488 config = clusterip_config_init(par->net, cipinfo,
492 return PTR_ERR(config);
495 cipinfo->config = config;
497 ret = nf_ct_netns_get(par->net, par->family);
499 pr_info("cannot load conntrack support for proto=%u\n",
502 if (!par->net->xt.clusterip_deprecated_warning) {
503 pr_info("ipt_CLUSTERIP is deprecated and it will removed soon, "
504 "use xt_cluster instead\n");
505 par->net->xt.clusterip_deprecated_warning = true;
511 /* drop reference count of cluster config when rule is deleted */
512 static void clusterip_tg_destroy(const struct xt_tgdtor_param *par)
514 const struct ipt_clusterip_tgt_info *cipinfo = par->targinfo;
516 /* if no more entries are referencing the config, remove it
517 * from the list and destroy the proc entry */
518 clusterip_config_entry_put(par->net, cipinfo->config);
520 clusterip_config_put(cipinfo->config);
522 nf_ct_netns_put(par->net, par->family);
526 struct compat_ipt_clusterip_tgt_info
529 u_int8_t clustermac[6];
530 u_int16_t num_total_nodes;
531 u_int16_t num_local_nodes;
532 u_int16_t local_nodes[CLUSTERIP_MAX_NODES];
534 u_int32_t hash_initval;
535 compat_uptr_t config;
537 #endif /* CONFIG_COMPAT */
539 static struct xt_target clusterip_tg_reg __read_mostly = {
541 .family = NFPROTO_IPV4,
542 .target = clusterip_tg,
543 .checkentry = clusterip_tg_check,
544 .destroy = clusterip_tg_destroy,
545 .targetsize = sizeof(struct ipt_clusterip_tgt_info),
546 .usersize = offsetof(struct ipt_clusterip_tgt_info, config),
548 .compatsize = sizeof(struct compat_ipt_clusterip_tgt_info),
549 #endif /* CONFIG_COMPAT */
554 /***********************************************************************
556 ***********************************************************************/
558 /* hardcoded for 48bit ethernet and 32bit ipv4 addresses */
560 u_int8_t src_hw[ETH_ALEN];
562 u_int8_t dst_hw[ETH_ALEN];
567 static void arp_print(struct arp_payload *payload)
569 #define HBUFFERLEN 30
570 char hbuffer[HBUFFERLEN];
573 for (k = 0, j = 0; k < HBUFFERLEN - 3 && j < ETH_ALEN; j++) {
574 hbuffer[k++] = hex_asc_hi(payload->src_hw[j]);
575 hbuffer[k++] = hex_asc_lo(payload->src_hw[j]);
580 pr_debug("src %pI4@%s, dst %pI4\n",
581 &payload->src_ip, hbuffer, &payload->dst_ip);
586 arp_mangle(void *priv,
588 const struct nf_hook_state *state)
590 struct arphdr *arp = arp_hdr(skb);
591 struct arp_payload *payload;
592 struct clusterip_config *c;
593 struct net *net = state->net;
595 /* we don't care about non-ethernet and non-ipv4 ARP */
596 if (arp->ar_hrd != htons(ARPHRD_ETHER) ||
597 arp->ar_pro != htons(ETH_P_IP) ||
598 arp->ar_pln != 4 || arp->ar_hln != ETH_ALEN)
601 /* we only want to mangle arp requests and replies */
602 if (arp->ar_op != htons(ARPOP_REPLY) &&
603 arp->ar_op != htons(ARPOP_REQUEST))
606 payload = (void *)(arp+1);
608 /* if there is no clusterip configuration for the arp reply's
609 * source ip, we don't want to mangle it */
610 c = clusterip_config_find_get(net, payload->src_ip, 0);
614 /* normally the linux kernel always replies to arp queries of
615 * addresses on different interfacs. However, in the CLUSTERIP case
616 * this wouldn't work, since we didn't subscribe the mcast group on
617 * other interfaces */
618 if (c->ifindex != state->out->ifindex) {
619 pr_debug("not mangling arp reply on different interface: cip'%d'-skb'%d'\n",
620 c->ifindex, state->out->ifindex);
621 clusterip_config_put(c);
625 /* mangle reply hardware address */
626 memcpy(payload->src_hw, c->clustermac, arp->ar_hln);
629 pr_debug("mangled arp reply: ");
633 clusterip_config_put(c);
638 static const struct nf_hook_ops cip_arp_ops = {
641 .hooknum = NF_ARP_OUT,
645 /***********************************************************************
647 ***********************************************************************/
649 #ifdef CONFIG_PROC_FS
651 struct clusterip_seq_position {
652 unsigned int pos; /* position */
653 unsigned int weight; /* number of bits set == size */
654 unsigned int bit; /* current bit */
655 unsigned long val; /* current value */
658 static void *clusterip_seq_start(struct seq_file *s, loff_t *pos)
660 struct clusterip_config *c = s->private;
662 u_int32_t local_nodes;
663 struct clusterip_seq_position *idx;
665 /* FIXME: possible race */
666 local_nodes = c->local_nodes;
667 weight = hweight32(local_nodes);
671 idx = kmalloc(sizeof(struct clusterip_seq_position), GFP_KERNEL);
673 return ERR_PTR(-ENOMEM);
676 idx->weight = weight;
677 idx->bit = ffs(local_nodes);
678 idx->val = local_nodes;
679 clear_bit(idx->bit - 1, &idx->val);
684 static void *clusterip_seq_next(struct seq_file *s, void *v, loff_t *pos)
686 struct clusterip_seq_position *idx = v;
689 if (*pos >= idx->weight) {
693 idx->bit = ffs(idx->val);
694 clear_bit(idx->bit - 1, &idx->val);
698 static void clusterip_seq_stop(struct seq_file *s, void *v)
704 static int clusterip_seq_show(struct seq_file *s, void *v)
706 struct clusterip_seq_position *idx = v;
711 seq_printf(s, "%u", idx->bit);
713 if (idx->pos == idx->weight - 1)
719 static const struct seq_operations clusterip_seq_ops = {
720 .start = clusterip_seq_start,
721 .next = clusterip_seq_next,
722 .stop = clusterip_seq_stop,
723 .show = clusterip_seq_show,
726 static int clusterip_proc_open(struct inode *inode, struct file *file)
728 int ret = seq_open(file, &clusterip_seq_ops);
731 struct seq_file *sf = file->private_data;
732 struct clusterip_config *c = PDE_DATA(inode);
736 clusterip_config_get(c);
742 static int clusterip_proc_release(struct inode *inode, struct file *file)
744 struct clusterip_config *c = PDE_DATA(inode);
747 ret = seq_release(inode, file);
750 clusterip_config_put(c);
755 static ssize_t clusterip_proc_write(struct file *file, const char __user *input,
756 size_t size, loff_t *ofs)
758 struct clusterip_config *c = PDE_DATA(file_inode(file));
759 #define PROC_WRITELEN 10
760 char buffer[PROC_WRITELEN+1];
761 unsigned long nodenum;
764 if (size > PROC_WRITELEN)
766 if (copy_from_user(buffer, input, size))
770 if (*buffer == '+') {
771 rc = kstrtoul(buffer+1, 10, &nodenum);
774 if (clusterip_add_node(c, nodenum))
776 } else if (*buffer == '-') {
777 rc = kstrtoul(buffer+1, 10, &nodenum);
780 if (clusterip_del_node(c, nodenum))
788 static const struct file_operations clusterip_proc_fops = {
789 .open = clusterip_proc_open,
791 .write = clusterip_proc_write,
793 .release = clusterip_proc_release,
796 #endif /* CONFIG_PROC_FS */
798 static int clusterip_net_init(struct net *net)
800 struct clusterip_net *cn = net_generic(net, clusterip_net_id);
803 INIT_LIST_HEAD(&cn->configs);
805 spin_lock_init(&cn->lock);
807 ret = nf_register_net_hook(net, &cip_arp_ops);
811 #ifdef CONFIG_PROC_FS
812 cn->procdir = proc_mkdir("ipt_CLUSTERIP", net->proc_net);
814 nf_unregister_net_hook(net, &cip_arp_ops);
815 pr_err("Unable to proc dir entry\n");
818 #endif /* CONFIG_PROC_FS */
823 static void clusterip_net_exit(struct net *net)
825 struct clusterip_net *cn = net_generic(net, clusterip_net_id);
826 #ifdef CONFIG_PROC_FS
827 proc_remove(cn->procdir);
830 nf_unregister_net_hook(net, &cip_arp_ops);
831 WARN_ON_ONCE(!list_empty(&cn->configs));
834 static struct pernet_operations clusterip_net_ops = {
835 .init = clusterip_net_init,
836 .exit = clusterip_net_exit,
837 .id = &clusterip_net_id,
838 .size = sizeof(struct clusterip_net),
841 static int __init clusterip_tg_init(void)
845 ret = register_pernet_subsys(&clusterip_net_ops);
849 ret = xt_register_target(&clusterip_tg_reg);
853 pr_info("ClusterIP Version %s loaded successfully\n",
859 unregister_pernet_subsys(&clusterip_net_ops);
863 static void __exit clusterip_tg_exit(void)
865 pr_info("ClusterIP Version %s unloading\n", CLUSTERIP_VERSION);
867 xt_unregister_target(&clusterip_tg_reg);
868 unregister_pernet_subsys(&clusterip_net_ops);
870 /* Wait for completion of call_rcu_bh()'s (clusterip_config_rcu_free) */
874 module_init(clusterip_tg_init);
875 module_exit(clusterip_tg_exit);