4c8cfd352687ba56af5063d2ad9d76c0724ed33a
[linux-2.6-microblaze.git] / net / ipv4 / netfilter / ipt_CLUSTERIP.c
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>
4  *
5  * Development of this code funded by SuSE Linux AG, http://www.suse.com/
6  *
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.
10  *
11  */
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>
19 #include <linux/ip.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>
34 #include <net/ip.h>
35
36 #define CLUSTERIP_VERSION "0.8"
37
38 MODULE_LICENSE("GPL");
39 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
40 MODULE_DESCRIPTION("Xtables: CLUSTERIP target");
41
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
46                                                  * referencing us */
47
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 */
53
54 #ifdef CONFIG_PROC_FS
55         struct proc_dir_entry *pde;             /* proc dir entry */
56 #endif
57         enum clusterip_hashmode hash_mode;      /* which hashing mode */
58         u_int32_t hash_initval;                 /* hash initialization */
59         struct rcu_head rcu;
60
61         char ifname[IFNAMSIZ];                  /* device ifname */
62         struct notifier_block notifier;         /* refresh c->ifindex in it */
63 };
64
65 #ifdef CONFIG_PROC_FS
66 static const struct file_operations clusterip_proc_fops;
67 #endif
68
69 static unsigned int clusterip_net_id __read_mostly;
70
71 struct clusterip_net {
72         struct list_head configs;
73         /* lock protects the configs list */
74         spinlock_t lock;
75
76 #ifdef CONFIG_PROC_FS
77         struct proc_dir_entry *procdir;
78 #endif
79 };
80
81 static inline void
82 clusterip_config_get(struct clusterip_config *c)
83 {
84         refcount_inc(&c->refcount);
85 }
86
87
88 static void clusterip_config_rcu_free(struct rcu_head *head)
89 {
90         kfree(container_of(head, struct clusterip_config, rcu));
91 }
92
93 static inline void
94 clusterip_config_put(struct clusterip_config *c)
95 {
96         if (refcount_dec_and_test(&c->refcount))
97                 call_rcu_bh(&c->rcu, clusterip_config_rcu_free);
98 }
99
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 */
103 static inline void
104 clusterip_config_entry_put(struct net *net, struct clusterip_config *c)
105 {
106         struct clusterip_net *cn = net_generic(net, clusterip_net_id);
107
108         local_bh_disable();
109         if (refcount_dec_and_lock(&c->entries, &cn->lock)) {
110                 /* In case anyone still accesses the file, the open/close
111                  * functions are also incrementing the refcount on their own,
112                  * so it's safe to remove the entry even if it's in use. */
113 #ifdef CONFIG_PROC_FS
114                 if (cn->procdir)
115                         proc_remove(c->pde);
116 #endif
117                 list_del_rcu(&c->list);
118                 spin_unlock(&cn->lock);
119                 local_bh_enable();
120
121                 unregister_netdevice_notifier(&c->notifier);
122
123                 return;
124         }
125         local_bh_enable();
126 }
127
128 static struct clusterip_config *
129 __clusterip_config_find(struct net *net, __be32 clusterip)
130 {
131         struct clusterip_config *c;
132         struct clusterip_net *cn = net_generic(net, clusterip_net_id);
133
134         list_for_each_entry_rcu(c, &cn->configs, list) {
135                 if (c->clusterip == clusterip)
136                         return c;
137         }
138
139         return NULL;
140 }
141
142 static inline struct clusterip_config *
143 clusterip_config_find_get(struct net *net, __be32 clusterip, int entry)
144 {
145         struct clusterip_config *c;
146
147         rcu_read_lock_bh();
148         c = __clusterip_config_find(net, clusterip);
149         if (c) {
150 #ifdef CONFIG_PROC_FS
151                 if (!c->pde)
152                         c = NULL;
153                 else
154 #endif
155                 if (unlikely(!refcount_inc_not_zero(&c->refcount)))
156                         c = NULL;
157                 else if (entry) {
158                         if (unlikely(!refcount_inc_not_zero(&c->entries))) {
159                                 clusterip_config_put(c);
160                                 c = NULL;
161                         }
162                 }
163         }
164         rcu_read_unlock_bh();
165
166         return c;
167 }
168
169 static void
170 clusterip_config_init_nodelist(struct clusterip_config *c,
171                                const struct ipt_clusterip_tgt_info *i)
172 {
173         int n;
174
175         for (n = 0; n < i->num_local_nodes; n++)
176                 set_bit(i->local_nodes[n] - 1, &c->local_nodes);
177 }
178
179 static int
180 clusterip_netdev_event(struct notifier_block *this, unsigned long event,
181                        void *ptr)
182 {
183         struct net_device *dev = netdev_notifier_info_to_dev(ptr);
184         struct clusterip_config *c;
185
186         c = container_of(this, struct clusterip_config, notifier);
187         switch (event) {
188         case NETDEV_REGISTER:
189                 if (!strcmp(dev->name, c->ifname)) {
190                         c->ifindex = dev->ifindex;
191                         dev_mc_add(dev, c->clustermac);
192                 }
193                 break;
194         case NETDEV_UNREGISTER:
195                 if (dev->ifindex == c->ifindex) {
196                         dev_mc_del(dev, c->clustermac);
197                         c->ifindex = -1;
198                 }
199                 break;
200         case NETDEV_CHANGENAME:
201                 if (!strcmp(dev->name, c->ifname)) {
202                         c->ifindex = dev->ifindex;
203                         dev_mc_add(dev, c->clustermac);
204                 } else if (dev->ifindex == c->ifindex) {
205                         dev_mc_del(dev, c->clustermac);
206                         c->ifindex = -1;
207                 }
208                 break;
209         }
210
211         return NOTIFY_DONE;
212 }
213
214 static struct clusterip_config *
215 clusterip_config_init(struct net *net, const struct ipt_clusterip_tgt_info *i,
216                       __be32 ip, const char *iniface)
217 {
218         struct clusterip_net *cn = net_generic(net, clusterip_net_id);
219         struct clusterip_config *c;
220         int err;
221
222         c = kzalloc(sizeof(*c), GFP_ATOMIC);
223         if (!c)
224                 return ERR_PTR(-ENOMEM);
225
226         strcpy(c->ifname, iniface);
227         c->ifindex = -1;
228         c->clusterip = ip;
229         memcpy(&c->clustermac, &i->clustermac, ETH_ALEN);
230         c->num_total_nodes = i->num_total_nodes;
231         clusterip_config_init_nodelist(c, i);
232         c->hash_mode = i->hash_mode;
233         c->hash_initval = i->hash_initval;
234         refcount_set(&c->refcount, 1);
235         refcount_set(&c->entries, 1);
236
237         spin_lock_bh(&cn->lock);
238         if (__clusterip_config_find(net, ip)) {
239                 spin_unlock_bh(&cn->lock);
240                 kfree(c);
241
242                 return ERR_PTR(-EBUSY);
243         }
244
245         list_add_rcu(&c->list, &cn->configs);
246         spin_unlock_bh(&cn->lock);
247
248 #ifdef CONFIG_PROC_FS
249         {
250                 char buffer[16];
251
252                 /* create proc dir entry */
253                 sprintf(buffer, "%pI4", &ip);
254                 c->pde = proc_create_data(buffer, S_IWUSR|S_IRUSR,
255                                           cn->procdir,
256                                           &clusterip_proc_fops, c);
257                 if (!c->pde) {
258                         err = -ENOMEM;
259                         goto err;
260                 }
261         }
262 #endif
263
264         c->notifier.notifier_call = clusterip_netdev_event;
265         err = register_netdevice_notifier(&c->notifier);
266         if (!err)
267                 return c;
268
269 #ifdef CONFIG_PROC_FS
270         proc_remove(c->pde);
271 err:
272 #endif
273         spin_lock_bh(&cn->lock);
274         list_del_rcu(&c->list);
275         spin_unlock_bh(&cn->lock);
276         kfree(c);
277
278         return ERR_PTR(err);
279 }
280
281 #ifdef CONFIG_PROC_FS
282 static int
283 clusterip_add_node(struct clusterip_config *c, u_int16_t nodenum)
284 {
285
286         if (nodenum == 0 ||
287             nodenum > c->num_total_nodes)
288                 return 1;
289
290         /* check if we already have this number in our bitfield */
291         if (test_and_set_bit(nodenum - 1, &c->local_nodes))
292                 return 1;
293
294         return 0;
295 }
296
297 static bool
298 clusterip_del_node(struct clusterip_config *c, u_int16_t nodenum)
299 {
300         if (nodenum == 0 ||
301             nodenum > c->num_total_nodes)
302                 return true;
303
304         if (test_and_clear_bit(nodenum - 1, &c->local_nodes))
305                 return false;
306
307         return true;
308 }
309 #endif
310
311 static inline u_int32_t
312 clusterip_hashfn(const struct sk_buff *skb,
313                  const struct clusterip_config *config)
314 {
315         const struct iphdr *iph = ip_hdr(skb);
316         unsigned long hashval;
317         u_int16_t sport = 0, dport = 0;
318         int poff;
319
320         poff = proto_ports_offset(iph->protocol);
321         if (poff >= 0) {
322                 const u_int16_t *ports;
323                 u16 _ports[2];
324
325                 ports = skb_header_pointer(skb, iph->ihl * 4 + poff, 4, _ports);
326                 if (ports) {
327                         sport = ports[0];
328                         dport = ports[1];
329                 }
330         } else {
331                 net_info_ratelimited("unknown protocol %u\n", iph->protocol);
332         }
333
334         switch (config->hash_mode) {
335         case CLUSTERIP_HASHMODE_SIP:
336                 hashval = jhash_1word(ntohl(iph->saddr),
337                                       config->hash_initval);
338                 break;
339         case CLUSTERIP_HASHMODE_SIP_SPT:
340                 hashval = jhash_2words(ntohl(iph->saddr), sport,
341                                        config->hash_initval);
342                 break;
343         case CLUSTERIP_HASHMODE_SIP_SPT_DPT:
344                 hashval = jhash_3words(ntohl(iph->saddr), sport, dport,
345                                        config->hash_initval);
346                 break;
347         default:
348                 /* to make gcc happy */
349                 hashval = 0;
350                 /* This cannot happen, unless the check function wasn't called
351                  * at rule load time */
352                 pr_info("unknown mode %u\n", config->hash_mode);
353                 BUG();
354                 break;
355         }
356
357         /* node numbers are 1..n, not 0..n */
358         return reciprocal_scale(hashval, config->num_total_nodes) + 1;
359 }
360
361 static inline int
362 clusterip_responsible(const struct clusterip_config *config, u_int32_t hash)
363 {
364         return test_bit(hash - 1, &config->local_nodes);
365 }
366
367 /***********************************************************************
368  * IPTABLES TARGET
369  ***********************************************************************/
370
371 static unsigned int
372 clusterip_tg(struct sk_buff *skb, const struct xt_action_param *par)
373 {
374         const struct ipt_clusterip_tgt_info *cipinfo = par->targinfo;
375         struct nf_conn *ct;
376         enum ip_conntrack_info ctinfo;
377         u_int32_t hash;
378
379         /* don't need to clusterip_config_get() here, since refcount
380          * is only decremented by destroy() - and ip_tables guarantees
381          * that the ->target() function isn't called after ->destroy() */
382
383         ct = nf_ct_get(skb, &ctinfo);
384         if (ct == NULL)
385                 return NF_DROP;
386
387         /* special case: ICMP error handling. conntrack distinguishes between
388          * error messages (RELATED) and information requests (see below) */
389         if (ip_hdr(skb)->protocol == IPPROTO_ICMP &&
390             (ctinfo == IP_CT_RELATED ||
391              ctinfo == IP_CT_RELATED_REPLY))
392                 return XT_CONTINUE;
393
394         /* ip_conntrack_icmp guarantees us that we only have ICMP_ECHO,
395          * TIMESTAMP, INFO_REQUEST or ADDRESS type icmp packets from here
396          * on, which all have an ID field [relevant for hashing]. */
397
398         hash = clusterip_hashfn(skb, cipinfo->config);
399
400         switch (ctinfo) {
401         case IP_CT_NEW:
402                 ct->mark = hash;
403                 break;
404         case IP_CT_RELATED:
405         case IP_CT_RELATED_REPLY:
406                 /* FIXME: we don't handle expectations at the moment.
407                  * They can arrive on a different node than
408                  * the master connection (e.g. FTP passive mode) */
409         case IP_CT_ESTABLISHED:
410         case IP_CT_ESTABLISHED_REPLY:
411                 break;
412         default:                        /* Prevent gcc warnings */
413                 break;
414         }
415
416 #ifdef DEBUG
417         nf_ct_dump_tuple_ip(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
418 #endif
419         pr_debug("hash=%u ct_hash=%u ", hash, ct->mark);
420         if (!clusterip_responsible(cipinfo->config, hash)) {
421                 pr_debug("not responsible\n");
422                 return NF_DROP;
423         }
424         pr_debug("responsible\n");
425
426         /* despite being received via linklayer multicast, this is
427          * actually a unicast IP packet. TCP doesn't like PACKET_MULTICAST */
428         skb->pkt_type = PACKET_HOST;
429
430         return XT_CONTINUE;
431 }
432
433 static int clusterip_tg_check(const struct xt_tgchk_param *par)
434 {
435         struct ipt_clusterip_tgt_info *cipinfo = par->targinfo;
436         const struct ipt_entry *e = par->entryinfo;
437         struct clusterip_config *config;
438         int ret, i;
439
440         if (par->nft_compat) {
441                 pr_err("cannot use CLUSTERIP target from nftables compat\n");
442                 return -EOPNOTSUPP;
443         }
444
445         if (cipinfo->hash_mode != CLUSTERIP_HASHMODE_SIP &&
446             cipinfo->hash_mode != CLUSTERIP_HASHMODE_SIP_SPT &&
447             cipinfo->hash_mode != CLUSTERIP_HASHMODE_SIP_SPT_DPT) {
448                 pr_info("unknown mode %u\n", cipinfo->hash_mode);
449                 return -EINVAL;
450
451         }
452         if (e->ip.dmsk.s_addr != htonl(0xffffffff) ||
453             e->ip.dst.s_addr == 0) {
454                 pr_info("Please specify destination IP\n");
455                 return -EINVAL;
456         }
457         if (cipinfo->num_local_nodes > ARRAY_SIZE(cipinfo->local_nodes)) {
458                 pr_info("bad num_local_nodes %u\n", cipinfo->num_local_nodes);
459                 return -EINVAL;
460         }
461         for (i = 0; i < cipinfo->num_local_nodes; i++) {
462                 if (cipinfo->local_nodes[i] - 1 >=
463                     sizeof(config->local_nodes) * 8) {
464                         pr_info("bad local_nodes[%d] %u\n",
465                                 i, cipinfo->local_nodes[i]);
466                         return -EINVAL;
467                 }
468         }
469
470         config = clusterip_config_find_get(par->net, e->ip.dst.s_addr, 1);
471         if (!config) {
472                 if (!(cipinfo->flags & CLUSTERIP_FLAG_NEW)) {
473                         pr_info("no config found for %pI4, need 'new'\n",
474                                 &e->ip.dst.s_addr);
475                         return -EINVAL;
476                 } else {
477                         struct net_device *dev;
478
479                         if (e->ip.iniface[0] == '\0') {
480                                 pr_info("Please specify an interface name\n");
481                                 return -EINVAL;
482                         }
483
484                         dev = dev_get_by_name(par->net, e->ip.iniface);
485                         if (!dev) {
486                                 pr_info("no such interface %s\n",
487                                         e->ip.iniface);
488                                 return -ENOENT;
489                         }
490                         dev_put(dev);
491
492                         config = clusterip_config_init(par->net, cipinfo,
493                                                        e->ip.dst.s_addr,
494                                                        e->ip.iniface);
495                         if (IS_ERR(config))
496                                 return PTR_ERR(config);
497                 }
498         }
499
500         ret = nf_ct_netns_get(par->net, par->family);
501         if (ret < 0) {
502                 pr_info("cannot load conntrack support for proto=%u\n",
503                         par->family);
504                 clusterip_config_entry_put(par->net, config);
505                 clusterip_config_put(config);
506                 return ret;
507         }
508
509         if (!par->net->xt.clusterip_deprecated_warning) {
510                 pr_info("ipt_CLUSTERIP is deprecated and it will removed soon, "
511                         "use xt_cluster instead\n");
512                 par->net->xt.clusterip_deprecated_warning = true;
513         }
514
515         cipinfo->config = config;
516         return ret;
517 }
518
519 /* drop reference count of cluster config when rule is deleted */
520 static void clusterip_tg_destroy(const struct xt_tgdtor_param *par)
521 {
522         const struct ipt_clusterip_tgt_info *cipinfo = par->targinfo;
523
524         /* if no more entries are referencing the config, remove it
525          * from the list and destroy the proc entry */
526         clusterip_config_entry_put(par->net, cipinfo->config);
527
528         clusterip_config_put(cipinfo->config);
529
530         nf_ct_netns_put(par->net, par->family);
531 }
532
533 #ifdef CONFIG_COMPAT
534 struct compat_ipt_clusterip_tgt_info
535 {
536         u_int32_t       flags;
537         u_int8_t        clustermac[6];
538         u_int16_t       num_total_nodes;
539         u_int16_t       num_local_nodes;
540         u_int16_t       local_nodes[CLUSTERIP_MAX_NODES];
541         u_int32_t       hash_mode;
542         u_int32_t       hash_initval;
543         compat_uptr_t   config;
544 };
545 #endif /* CONFIG_COMPAT */
546
547 static struct xt_target clusterip_tg_reg __read_mostly = {
548         .name           = "CLUSTERIP",
549         .family         = NFPROTO_IPV4,
550         .target         = clusterip_tg,
551         .checkentry     = clusterip_tg_check,
552         .destroy        = clusterip_tg_destroy,
553         .targetsize     = sizeof(struct ipt_clusterip_tgt_info),
554         .usersize       = offsetof(struct ipt_clusterip_tgt_info, config),
555 #ifdef CONFIG_COMPAT
556         .compatsize     = sizeof(struct compat_ipt_clusterip_tgt_info),
557 #endif /* CONFIG_COMPAT */
558         .me             = THIS_MODULE
559 };
560
561
562 /***********************************************************************
563  * ARP MANGLING CODE
564  ***********************************************************************/
565
566 /* hardcoded for 48bit ethernet and 32bit ipv4 addresses */
567 struct arp_payload {
568         u_int8_t src_hw[ETH_ALEN];
569         __be32 src_ip;
570         u_int8_t dst_hw[ETH_ALEN];
571         __be32 dst_ip;
572 } __packed;
573
574 #ifdef DEBUG
575 static void arp_print(struct arp_payload *payload)
576 {
577 #define HBUFFERLEN 30
578         char hbuffer[HBUFFERLEN];
579         int j, k;
580
581         for (k = 0, j = 0; k < HBUFFERLEN - 3 && j < ETH_ALEN; j++) {
582                 hbuffer[k++] = hex_asc_hi(payload->src_hw[j]);
583                 hbuffer[k++] = hex_asc_lo(payload->src_hw[j]);
584                 hbuffer[k++] = ':';
585         }
586         hbuffer[--k] = '\0';
587
588         pr_debug("src %pI4@%s, dst %pI4\n",
589                  &payload->src_ip, hbuffer, &payload->dst_ip);
590 }
591 #endif
592
593 static unsigned int
594 arp_mangle(void *priv,
595            struct sk_buff *skb,
596            const struct nf_hook_state *state)
597 {
598         struct arphdr *arp = arp_hdr(skb);
599         struct arp_payload *payload;
600         struct clusterip_config *c;
601         struct net *net = state->net;
602
603         /* we don't care about non-ethernet and non-ipv4 ARP */
604         if (arp->ar_hrd != htons(ARPHRD_ETHER) ||
605             arp->ar_pro != htons(ETH_P_IP) ||
606             arp->ar_pln != 4 || arp->ar_hln != ETH_ALEN)
607                 return NF_ACCEPT;
608
609         /* we only want to mangle arp requests and replies */
610         if (arp->ar_op != htons(ARPOP_REPLY) &&
611             arp->ar_op != htons(ARPOP_REQUEST))
612                 return NF_ACCEPT;
613
614         payload = (void *)(arp+1);
615
616         /* if there is no clusterip configuration for the arp reply's
617          * source ip, we don't want to mangle it */
618         c = clusterip_config_find_get(net, payload->src_ip, 0);
619         if (!c)
620                 return NF_ACCEPT;
621
622         /* normally the linux kernel always replies to arp queries of
623          * addresses on different interfacs.  However, in the CLUSTERIP case
624          * this wouldn't work, since we didn't subscribe the mcast group on
625          * other interfaces */
626         if (c->ifindex != state->out->ifindex) {
627                 pr_debug("not mangling arp reply on different interface: cip'%d'-skb'%d'\n",
628                          c->ifindex, state->out->ifindex);
629                 clusterip_config_put(c);
630                 return NF_ACCEPT;
631         }
632
633         /* mangle reply hardware address */
634         memcpy(payload->src_hw, c->clustermac, arp->ar_hln);
635
636 #ifdef DEBUG
637         pr_debug("mangled arp reply: ");
638         arp_print(payload);
639 #endif
640
641         clusterip_config_put(c);
642
643         return NF_ACCEPT;
644 }
645
646 static const struct nf_hook_ops cip_arp_ops = {
647         .hook = arp_mangle,
648         .pf = NFPROTO_ARP,
649         .hooknum = NF_ARP_OUT,
650         .priority = -1
651 };
652
653 /***********************************************************************
654  * PROC DIR HANDLING
655  ***********************************************************************/
656
657 #ifdef CONFIG_PROC_FS
658
659 struct clusterip_seq_position {
660         unsigned int pos;       /* position */
661         unsigned int weight;    /* number of bits set == size */
662         unsigned int bit;       /* current bit */
663         unsigned long val;      /* current value */
664 };
665
666 static void *clusterip_seq_start(struct seq_file *s, loff_t *pos)
667 {
668         struct clusterip_config *c = s->private;
669         unsigned int weight;
670         u_int32_t local_nodes;
671         struct clusterip_seq_position *idx;
672
673         /* FIXME: possible race */
674         local_nodes = c->local_nodes;
675         weight = hweight32(local_nodes);
676         if (*pos >= weight)
677                 return NULL;
678
679         idx = kmalloc(sizeof(struct clusterip_seq_position), GFP_KERNEL);
680         if (!idx)
681                 return ERR_PTR(-ENOMEM);
682
683         idx->pos = *pos;
684         idx->weight = weight;
685         idx->bit = ffs(local_nodes);
686         idx->val = local_nodes;
687         clear_bit(idx->bit - 1, &idx->val);
688
689         return idx;
690 }
691
692 static void *clusterip_seq_next(struct seq_file *s, void *v, loff_t *pos)
693 {
694         struct clusterip_seq_position *idx = v;
695
696         *pos = ++idx->pos;
697         if (*pos >= idx->weight) {
698                 kfree(v);
699                 return NULL;
700         }
701         idx->bit = ffs(idx->val);
702         clear_bit(idx->bit - 1, &idx->val);
703         return idx;
704 }
705
706 static void clusterip_seq_stop(struct seq_file *s, void *v)
707 {
708         if (!IS_ERR(v))
709                 kfree(v);
710 }
711
712 static int clusterip_seq_show(struct seq_file *s, void *v)
713 {
714         struct clusterip_seq_position *idx = v;
715
716         if (idx->pos != 0)
717                 seq_putc(s, ',');
718
719         seq_printf(s, "%u", idx->bit);
720
721         if (idx->pos == idx->weight - 1)
722                 seq_putc(s, '\n');
723
724         return 0;
725 }
726
727 static const struct seq_operations clusterip_seq_ops = {
728         .start  = clusterip_seq_start,
729         .next   = clusterip_seq_next,
730         .stop   = clusterip_seq_stop,
731         .show   = clusterip_seq_show,
732 };
733
734 static int clusterip_proc_open(struct inode *inode, struct file *file)
735 {
736         int ret = seq_open(file, &clusterip_seq_ops);
737
738         if (!ret) {
739                 struct seq_file *sf = file->private_data;
740                 struct clusterip_config *c = PDE_DATA(inode);
741
742                 sf->private = c;
743
744                 clusterip_config_get(c);
745         }
746
747         return ret;
748 }
749
750 static int clusterip_proc_release(struct inode *inode, struct file *file)
751 {
752         struct clusterip_config *c = PDE_DATA(inode);
753         int ret;
754
755         ret = seq_release(inode, file);
756
757         if (!ret)
758                 clusterip_config_put(c);
759
760         return ret;
761 }
762
763 static ssize_t clusterip_proc_write(struct file *file, const char __user *input,
764                                 size_t size, loff_t *ofs)
765 {
766         struct clusterip_config *c = PDE_DATA(file_inode(file));
767 #define PROC_WRITELEN   10
768         char buffer[PROC_WRITELEN+1];
769         unsigned long nodenum;
770         int rc;
771
772         if (size > PROC_WRITELEN)
773                 return -EIO;
774         if (copy_from_user(buffer, input, size))
775                 return -EFAULT;
776         buffer[size] = 0;
777
778         if (*buffer == '+') {
779                 rc = kstrtoul(buffer+1, 10, &nodenum);
780                 if (rc)
781                         return rc;
782                 if (clusterip_add_node(c, nodenum))
783                         return -ENOMEM;
784         } else if (*buffer == '-') {
785                 rc = kstrtoul(buffer+1, 10, &nodenum);
786                 if (rc)
787                         return rc;
788                 if (clusterip_del_node(c, nodenum))
789                         return -ENOENT;
790         } else
791                 return -EIO;
792
793         return size;
794 }
795
796 static const struct file_operations clusterip_proc_fops = {
797         .open    = clusterip_proc_open,
798         .read    = seq_read,
799         .write   = clusterip_proc_write,
800         .llseek  = seq_lseek,
801         .release = clusterip_proc_release,
802 };
803
804 #endif /* CONFIG_PROC_FS */
805
806 static int clusterip_net_init(struct net *net)
807 {
808         struct clusterip_net *cn = net_generic(net, clusterip_net_id);
809         int ret;
810
811         INIT_LIST_HEAD(&cn->configs);
812
813         spin_lock_init(&cn->lock);
814
815         ret = nf_register_net_hook(net, &cip_arp_ops);
816         if (ret < 0)
817                 return ret;
818
819 #ifdef CONFIG_PROC_FS
820         cn->procdir = proc_mkdir("ipt_CLUSTERIP", net->proc_net);
821         if (!cn->procdir) {
822                 nf_unregister_net_hook(net, &cip_arp_ops);
823                 pr_err("Unable to proc dir entry\n");
824                 return -ENOMEM;
825         }
826 #endif /* CONFIG_PROC_FS */
827
828         return 0;
829 }
830
831 static void clusterip_net_exit(struct net *net)
832 {
833         struct clusterip_net *cn = net_generic(net, clusterip_net_id);
834 #ifdef CONFIG_PROC_FS
835         proc_remove(cn->procdir);
836         cn->procdir = NULL;
837 #endif
838         nf_unregister_net_hook(net, &cip_arp_ops);
839         WARN_ON_ONCE(!list_empty(&cn->configs));
840 }
841
842 static struct pernet_operations clusterip_net_ops = {
843         .init = clusterip_net_init,
844         .exit = clusterip_net_exit,
845         .id   = &clusterip_net_id,
846         .size = sizeof(struct clusterip_net),
847 };
848
849 static int __init clusterip_tg_init(void)
850 {
851         int ret;
852
853         ret = register_pernet_subsys(&clusterip_net_ops);
854         if (ret < 0)
855                 return ret;
856
857         ret = xt_register_target(&clusterip_tg_reg);
858         if (ret < 0)
859                 goto cleanup_subsys;
860
861         pr_info("ClusterIP Version %s loaded successfully\n",
862                 CLUSTERIP_VERSION);
863
864         return 0;
865
866 cleanup_subsys:
867         unregister_pernet_subsys(&clusterip_net_ops);
868         return ret;
869 }
870
871 static void __exit clusterip_tg_exit(void)
872 {
873         pr_info("ClusterIP Version %s unloading\n", CLUSTERIP_VERSION);
874
875         xt_unregister_target(&clusterip_tg_reg);
876         unregister_pernet_subsys(&clusterip_net_ops);
877
878         /* Wait for completion of call_rcu_bh()'s (clusterip_config_rcu_free) */
879         rcu_barrier_bh();
880 }
881
882 module_init(clusterip_tg_init);
883 module_exit(clusterip_tg_exit);