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/netfilter_arp.h>
26 #include <linux/netfilter/x_tables.h>
27 #include <linux/netfilter_ipv4/ip_tables.h>
28 #include <linux/netfilter_ipv4/ipt_CLUSTERIP.h>
29 #include <net/netfilter/nf_conntrack.h>
30 #include <net/net_namespace.h>
31 #include <net/netns/generic.h>
32 #include <net/checksum.h>
35 #define CLUSTERIP_VERSION "0.8"
37 MODULE_LICENSE("GPL");
38 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
39 MODULE_DESCRIPTION("Xtables: CLUSTERIP target");
41 struct clusterip_config {
42 struct list_head list; /* list of all configs */
43 atomic_t refcount; /* reference count */
44 atomic_t entries; /* number of entries/rules
47 __be32 clusterip; /* the IP address */
48 u_int8_t clustermac[ETH_ALEN]; /* the MAC address */
49 struct net_device *dev; /* device */
50 u_int16_t num_total_nodes; /* total number of nodes */
51 unsigned long local_nodes; /* node number array */
54 struct proc_dir_entry *pde; /* proc dir entry */
56 enum clusterip_hashmode hash_mode; /* which hashing mode */
57 u_int32_t hash_initval; /* hash initialization */
62 static const struct file_operations clusterip_proc_fops;
65 static unsigned int clusterip_net_id __read_mostly;
67 struct clusterip_net {
68 struct list_head configs;
69 /* lock protects the configs list */
73 struct proc_dir_entry *procdir;
78 clusterip_config_get(struct clusterip_config *c)
80 atomic_inc(&c->refcount);
84 static void clusterip_config_rcu_free(struct rcu_head *head)
86 kfree(container_of(head, struct clusterip_config, rcu));
90 clusterip_config_put(struct clusterip_config *c)
92 if (atomic_dec_and_test(&c->refcount))
93 call_rcu_bh(&c->rcu, clusterip_config_rcu_free);
96 /* decrease the count of entries using/referencing this config. If last
97 * entry(rule) is removed, remove the config from lists, but don't free it
98 * yet, since proc-files could still be holding references */
100 clusterip_config_entry_put(struct clusterip_config *c)
102 struct net *net = dev_net(c->dev);
103 struct clusterip_net *cn = net_generic(net, clusterip_net_id);
106 if (atomic_dec_and_lock(&c->entries, &cn->lock)) {
107 list_del_rcu(&c->list);
108 spin_unlock(&cn->lock);
111 dev_mc_del(c->dev, c->clustermac);
114 /* In case anyone still accesses the file, the open/close
115 * functions are also incrementing the refcount on their own,
116 * so it's safe to remove the entry even if it's in use. */
117 #ifdef CONFIG_PROC_FS
125 static struct clusterip_config *
126 __clusterip_config_find(struct net *net, __be32 clusterip)
128 struct clusterip_config *c;
129 struct clusterip_net *cn = net_generic(net, clusterip_net_id);
131 list_for_each_entry_rcu(c, &cn->configs, list) {
132 if (c->clusterip == clusterip)
139 static inline struct clusterip_config *
140 clusterip_config_find_get(struct net *net, __be32 clusterip, int entry)
142 struct clusterip_config *c;
145 c = __clusterip_config_find(net, clusterip);
147 #ifdef CONFIG_PROC_FS
152 if (unlikely(!atomic_inc_not_zero(&c->refcount)))
155 atomic_inc(&c->entries);
157 rcu_read_unlock_bh();
163 clusterip_config_init_nodelist(struct clusterip_config *c,
164 const struct ipt_clusterip_tgt_info *i)
168 for (n = 0; n < i->num_local_nodes; n++)
169 set_bit(i->local_nodes[n] - 1, &c->local_nodes);
172 static struct clusterip_config *
173 clusterip_config_init(const struct ipt_clusterip_tgt_info *i, __be32 ip,
174 struct net_device *dev)
176 struct net *net = dev_net(dev);
177 struct clusterip_config *c;
178 struct clusterip_net *cn = net_generic(net, clusterip_net_id);
180 c = kzalloc(sizeof(*c), GFP_ATOMIC);
182 return ERR_PTR(-ENOMEM);
186 memcpy(&c->clustermac, &i->clustermac, ETH_ALEN);
187 c->num_total_nodes = i->num_total_nodes;
188 clusterip_config_init_nodelist(c, i);
189 c->hash_mode = i->hash_mode;
190 c->hash_initval = i->hash_initval;
191 atomic_set(&c->refcount, 1);
192 atomic_set(&c->entries, 1);
194 spin_lock_bh(&cn->lock);
195 if (__clusterip_config_find(net, ip)) {
196 spin_unlock_bh(&cn->lock);
199 return ERR_PTR(-EBUSY);
202 list_add_rcu(&c->list, &cn->configs);
203 spin_unlock_bh(&cn->lock);
205 #ifdef CONFIG_PROC_FS
209 /* create proc dir entry */
210 sprintf(buffer, "%pI4", &ip);
211 c->pde = proc_create_data(buffer, S_IWUSR|S_IRUSR,
213 &clusterip_proc_fops, c);
215 spin_lock_bh(&cn->lock);
216 list_del_rcu(&c->list);
217 spin_unlock_bh(&cn->lock);
220 return ERR_PTR(-ENOMEM);
228 #ifdef CONFIG_PROC_FS
230 clusterip_add_node(struct clusterip_config *c, u_int16_t nodenum)
234 nodenum > c->num_total_nodes)
237 /* check if we already have this number in our bitfield */
238 if (test_and_set_bit(nodenum - 1, &c->local_nodes))
245 clusterip_del_node(struct clusterip_config *c, u_int16_t nodenum)
248 nodenum > c->num_total_nodes)
251 if (test_and_clear_bit(nodenum - 1, &c->local_nodes))
258 static inline u_int32_t
259 clusterip_hashfn(const struct sk_buff *skb,
260 const struct clusterip_config *config)
262 const struct iphdr *iph = ip_hdr(skb);
263 unsigned long hashval;
264 u_int16_t sport = 0, dport = 0;
267 poff = proto_ports_offset(iph->protocol);
269 const u_int16_t *ports;
272 ports = skb_header_pointer(skb, iph->ihl * 4 + poff, 4, _ports);
278 net_info_ratelimited("unknown protocol %u\n", iph->protocol);
281 switch (config->hash_mode) {
282 case CLUSTERIP_HASHMODE_SIP:
283 hashval = jhash_1word(ntohl(iph->saddr),
284 config->hash_initval);
286 case CLUSTERIP_HASHMODE_SIP_SPT:
287 hashval = jhash_2words(ntohl(iph->saddr), sport,
288 config->hash_initval);
290 case CLUSTERIP_HASHMODE_SIP_SPT_DPT:
291 hashval = jhash_3words(ntohl(iph->saddr), sport, dport,
292 config->hash_initval);
295 /* to make gcc happy */
297 /* This cannot happen, unless the check function wasn't called
298 * at rule load time */
299 pr_info("unknown mode %u\n", config->hash_mode);
304 /* node numbers are 1..n, not 0..n */
305 return reciprocal_scale(hashval, config->num_total_nodes) + 1;
309 clusterip_responsible(const struct clusterip_config *config, u_int32_t hash)
311 return test_bit(hash - 1, &config->local_nodes);
314 /***********************************************************************
316 ***********************************************************************/
319 clusterip_tg(struct sk_buff *skb, const struct xt_action_param *par)
321 const struct ipt_clusterip_tgt_info *cipinfo = par->targinfo;
323 enum ip_conntrack_info ctinfo;
326 /* don't need to clusterip_config_get() here, since refcount
327 * is only decremented by destroy() - and ip_tables guarantees
328 * that the ->target() function isn't called after ->destroy() */
330 ct = nf_ct_get(skb, &ctinfo);
334 /* special case: ICMP error handling. conntrack distinguishes between
335 * error messages (RELATED) and information requests (see below) */
336 if (ip_hdr(skb)->protocol == IPPROTO_ICMP &&
337 (ctinfo == IP_CT_RELATED ||
338 ctinfo == IP_CT_RELATED_REPLY))
341 /* ip_conntrack_icmp guarantees us that we only have ICMP_ECHO,
342 * TIMESTAMP, INFO_REQUEST or ADDRESS type icmp packets from here
343 * on, which all have an ID field [relevant for hashing]. */
345 hash = clusterip_hashfn(skb, cipinfo->config);
352 case IP_CT_RELATED_REPLY:
353 /* FIXME: we don't handle expectations at the moment.
354 * They can arrive on a different node than
355 * the master connection (e.g. FTP passive mode) */
356 case IP_CT_ESTABLISHED:
357 case IP_CT_ESTABLISHED_REPLY:
359 default: /* Prevent gcc warnings */
364 nf_ct_dump_tuple_ip(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
366 pr_debug("hash=%u ct_hash=%u ", hash, ct->mark);
367 if (!clusterip_responsible(cipinfo->config, hash)) {
368 pr_debug("not responsible\n");
371 pr_debug("responsible\n");
373 /* despite being received via linklayer multicast, this is
374 * actually a unicast IP packet. TCP doesn't like PACKET_MULTICAST */
375 skb->pkt_type = PACKET_HOST;
380 static int clusterip_tg_check(const struct xt_tgchk_param *par)
382 struct ipt_clusterip_tgt_info *cipinfo = par->targinfo;
383 const struct ipt_entry *e = par->entryinfo;
384 struct clusterip_config *config;
387 if (par->nft_compat) {
388 pr_err("cannot use CLUSTERIP target from nftables compat\n");
392 if (cipinfo->hash_mode != CLUSTERIP_HASHMODE_SIP &&
393 cipinfo->hash_mode != CLUSTERIP_HASHMODE_SIP_SPT &&
394 cipinfo->hash_mode != CLUSTERIP_HASHMODE_SIP_SPT_DPT) {
395 pr_info("unknown mode %u\n", cipinfo->hash_mode);
399 if (e->ip.dmsk.s_addr != htonl(0xffffffff) ||
400 e->ip.dst.s_addr == 0) {
401 pr_info("Please specify destination IP\n");
405 /* FIXME: further sanity checks */
407 config = clusterip_config_find_get(par->net, e->ip.dst.s_addr, 1);
409 if (!(cipinfo->flags & CLUSTERIP_FLAG_NEW)) {
410 pr_info("no config found for %pI4, need 'new'\n",
414 struct net_device *dev;
416 if (e->ip.iniface[0] == '\0') {
417 pr_info("Please specify an interface name\n");
421 dev = dev_get_by_name(par->net, e->ip.iniface);
423 pr_info("no such interface %s\n",
428 config = clusterip_config_init(cipinfo,
429 e->ip.dst.s_addr, dev);
430 if (IS_ERR(config)) {
432 return PTR_ERR(config);
434 dev_mc_add(config->dev, config->clustermac);
437 cipinfo->config = config;
439 ret = nf_ct_netns_get(par->net, par->family);
441 pr_info("cannot load conntrack support for proto=%u\n",
444 if (!par->net->xt.clusterip_deprecated_warning) {
445 pr_info("ipt_CLUSTERIP is deprecated and it will removed soon, "
446 "use xt_cluster instead\n");
447 par->net->xt.clusterip_deprecated_warning = true;
453 /* drop reference count of cluster config when rule is deleted */
454 static void clusterip_tg_destroy(const struct xt_tgdtor_param *par)
456 const struct ipt_clusterip_tgt_info *cipinfo = par->targinfo;
458 /* if no more entries are referencing the config, remove it
459 * from the list and destroy the proc entry */
460 clusterip_config_entry_put(cipinfo->config);
462 clusterip_config_put(cipinfo->config);
464 nf_ct_netns_get(par->net, par->family);
468 struct compat_ipt_clusterip_tgt_info
471 u_int8_t clustermac[6];
472 u_int16_t num_total_nodes;
473 u_int16_t num_local_nodes;
474 u_int16_t local_nodes[CLUSTERIP_MAX_NODES];
476 u_int32_t hash_initval;
477 compat_uptr_t config;
479 #endif /* CONFIG_COMPAT */
481 static struct xt_target clusterip_tg_reg __read_mostly = {
483 .family = NFPROTO_IPV4,
484 .target = clusterip_tg,
485 .checkentry = clusterip_tg_check,
486 .destroy = clusterip_tg_destroy,
487 .targetsize = sizeof(struct ipt_clusterip_tgt_info),
489 .compatsize = sizeof(struct compat_ipt_clusterip_tgt_info),
490 #endif /* CONFIG_COMPAT */
495 /***********************************************************************
497 ***********************************************************************/
499 /* hardcoded for 48bit ethernet and 32bit ipv4 addresses */
501 u_int8_t src_hw[ETH_ALEN];
503 u_int8_t dst_hw[ETH_ALEN];
508 static void arp_print(struct arp_payload *payload)
510 #define HBUFFERLEN 30
511 char hbuffer[HBUFFERLEN];
514 for (k = 0, j = 0; k < HBUFFERLEN - 3 && j < ETH_ALEN; j++) {
515 hbuffer[k++] = hex_asc_hi(payload->src_hw[j]);
516 hbuffer[k++] = hex_asc_lo(payload->src_hw[j]);
521 pr_debug("src %pI4@%s, dst %pI4\n",
522 &payload->src_ip, hbuffer, &payload->dst_ip);
527 arp_mangle(void *priv,
529 const struct nf_hook_state *state)
531 struct arphdr *arp = arp_hdr(skb);
532 struct arp_payload *payload;
533 struct clusterip_config *c;
534 struct net *net = state->net;
536 /* we don't care about non-ethernet and non-ipv4 ARP */
537 if (arp->ar_hrd != htons(ARPHRD_ETHER) ||
538 arp->ar_pro != htons(ETH_P_IP) ||
539 arp->ar_pln != 4 || arp->ar_hln != ETH_ALEN)
542 /* we only want to mangle arp requests and replies */
543 if (arp->ar_op != htons(ARPOP_REPLY) &&
544 arp->ar_op != htons(ARPOP_REQUEST))
547 payload = (void *)(arp+1);
549 /* if there is no clusterip configuration for the arp reply's
550 * source ip, we don't want to mangle it */
551 c = clusterip_config_find_get(net, payload->src_ip, 0);
555 /* normally the linux kernel always replies to arp queries of
556 * addresses on different interfacs. However, in the CLUSTERIP case
557 * this wouldn't work, since we didn't subscribe the mcast group on
558 * other interfaces */
559 if (c->dev != state->out) {
560 pr_debug("not mangling arp reply on different "
561 "interface: cip'%s'-skb'%s'\n",
562 c->dev->name, state->out->name);
563 clusterip_config_put(c);
567 /* mangle reply hardware address */
568 memcpy(payload->src_hw, c->clustermac, arp->ar_hln);
571 pr_debug("mangled arp reply: ");
575 clusterip_config_put(c);
580 static struct nf_hook_ops cip_arp_ops __read_mostly = {
583 .hooknum = NF_ARP_OUT,
587 /***********************************************************************
589 ***********************************************************************/
591 #ifdef CONFIG_PROC_FS
593 struct clusterip_seq_position {
594 unsigned int pos; /* position */
595 unsigned int weight; /* number of bits set == size */
596 unsigned int bit; /* current bit */
597 unsigned long val; /* current value */
600 static void *clusterip_seq_start(struct seq_file *s, loff_t *pos)
602 struct clusterip_config *c = s->private;
604 u_int32_t local_nodes;
605 struct clusterip_seq_position *idx;
607 /* FIXME: possible race */
608 local_nodes = c->local_nodes;
609 weight = hweight32(local_nodes);
613 idx = kmalloc(sizeof(struct clusterip_seq_position), GFP_KERNEL);
615 return ERR_PTR(-ENOMEM);
618 idx->weight = weight;
619 idx->bit = ffs(local_nodes);
620 idx->val = local_nodes;
621 clear_bit(idx->bit - 1, &idx->val);
626 static void *clusterip_seq_next(struct seq_file *s, void *v, loff_t *pos)
628 struct clusterip_seq_position *idx = v;
631 if (*pos >= idx->weight) {
635 idx->bit = ffs(idx->val);
636 clear_bit(idx->bit - 1, &idx->val);
640 static void clusterip_seq_stop(struct seq_file *s, void *v)
646 static int clusterip_seq_show(struct seq_file *s, void *v)
648 struct clusterip_seq_position *idx = v;
653 seq_printf(s, "%u", idx->bit);
655 if (idx->pos == idx->weight - 1)
661 static const struct seq_operations clusterip_seq_ops = {
662 .start = clusterip_seq_start,
663 .next = clusterip_seq_next,
664 .stop = clusterip_seq_stop,
665 .show = clusterip_seq_show,
668 static int clusterip_proc_open(struct inode *inode, struct file *file)
670 int ret = seq_open(file, &clusterip_seq_ops);
673 struct seq_file *sf = file->private_data;
674 struct clusterip_config *c = PDE_DATA(inode);
678 clusterip_config_get(c);
684 static int clusterip_proc_release(struct inode *inode, struct file *file)
686 struct clusterip_config *c = PDE_DATA(inode);
689 ret = seq_release(inode, file);
692 clusterip_config_put(c);
697 static ssize_t clusterip_proc_write(struct file *file, const char __user *input,
698 size_t size, loff_t *ofs)
700 struct clusterip_config *c = PDE_DATA(file_inode(file));
701 #define PROC_WRITELEN 10
702 char buffer[PROC_WRITELEN+1];
703 unsigned long nodenum;
706 if (size > PROC_WRITELEN)
708 if (copy_from_user(buffer, input, size))
712 if (*buffer == '+') {
713 rc = kstrtoul(buffer+1, 10, &nodenum);
716 if (clusterip_add_node(c, nodenum))
718 } else if (*buffer == '-') {
719 rc = kstrtoul(buffer+1, 10, &nodenum);
722 if (clusterip_del_node(c, nodenum))
730 static const struct file_operations clusterip_proc_fops = {
731 .owner = THIS_MODULE,
732 .open = clusterip_proc_open,
734 .write = clusterip_proc_write,
736 .release = clusterip_proc_release,
739 #endif /* CONFIG_PROC_FS */
741 static int clusterip_net_init(struct net *net)
743 struct clusterip_net *cn = net_generic(net, clusterip_net_id);
745 INIT_LIST_HEAD(&cn->configs);
747 spin_lock_init(&cn->lock);
749 #ifdef CONFIG_PROC_FS
750 cn->procdir = proc_mkdir("ipt_CLUSTERIP", net->proc_net);
752 pr_err("Unable to proc dir entry\n");
755 #endif /* CONFIG_PROC_FS */
760 static void clusterip_net_exit(struct net *net)
762 #ifdef CONFIG_PROC_FS
763 struct clusterip_net *cn = net_generic(net, clusterip_net_id);
764 proc_remove(cn->procdir);
768 static struct pernet_operations clusterip_net_ops = {
769 .init = clusterip_net_init,
770 .exit = clusterip_net_exit,
771 .id = &clusterip_net_id,
772 .size = sizeof(struct clusterip_net),
775 static int __init clusterip_tg_init(void)
779 ret = register_pernet_subsys(&clusterip_net_ops);
783 ret = xt_register_target(&clusterip_tg_reg);
787 ret = nf_register_hook(&cip_arp_ops);
791 pr_info("ClusterIP Version %s loaded successfully\n",
797 xt_unregister_target(&clusterip_tg_reg);
799 unregister_pernet_subsys(&clusterip_net_ops);
803 static void __exit clusterip_tg_exit(void)
805 pr_info("ClusterIP Version %s unloading\n", CLUSTERIP_VERSION);
807 nf_unregister_hook(&cip_arp_ops);
808 xt_unregister_target(&clusterip_tg_reg);
809 unregister_pernet_subsys(&clusterip_net_ops);
811 /* Wait for completion of call_rcu_bh()'s (clusterip_config_rcu_free) */
815 module_init(clusterip_tg_init);
816 module_exit(clusterip_tg_exit);