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