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 */
59 struct rcu_head rcu; /* for call_rcu_bh */
60 struct net *net; /* netns for pernet list */
61 char ifname[IFNAMSIZ]; /* device ifname */
65 static const struct file_operations clusterip_proc_fops;
68 struct clusterip_net {
69 struct list_head configs;
70 /* lock protects the configs list */
74 struct proc_dir_entry *procdir;
75 /* mutex protects the config->pde*/
80 static unsigned int clusterip_net_id __read_mostly;
81 static inline struct clusterip_net *clusterip_pernet(struct net *net)
83 return net_generic(net, clusterip_net_id);
87 clusterip_config_get(struct clusterip_config *c)
89 refcount_inc(&c->refcount);
92 static void clusterip_config_rcu_free(struct rcu_head *head)
94 struct clusterip_config *config;
95 struct net_device *dev;
97 config = container_of(head, struct clusterip_config, rcu);
98 dev = dev_get_by_name(config->net, config->ifname);
100 dev_mc_del(dev, config->clustermac);
107 clusterip_config_put(struct clusterip_config *c)
109 if (refcount_dec_and_test(&c->refcount))
110 call_rcu(&c->rcu, clusterip_config_rcu_free);
113 /* decrease the count of entries using/referencing this config. If last
114 * entry(rule) is removed, remove the config from lists, but don't free it
115 * yet, since proc-files could still be holding references */
117 clusterip_config_entry_put(struct clusterip_config *c)
119 struct clusterip_net *cn = clusterip_pernet(c->net);
122 if (refcount_dec_and_lock(&c->entries, &cn->lock)) {
123 list_del_rcu(&c->list);
124 spin_unlock(&cn->lock);
126 /* In case anyone still accesses the file, the open/close
127 * functions are also incrementing the refcount on their own,
128 * so it's safe to remove the entry even if it's in use. */
129 #ifdef CONFIG_PROC_FS
130 mutex_lock(&cn->mutex);
133 mutex_unlock(&cn->mutex);
140 static struct clusterip_config *
141 __clusterip_config_find(struct net *net, __be32 clusterip)
143 struct clusterip_config *c;
144 struct clusterip_net *cn = clusterip_pernet(net);
146 list_for_each_entry_rcu(c, &cn->configs, list) {
147 if (c->clusterip == clusterip)
154 static inline struct clusterip_config *
155 clusterip_config_find_get(struct net *net, __be32 clusterip, int entry)
157 struct clusterip_config *c;
160 c = __clusterip_config_find(net, clusterip);
162 #ifdef CONFIG_PROC_FS
167 if (unlikely(!refcount_inc_not_zero(&c->refcount)))
170 if (unlikely(!refcount_inc_not_zero(&c->entries))) {
171 clusterip_config_put(c);
176 rcu_read_unlock_bh();
182 clusterip_config_init_nodelist(struct clusterip_config *c,
183 const struct ipt_clusterip_tgt_info *i)
187 for (n = 0; n < i->num_local_nodes; n++)
188 set_bit(i->local_nodes[n] - 1, &c->local_nodes);
192 clusterip_netdev_event(struct notifier_block *this, unsigned long event,
195 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
196 struct net *net = dev_net(dev);
197 struct clusterip_net *cn = clusterip_pernet(net);
198 struct clusterip_config *c;
200 spin_lock_bh(&cn->lock);
201 list_for_each_entry_rcu(c, &cn->configs, list) {
203 case NETDEV_REGISTER:
204 if (!strcmp(dev->name, c->ifname)) {
205 c->ifindex = dev->ifindex;
206 dev_mc_add(dev, c->clustermac);
209 case NETDEV_UNREGISTER:
210 if (dev->ifindex == c->ifindex) {
211 dev_mc_del(dev, c->clustermac);
215 case NETDEV_CHANGENAME:
216 if (!strcmp(dev->name, c->ifname)) {
217 c->ifindex = dev->ifindex;
218 dev_mc_add(dev, c->clustermac);
219 } else if (dev->ifindex == c->ifindex) {
220 dev_mc_del(dev, c->clustermac);
226 spin_unlock_bh(&cn->lock);
231 static struct clusterip_config *
232 clusterip_config_init(struct net *net, const struct ipt_clusterip_tgt_info *i,
233 __be32 ip, const char *iniface)
235 struct clusterip_net *cn = clusterip_pernet(net);
236 struct clusterip_config *c;
237 struct net_device *dev;
240 if (iniface[0] == '\0') {
241 pr_info("Please specify an interface name\n");
242 return ERR_PTR(-EINVAL);
245 c = kzalloc(sizeof(*c), GFP_ATOMIC);
247 return ERR_PTR(-ENOMEM);
249 dev = dev_get_by_name(net, iniface);
251 pr_info("no such interface %s\n", iniface);
253 return ERR_PTR(-ENOENT);
255 c->ifindex = dev->ifindex;
256 strcpy(c->ifname, dev->name);
257 memcpy(&c->clustermac, &i->clustermac, ETH_ALEN);
258 dev_mc_add(dev, c->clustermac);
262 c->num_total_nodes = i->num_total_nodes;
263 clusterip_config_init_nodelist(c, i);
264 c->hash_mode = i->hash_mode;
265 c->hash_initval = i->hash_initval;
267 refcount_set(&c->refcount, 1);
269 spin_lock_bh(&cn->lock);
270 if (__clusterip_config_find(net, ip)) {
275 list_add_rcu(&c->list, &cn->configs);
276 spin_unlock_bh(&cn->lock);
278 #ifdef CONFIG_PROC_FS
282 /* create proc dir entry */
283 sprintf(buffer, "%pI4", &ip);
284 mutex_lock(&cn->mutex);
285 c->pde = proc_create_data(buffer, 0600,
287 &clusterip_proc_fops, c);
288 mutex_unlock(&cn->mutex);
296 refcount_set(&c->entries, 1);
299 #ifdef CONFIG_PROC_FS
302 spin_lock_bh(&cn->lock);
303 list_del_rcu(&c->list);
305 spin_unlock_bh(&cn->lock);
306 clusterip_config_put(c);
310 #ifdef CONFIG_PROC_FS
312 clusterip_add_node(struct clusterip_config *c, u_int16_t nodenum)
316 nodenum > c->num_total_nodes)
319 /* check if we already have this number in our bitfield */
320 if (test_and_set_bit(nodenum - 1, &c->local_nodes))
327 clusterip_del_node(struct clusterip_config *c, u_int16_t nodenum)
330 nodenum > c->num_total_nodes)
333 if (test_and_clear_bit(nodenum - 1, &c->local_nodes))
340 static inline u_int32_t
341 clusterip_hashfn(const struct sk_buff *skb,
342 const struct clusterip_config *config)
344 const struct iphdr *iph = ip_hdr(skb);
345 unsigned long hashval;
346 u_int16_t sport = 0, dport = 0;
349 poff = proto_ports_offset(iph->protocol);
351 const u_int16_t *ports;
354 ports = skb_header_pointer(skb, iph->ihl * 4 + poff, 4, _ports);
360 net_info_ratelimited("unknown protocol %u\n", iph->protocol);
363 switch (config->hash_mode) {
364 case CLUSTERIP_HASHMODE_SIP:
365 hashval = jhash_1word(ntohl(iph->saddr),
366 config->hash_initval);
368 case CLUSTERIP_HASHMODE_SIP_SPT:
369 hashval = jhash_2words(ntohl(iph->saddr), sport,
370 config->hash_initval);
372 case CLUSTERIP_HASHMODE_SIP_SPT_DPT:
373 hashval = jhash_3words(ntohl(iph->saddr), sport, dport,
374 config->hash_initval);
377 /* to make gcc happy */
379 /* This cannot happen, unless the check function wasn't called
380 * at rule load time */
381 pr_info("unknown mode %u\n", config->hash_mode);
386 /* node numbers are 1..n, not 0..n */
387 return reciprocal_scale(hashval, config->num_total_nodes) + 1;
391 clusterip_responsible(const struct clusterip_config *config, u_int32_t hash)
393 return test_bit(hash - 1, &config->local_nodes);
396 /***********************************************************************
398 ***********************************************************************/
401 clusterip_tg(struct sk_buff *skb, const struct xt_action_param *par)
403 const struct ipt_clusterip_tgt_info *cipinfo = par->targinfo;
405 enum ip_conntrack_info ctinfo;
408 /* don't need to clusterip_config_get() here, since refcount
409 * is only decremented by destroy() - and ip_tables guarantees
410 * that the ->target() function isn't called after ->destroy() */
412 ct = nf_ct_get(skb, &ctinfo);
416 /* special case: ICMP error handling. conntrack distinguishes between
417 * error messages (RELATED) and information requests (see below) */
418 if (ip_hdr(skb)->protocol == IPPROTO_ICMP &&
419 (ctinfo == IP_CT_RELATED ||
420 ctinfo == IP_CT_RELATED_REPLY))
423 /* ip_conntrack_icmp guarantees us that we only have ICMP_ECHO,
424 * TIMESTAMP, INFO_REQUEST or ADDRESS type icmp packets from here
425 * on, which all have an ID field [relevant for hashing]. */
427 hash = clusterip_hashfn(skb, cipinfo->config);
434 case IP_CT_RELATED_REPLY:
435 /* FIXME: we don't handle expectations at the moment.
436 * They can arrive on a different node than
437 * the master connection (e.g. FTP passive mode) */
438 case IP_CT_ESTABLISHED:
439 case IP_CT_ESTABLISHED_REPLY:
441 default: /* Prevent gcc warnings */
446 nf_ct_dump_tuple_ip(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
448 pr_debug("hash=%u ct_hash=%u ", hash, ct->mark);
449 if (!clusterip_responsible(cipinfo->config, hash)) {
450 pr_debug("not responsible\n");
453 pr_debug("responsible\n");
455 /* despite being received via linklayer multicast, this is
456 * actually a unicast IP packet. TCP doesn't like PACKET_MULTICAST */
457 skb->pkt_type = PACKET_HOST;
462 static int clusterip_tg_check(const struct xt_tgchk_param *par)
464 struct ipt_clusterip_tgt_info *cipinfo = par->targinfo;
465 const struct ipt_entry *e = par->entryinfo;
466 struct clusterip_config *config;
469 if (par->nft_compat) {
470 pr_err("cannot use CLUSTERIP target from nftables compat\n");
474 if (cipinfo->hash_mode != CLUSTERIP_HASHMODE_SIP &&
475 cipinfo->hash_mode != CLUSTERIP_HASHMODE_SIP_SPT &&
476 cipinfo->hash_mode != CLUSTERIP_HASHMODE_SIP_SPT_DPT) {
477 pr_info("unknown mode %u\n", cipinfo->hash_mode);
481 if (e->ip.dmsk.s_addr != htonl(0xffffffff) ||
482 e->ip.dst.s_addr == 0) {
483 pr_info("Please specify destination IP\n");
486 if (cipinfo->num_local_nodes > ARRAY_SIZE(cipinfo->local_nodes)) {
487 pr_info("bad num_local_nodes %u\n", cipinfo->num_local_nodes);
490 for (i = 0; i < cipinfo->num_local_nodes; i++) {
491 if (cipinfo->local_nodes[i] - 1 >=
492 sizeof(config->local_nodes) * 8) {
493 pr_info("bad local_nodes[%d] %u\n",
494 i, cipinfo->local_nodes[i]);
499 config = clusterip_config_find_get(par->net, e->ip.dst.s_addr, 1);
501 if (!(cipinfo->flags & CLUSTERIP_FLAG_NEW)) {
502 pr_info("no config found for %pI4, need 'new'\n",
506 config = clusterip_config_init(par->net, cipinfo,
510 return PTR_ERR(config);
512 } else if (memcmp(&config->clustermac, &cipinfo->clustermac, ETH_ALEN))
515 ret = nf_ct_netns_get(par->net, par->family);
517 pr_info("cannot load conntrack support for proto=%u\n",
519 clusterip_config_entry_put(config);
520 clusterip_config_put(config);
524 if (!par->net->xt.clusterip_deprecated_warning) {
525 pr_info("ipt_CLUSTERIP is deprecated and it will removed soon, "
526 "use xt_cluster instead\n");
527 par->net->xt.clusterip_deprecated_warning = true;
530 cipinfo->config = config;
534 /* drop reference count of cluster config when rule is deleted */
535 static void clusterip_tg_destroy(const struct xt_tgdtor_param *par)
537 const struct ipt_clusterip_tgt_info *cipinfo = par->targinfo;
539 /* if no more entries are referencing the config, remove it
540 * from the list and destroy the proc entry */
541 clusterip_config_entry_put(cipinfo->config);
543 clusterip_config_put(cipinfo->config);
545 nf_ct_netns_put(par->net, par->family);
549 struct compat_ipt_clusterip_tgt_info
552 u_int8_t clustermac[6];
553 u_int16_t num_total_nodes;
554 u_int16_t num_local_nodes;
555 u_int16_t local_nodes[CLUSTERIP_MAX_NODES];
557 u_int32_t hash_initval;
558 compat_uptr_t config;
560 #endif /* CONFIG_COMPAT */
562 static struct xt_target clusterip_tg_reg __read_mostly = {
564 .family = NFPROTO_IPV4,
565 .target = clusterip_tg,
566 .checkentry = clusterip_tg_check,
567 .destroy = clusterip_tg_destroy,
568 .targetsize = sizeof(struct ipt_clusterip_tgt_info),
569 .usersize = offsetof(struct ipt_clusterip_tgt_info, config),
571 .compatsize = sizeof(struct compat_ipt_clusterip_tgt_info),
572 #endif /* CONFIG_COMPAT */
577 /***********************************************************************
579 ***********************************************************************/
581 /* hardcoded for 48bit ethernet and 32bit ipv4 addresses */
583 u_int8_t src_hw[ETH_ALEN];
585 u_int8_t dst_hw[ETH_ALEN];
590 static void arp_print(struct arp_payload *payload)
592 #define HBUFFERLEN 30
593 char hbuffer[HBUFFERLEN];
596 for (k = 0, j = 0; k < HBUFFERLEN - 3 && j < ETH_ALEN; j++) {
597 hbuffer[k++] = hex_asc_hi(payload->src_hw[j]);
598 hbuffer[k++] = hex_asc_lo(payload->src_hw[j]);
603 pr_debug("src %pI4@%s, dst %pI4\n",
604 &payload->src_ip, hbuffer, &payload->dst_ip);
609 arp_mangle(void *priv,
611 const struct nf_hook_state *state)
613 struct arphdr *arp = arp_hdr(skb);
614 struct arp_payload *payload;
615 struct clusterip_config *c;
616 struct net *net = state->net;
618 /* we don't care about non-ethernet and non-ipv4 ARP */
619 if (arp->ar_hrd != htons(ARPHRD_ETHER) ||
620 arp->ar_pro != htons(ETH_P_IP) ||
621 arp->ar_pln != 4 || arp->ar_hln != ETH_ALEN)
624 /* we only want to mangle arp requests and replies */
625 if (arp->ar_op != htons(ARPOP_REPLY) &&
626 arp->ar_op != htons(ARPOP_REQUEST))
629 payload = (void *)(arp+1);
631 /* if there is no clusterip configuration for the arp reply's
632 * source ip, we don't want to mangle it */
633 c = clusterip_config_find_get(net, payload->src_ip, 0);
637 /* normally the linux kernel always replies to arp queries of
638 * addresses on different interfacs. However, in the CLUSTERIP case
639 * this wouldn't work, since we didn't subscribe the mcast group on
640 * other interfaces */
641 if (c->ifindex != state->out->ifindex) {
642 pr_debug("not mangling arp reply on different interface: cip'%d'-skb'%d'\n",
643 c->ifindex, state->out->ifindex);
644 clusterip_config_put(c);
648 /* mangle reply hardware address */
649 memcpy(payload->src_hw, c->clustermac, arp->ar_hln);
652 pr_debug("mangled arp reply: ");
656 clusterip_config_put(c);
661 static const struct nf_hook_ops cip_arp_ops = {
664 .hooknum = NF_ARP_OUT,
668 /***********************************************************************
670 ***********************************************************************/
672 #ifdef CONFIG_PROC_FS
674 struct clusterip_seq_position {
675 unsigned int pos; /* position */
676 unsigned int weight; /* number of bits set == size */
677 unsigned int bit; /* current bit */
678 unsigned long val; /* current value */
681 static void *clusterip_seq_start(struct seq_file *s, loff_t *pos)
683 struct clusterip_config *c = s->private;
685 u_int32_t local_nodes;
686 struct clusterip_seq_position *idx;
688 /* FIXME: possible race */
689 local_nodes = c->local_nodes;
690 weight = hweight32(local_nodes);
694 idx = kmalloc(sizeof(struct clusterip_seq_position), GFP_KERNEL);
696 return ERR_PTR(-ENOMEM);
699 idx->weight = weight;
700 idx->bit = ffs(local_nodes);
701 idx->val = local_nodes;
702 clear_bit(idx->bit - 1, &idx->val);
707 static void *clusterip_seq_next(struct seq_file *s, void *v, loff_t *pos)
709 struct clusterip_seq_position *idx = v;
712 if (*pos >= idx->weight) {
716 idx->bit = ffs(idx->val);
717 clear_bit(idx->bit - 1, &idx->val);
721 static void clusterip_seq_stop(struct seq_file *s, void *v)
727 static int clusterip_seq_show(struct seq_file *s, void *v)
729 struct clusterip_seq_position *idx = v;
734 seq_printf(s, "%u", idx->bit);
736 if (idx->pos == idx->weight - 1)
742 static const struct seq_operations clusterip_seq_ops = {
743 .start = clusterip_seq_start,
744 .next = clusterip_seq_next,
745 .stop = clusterip_seq_stop,
746 .show = clusterip_seq_show,
749 static int clusterip_proc_open(struct inode *inode, struct file *file)
751 int ret = seq_open(file, &clusterip_seq_ops);
754 struct seq_file *sf = file->private_data;
755 struct clusterip_config *c = PDE_DATA(inode);
759 clusterip_config_get(c);
765 static int clusterip_proc_release(struct inode *inode, struct file *file)
767 struct clusterip_config *c = PDE_DATA(inode);
770 ret = seq_release(inode, file);
773 clusterip_config_put(c);
778 static ssize_t clusterip_proc_write(struct file *file, const char __user *input,
779 size_t size, loff_t *ofs)
781 struct clusterip_config *c = PDE_DATA(file_inode(file));
782 #define PROC_WRITELEN 10
783 char buffer[PROC_WRITELEN+1];
784 unsigned long nodenum;
787 if (size > PROC_WRITELEN)
789 if (copy_from_user(buffer, input, size))
793 if (*buffer == '+') {
794 rc = kstrtoul(buffer+1, 10, &nodenum);
797 if (clusterip_add_node(c, nodenum))
799 } else if (*buffer == '-') {
800 rc = kstrtoul(buffer+1, 10, &nodenum);
803 if (clusterip_del_node(c, nodenum))
811 static const struct file_operations clusterip_proc_fops = {
812 .open = clusterip_proc_open,
814 .write = clusterip_proc_write,
816 .release = clusterip_proc_release,
819 #endif /* CONFIG_PROC_FS */
821 static int clusterip_net_init(struct net *net)
823 struct clusterip_net *cn = clusterip_pernet(net);
826 INIT_LIST_HEAD(&cn->configs);
828 spin_lock_init(&cn->lock);
830 ret = nf_register_net_hook(net, &cip_arp_ops);
834 #ifdef CONFIG_PROC_FS
835 cn->procdir = proc_mkdir("ipt_CLUSTERIP", net->proc_net);
837 nf_unregister_net_hook(net, &cip_arp_ops);
838 pr_err("Unable to proc dir entry\n");
841 mutex_init(&cn->mutex);
842 #endif /* CONFIG_PROC_FS */
847 static void clusterip_net_exit(struct net *net)
849 #ifdef CONFIG_PROC_FS
850 struct clusterip_net *cn = clusterip_pernet(net);
852 mutex_lock(&cn->mutex);
853 proc_remove(cn->procdir);
855 mutex_unlock(&cn->mutex);
857 nf_unregister_net_hook(net, &cip_arp_ops);
860 static struct pernet_operations clusterip_net_ops = {
861 .init = clusterip_net_init,
862 .exit = clusterip_net_exit,
863 .id = &clusterip_net_id,
864 .size = sizeof(struct clusterip_net),
867 static struct notifier_block cip_netdev_notifier = {
868 .notifier_call = clusterip_netdev_event
871 static int __init clusterip_tg_init(void)
875 ret = register_pernet_subsys(&clusterip_net_ops);
879 ret = xt_register_target(&clusterip_tg_reg);
883 ret = register_netdevice_notifier(&cip_netdev_notifier);
885 goto unregister_target;
887 pr_info("ClusterIP Version %s loaded successfully\n",
893 xt_unregister_target(&clusterip_tg_reg);
895 unregister_pernet_subsys(&clusterip_net_ops);
899 static void __exit clusterip_tg_exit(void)
901 pr_info("ClusterIP Version %s unloading\n", CLUSTERIP_VERSION);
903 unregister_netdevice_notifier(&cip_netdev_notifier);
904 xt_unregister_target(&clusterip_tg_reg);
905 unregister_pernet_subsys(&clusterip_net_ops);
907 /* Wait for completion of call_rcu()'s (clusterip_config_rcu_free) */
911 module_init(clusterip_tg_init);
912 module_exit(clusterip_tg_exit);