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