blob: 16599bae11ddf6169264c7c81eff3ff23d7d019e [file] [log] [blame]
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001/* Cluster IP hashmark target
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * (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 */
Jan Engelhardtff67e4e2010-03-19 21:08:16 +010012#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/proc_fs.h>
15#include <linux/jhash.h>
KOVACS Krisztian136e92b2005-09-16 17:00:04 -070016#include <linux/bitops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/skbuff.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/netfilter_arp.h>
Jan Engelhardt6709dbb2007-02-07 15:11:19 -080026#include <linux/netfilter/x_tables.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/netfilter_ipv4/ip_tables.h>
28#include <linux/netfilter_ipv4/ipt_CLUSTERIP.h>
Patrick McHardy587aa642007-03-14 16:37:25 -070029#include <net/netfilter/nf_conntrack.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020030#include <net/net_namespace.h>
Gao fengce4ff762013-09-25 15:38:44 +080031#include <net/netns/generic.h>
Patrick McHardy587aa642007-03-14 16:37:25 -070032#include <net/checksum.h>
Changli Gao3d04ebb2010-08-17 20:34:40 +000033#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
KOVACS Krisztian136e92b2005-09-16 17:00:04 -070035#define CLUSTERIP_VERSION "0.8"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Linus Torvalds1da177e2005-04-16 15:20:36 -070037MODULE_LICENSE("GPL");
38MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
Jan Engelhardt2ae15b62008-01-14 23:42:28 -080039MODULE_DESCRIPTION("Xtables: CLUSTERIP target");
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41struct clusterip_config {
42 struct list_head list; /* list of all configs */
43 atomic_t refcount; /* reference count */
KOVACS Krisztian44513622005-09-16 16:59:46 -070044 atomic_t entries; /* number of entries/rules
45 * referencing us */
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Al Viro6a19d612006-09-28 14:22:24 -070047 __be32 clusterip; /* the IP address */
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 u_int8_t clustermac[ETH_ALEN]; /* the MAC address */
49 struct net_device *dev; /* device */
50 u_int16_t num_total_nodes; /* total number of nodes */
KOVACS Krisztian136e92b2005-09-16 17:00:04 -070051 unsigned long local_nodes; /* node number array */
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53#ifdef CONFIG_PROC_FS
54 struct proc_dir_entry *pde; /* proc dir entry */
55#endif
56 enum clusterip_hashmode hash_mode; /* which hashing mode */
57 u_int32_t hash_initval; /* hash initialization */
Eric Dumazetd73f33b2010-06-15 13:08:51 +020058 struct rcu_head rcu;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059};
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#ifdef CONFIG_PROC_FS
Arjan van de Ven9a321442007-02-12 00:55:35 -080062static const struct file_operations clusterip_proc_fops;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#endif
64
Gao fengce4ff762013-09-25 15:38:44 +080065static int clusterip_net_id __read_mostly;
66
67struct clusterip_net {
Gao feng26a89e42013-09-25 15:38:45 +080068 struct list_head configs;
Gao fengf1e80772013-09-25 15:38:46 +080069 /* lock protects the configs list */
70 spinlock_t lock;
71
Gao fengce4ff762013-09-25 15:38:44 +080072#ifdef CONFIG_PROC_FS
73 struct proc_dir_entry *procdir;
74#endif
75};
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static inline void
KOVACS Krisztian44513622005-09-16 16:59:46 -070078clusterip_config_get(struct clusterip_config *c)
79{
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 atomic_inc(&c->refcount);
81}
82
Eric Dumazetd73f33b2010-06-15 13:08:51 +020083
84static void clusterip_config_rcu_free(struct rcu_head *head)
85{
86 kfree(container_of(head, struct clusterip_config, rcu));
87}
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089static inline void
KOVACS Krisztian44513622005-09-16 16:59:46 -070090clusterip_config_put(struct clusterip_config *c)
91{
92 if (atomic_dec_and_test(&c->refcount))
Eric Dumazetd73f33b2010-06-15 13:08:51 +020093 call_rcu_bh(&c->rcu, clusterip_config_rcu_free);
KOVACS Krisztian44513622005-09-16 16:59:46 -070094}
95
KOVACS Krisztian44513622005-09-16 16:59:46 -070096/* decrease the count of entries using/referencing this config. If last
97 * entry(rule) is removed, remove the config from lists, but don't free it
98 * yet, since proc-files could still be holding references */
99static inline void
100clusterip_config_entry_put(struct clusterip_config *c)
101{
Gao fengd86946d2013-09-25 15:38:49 +0800102 struct net *net = dev_net(c->dev);
103 struct clusterip_net *cn = net_generic(net, clusterip_net_id);
Gao fengf1e80772013-09-25 15:38:46 +0800104
Eric Dumazetd73f33b2010-06-15 13:08:51 +0200105 local_bh_disable();
Gao fengf1e80772013-09-25 15:38:46 +0800106 if (atomic_dec_and_lock(&c->entries, &cn->lock)) {
Eric Dumazetd73f33b2010-06-15 13:08:51 +0200107 list_del_rcu(&c->list);
Gao fengf1e80772013-09-25 15:38:46 +0800108 spin_unlock(&cn->lock);
Eric Dumazetd73f33b2010-06-15 13:08:51 +0200109 local_bh_enable();
KOVACS Krisztian44513622005-09-16 16:59:46 -0700110
Jiri Pirko22bedad32010-04-01 21:22:57 +0000111 dev_mc_del(c->dev, c->clustermac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 dev_put(c->dev);
KOVACS Krisztian44513622005-09-16 16:59:46 -0700113
114 /* In case anyone still accesses the file, the open/close
115 * functions are also incrementing the refcount on their own,
116 * so it's safe to remove the entry even if it's in use. */
117#ifdef CONFIG_PROC_FS
David Howellsa8ca16e2013-04-12 17:27:28 +0100118 proc_remove(c->pde);
KOVACS Krisztian44513622005-09-16 16:59:46 -0700119#endif
Pavel Emelyanov4dee9592008-04-14 00:44:52 -0700120 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 }
Eric Dumazetd73f33b2010-06-15 13:08:51 +0200122 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125static struct clusterip_config *
Gao fengb5ef0f82013-09-25 15:38:47 +0800126__clusterip_config_find(struct net *net, __be32 clusterip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
Li Zefan4c610972007-12-04 23:22:26 -0800128 struct clusterip_config *c;
Gao fengb5ef0f82013-09-25 15:38:47 +0800129 struct clusterip_net *cn = net_generic(net, clusterip_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Gao feng26a89e42013-09-25 15:38:45 +0800131 list_for_each_entry_rcu(c, &cn->configs, list) {
Jan Engelhardt7c4e36b2007-07-07 22:19:08 -0700132 if (c->clusterip == clusterip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 return c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 }
135
136 return NULL;
137}
138
139static inline struct clusterip_config *
Gao fengb5ef0f82013-09-25 15:38:47 +0800140clusterip_config_find_get(struct net *net, __be32 clusterip, int entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
142 struct clusterip_config *c;
143
Eric Dumazetd73f33b2010-06-15 13:08:51 +0200144 rcu_read_lock_bh();
Gao fengb5ef0f82013-09-25 15:38:47 +0800145 c = __clusterip_config_find(net, clusterip);
Eric Dumazetd73f33b2010-06-15 13:08:51 +0200146 if (c) {
147 if (unlikely(!atomic_inc_not_zero(&c->refcount)))
148 c = NULL;
149 else if (entry)
150 atomic_inc(&c->entries);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 }
Eric Dumazetd73f33b2010-06-15 13:08:51 +0200152 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154 return c;
155}
156
KOVACS Krisztian136e92b2005-09-16 17:00:04 -0700157static void
158clusterip_config_init_nodelist(struct clusterip_config *c,
159 const struct ipt_clusterip_tgt_info *i)
160{
161 int n;
162
Jan Engelhardt7c4e36b2007-07-07 22:19:08 -0700163 for (n = 0; n < i->num_local_nodes; n++)
KOVACS Krisztian136e92b2005-09-16 17:00:04 -0700164 set_bit(i->local_nodes[n] - 1, &c->local_nodes);
KOVACS Krisztian136e92b2005-09-16 17:00:04 -0700165}
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167static struct clusterip_config *
Jan Engelhardt3cf93c92008-04-14 09:56:05 +0200168clusterip_config_init(const struct ipt_clusterip_tgt_info *i, __be32 ip,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 struct net_device *dev)
170{
171 struct clusterip_config *c;
Gao fengf58d7862013-09-25 15:38:48 +0800172 struct clusterip_net *cn = net_generic(dev_net(dev), clusterip_net_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700174 c = kzalloc(sizeof(*c), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 if (!c)
176 return NULL;
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 c->dev = dev;
179 c->clusterip = ip;
180 memcpy(&c->clustermac, &i->clustermac, ETH_ALEN);
181 c->num_total_nodes = i->num_total_nodes;
KOVACS Krisztian136e92b2005-09-16 17:00:04 -0700182 clusterip_config_init_nodelist(c, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 c->hash_mode = i->hash_mode;
184 c->hash_initval = i->hash_initval;
185 atomic_set(&c->refcount, 1);
KOVACS Krisztian44513622005-09-16 16:59:46 -0700186 atomic_set(&c->entries, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188#ifdef CONFIG_PROC_FS
Patrick McHardy76592582006-11-29 02:35:42 +0100189 {
190 char buffer[16];
191
192 /* create proc dir entry */
Harvey Harrisoncffee382008-10-31 00:53:08 -0700193 sprintf(buffer, "%pI4", &ip);
Denis V. Lunev6e79d852008-05-02 02:45:42 -0700194 c->pde = proc_create_data(buffer, S_IWUSR|S_IRUSR,
Gao fengce4ff762013-09-25 15:38:44 +0800195 cn->procdir,
Denis V. Lunev6e79d852008-05-02 02:45:42 -0700196 &clusterip_proc_fops, c);
Patrick McHardy76592582006-11-29 02:35:42 +0100197 if (!c->pde) {
198 kfree(c);
199 return NULL;
200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202#endif
203
Gao fengf1e80772013-09-25 15:38:46 +0800204 spin_lock_bh(&cn->lock);
Gao feng26a89e42013-09-25 15:38:45 +0800205 list_add_rcu(&c->list, &cn->configs);
Gao fengf1e80772013-09-25 15:38:46 +0800206 spin_unlock_bh(&cn->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208 return c;
209}
210
Patrick McHardy76592582006-11-29 02:35:42 +0100211#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212static int
213clusterip_add_node(struct clusterip_config *c, u_int16_t nodenum)
214{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
KOVACS Krisztian136e92b2005-09-16 17:00:04 -0700216 if (nodenum == 0 ||
217 nodenum > c->num_total_nodes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
KOVACS Krisztian136e92b2005-09-16 17:00:04 -0700220 /* check if we already have this number in our bitfield */
221 if (test_and_set_bit(nodenum - 1, &c->local_nodes))
222 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 return 0;
225}
226
Jan Engelhardte1931b72007-07-07 22:16:26 -0700227static bool
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228clusterip_del_node(struct clusterip_config *c, u_int16_t nodenum)
229{
KOVACS Krisztian136e92b2005-09-16 17:00:04 -0700230 if (nodenum == 0 ||
231 nodenum > c->num_total_nodes)
Jan Engelhardte1931b72007-07-07 22:16:26 -0700232 return true;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900233
KOVACS Krisztian136e92b2005-09-16 17:00:04 -0700234 if (test_and_clear_bit(nodenum - 1, &c->local_nodes))
Jan Engelhardte1931b72007-07-07 22:16:26 -0700235 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Jan Engelhardte1931b72007-07-07 22:16:26 -0700237 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
Patrick McHardy76592582006-11-29 02:35:42 +0100239#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241static inline u_int32_t
Jan Engelhardta47362a2007-07-07 22:16:55 -0700242clusterip_hashfn(const struct sk_buff *skb,
243 const struct clusterip_config *config)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
Jan Engelhardta47362a2007-07-07 22:16:55 -0700245 const struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 unsigned long hashval;
Changli Gao3d04ebb2010-08-17 20:34:40 +0000247 u_int16_t sport = 0, dport = 0;
248 int poff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Changli Gao3d04ebb2010-08-17 20:34:40 +0000250 poff = proto_ports_offset(iph->protocol);
251 if (poff >= 0) {
252 const u_int16_t *ports;
253 u16 _ports[2];
254
255 ports = skb_header_pointer(skb, iph->ihl * 4 + poff, 4, _ports);
256 if (ports) {
257 sport = ports[0];
258 dport = ports[1];
259 }
260 } else {
Joe Perchese87cc472012-05-13 21:56:26 +0000261 net_info_ratelimited("unknown protocol %u\n", iph->protocol);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 }
263
264 switch (config->hash_mode) {
265 case CLUSTERIP_HASHMODE_SIP:
266 hashval = jhash_1word(ntohl(iph->saddr),
267 config->hash_initval);
268 break;
269 case CLUSTERIP_HASHMODE_SIP_SPT:
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900270 hashval = jhash_2words(ntohl(iph->saddr), sport,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 config->hash_initval);
272 break;
273 case CLUSTERIP_HASHMODE_SIP_SPT_DPT:
274 hashval = jhash_3words(ntohl(iph->saddr), sport, dport,
275 config->hash_initval);
276 break;
277 default:
278 /* to make gcc happy */
279 hashval = 0;
280 /* This cannot happen, unless the check function wasn't called
281 * at rule load time */
Jan Engelhardtff67e4e2010-03-19 21:08:16 +0100282 pr_info("unknown mode %u\n", config->hash_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 BUG();
284 break;
285 }
286
287 /* node numbers are 1..n, not 0..n */
Daniel Borkmann8fc54f62014-08-23 20:58:54 +0200288 return reciprocal_scale(hashval, config->num_total_nodes) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289}
290
291static inline int
Jan Engelhardta47362a2007-07-07 22:16:55 -0700292clusterip_responsible(const struct clusterip_config *config, u_int32_t hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
KOVACS Krisztian136e92b2005-09-16 17:00:04 -0700294 return test_bit(hash - 1, &config->local_nodes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
296
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900297/***********************************************************************
298 * IPTABLES TARGET
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 ***********************************************************************/
300
301static unsigned int
Jan Engelhardt4b560b42009-07-05 19:43:26 +0200302clusterip_tg(struct sk_buff *skb, const struct xt_action_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
Jan Engelhardt7eb35582008-10-08 11:35:19 +0200304 const struct ipt_clusterip_tgt_info *cipinfo = par->targinfo;
Patrick McHardy587aa642007-03-14 16:37:25 -0700305 struct nf_conn *ct;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 enum ip_conntrack_info ctinfo;
Patrick McHardy587aa642007-03-14 16:37:25 -0700307 u_int32_t hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309 /* don't need to clusterip_config_get() here, since refcount
310 * is only decremented by destroy() - and ip_tables guarantees
311 * that the ->target() function isn't called after ->destroy() */
312
Herbert Xu3db05fe2007-10-15 00:53:15 -0700313 ct = nf_ct_get(skb, &ctinfo);
Eric Dumazet94d117a2011-01-18 16:27:56 +0100314 if (ct == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 return NF_DROP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317 /* special case: ICMP error handling. conntrack distinguishes between
318 * error messages (RELATED) and information requests (see below) */
Joe Perches3666ed12009-11-23 23:17:06 +0100319 if (ip_hdr(skb)->protocol == IPPROTO_ICMP &&
320 (ctinfo == IP_CT_RELATED ||
Eric Dumazetfb048832011-05-19 15:44:27 +0200321 ctinfo == IP_CT_RELATED_REPLY))
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800322 return XT_CONTINUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900324 /* ip_conntrack_icmp guarantees us that we only have ICMP_ECHO,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 * TIMESTAMP, INFO_REQUEST or ADDRESS type icmp packets from here
326 * on, which all have an ID field [relevant for hashing]. */
327
Herbert Xu3db05fe2007-10-15 00:53:15 -0700328 hash = clusterip_hashfn(skb, cipinfo->config);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
330 switch (ctinfo) {
Joe Perches181b1e92011-07-01 09:43:06 +0000331 case IP_CT_NEW:
332 ct->mark = hash;
333 break;
334 case IP_CT_RELATED:
335 case IP_CT_RELATED_REPLY:
336 /* FIXME: we don't handle expectations at the moment.
337 * They can arrive on a different node than
338 * the master connection (e.g. FTP passive mode) */
339 case IP_CT_ESTABLISHED:
340 case IP_CT_ESTABLISHED_REPLY:
341 break;
342 default: /* Prevent gcc warnings */
343 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 }
345
Patrick McHardy0d537782007-07-07 22:39:38 -0700346#ifdef DEBUG
Jan Engelhardt3c9fba62008-04-14 11:15:54 +0200347 nf_ct_dump_tuple_ip(&ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348#endif
Patrick McHardy0d537782007-07-07 22:39:38 -0700349 pr_debug("hash=%u ct_hash=%u ", hash, ct->mark);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 if (!clusterip_responsible(cipinfo->config, hash)) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700351 pr_debug("not responsible\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 return NF_DROP;
353 }
Patrick McHardy0d537782007-07-07 22:39:38 -0700354 pr_debug("responsible\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356 /* despite being received via linklayer multicast, this is
357 * actually a unicast IP packet. TCP doesn't like PACKET_MULTICAST */
Herbert Xu3db05fe2007-10-15 00:53:15 -0700358 skb->pkt_type = PACKET_HOST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800360 return XT_CONTINUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
362
Jan Engelhardt135367b2010-03-19 17:16:42 +0100363static int clusterip_tg_check(const struct xt_tgchk_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +0200365 struct ipt_clusterip_tgt_info *cipinfo = par->targinfo;
366 const struct ipt_entry *e = par->entryinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 struct clusterip_config *config;
Dmitry Vyukovab2b0f72018-01-30 15:21:34 +0100368 int ret, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Pablo Neira Ayuso55917a22015-05-14 14:57:23 +0200370 if (par->nft_compat) {
371 pr_err("cannot use CLUSTERIP target from nftables compat\n");
372 return -EOPNOTSUPP;
373 }
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 if (cipinfo->hash_mode != CLUSTERIP_HASHMODE_SIP &&
376 cipinfo->hash_mode != CLUSTERIP_HASHMODE_SIP_SPT &&
377 cipinfo->hash_mode != CLUSTERIP_HASHMODE_SIP_SPT_DPT) {
Jan Engelhardtff67e4e2010-03-19 21:08:16 +0100378 pr_info("unknown mode %u\n", cipinfo->hash_mode);
Jan Engelhardtd6b00a52010-03-25 16:34:45 +0100379 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 }
Joe Perches3666ed12009-11-23 23:17:06 +0100382 if (e->ip.dmsk.s_addr != htonl(0xffffffff) ||
383 e->ip.dst.s_addr == 0) {
Jan Engelhardtff67e4e2010-03-19 21:08:16 +0100384 pr_info("Please specify destination IP\n");
Jan Engelhardtd6b00a52010-03-25 16:34:45 +0100385 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 }
Dmitry Vyukovab2b0f72018-01-30 15:21:34 +0100387 if (cipinfo->num_local_nodes > ARRAY_SIZE(cipinfo->local_nodes)) {
388 pr_info("bad num_local_nodes %u\n", cipinfo->num_local_nodes);
389 return -EINVAL;
390 }
391 for (i = 0; i < cipinfo->num_local_nodes; i++) {
392 if (cipinfo->local_nodes[i] - 1 >=
393 sizeof(config->local_nodes) * 8) {
394 pr_info("bad local_nodes[%d] %u\n",
395 i, cipinfo->local_nodes[i]);
396 return -EINVAL;
397 }
398 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Gao fengd86946d2013-09-25 15:38:49 +0800400 config = clusterip_config_find_get(par->net, e->ip.dst.s_addr, 1);
Patrick McHardyd3c3f422007-07-07 22:38:30 -0700401 if (!config) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 if (!(cipinfo->flags & CLUSTERIP_FLAG_NEW)) {
Jan Engelhardtff67e4e2010-03-19 21:08:16 +0100403 pr_info("no config found for %pI4, need 'new'\n",
404 &e->ip.dst.s_addr);
Jan Engelhardtd6b00a52010-03-25 16:34:45 +0100405 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 } else {
407 struct net_device *dev;
408
409 if (e->ip.iniface[0] == '\0') {
Jan Engelhardtff67e4e2010-03-19 21:08:16 +0100410 pr_info("Please specify an interface name\n");
Jan Engelhardtd6b00a52010-03-25 16:34:45 +0100411 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 }
413
Gao fengd86946d2013-09-25 15:38:49 +0800414 dev = dev_get_by_name(par->net, e->ip.iniface);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 if (!dev) {
Jan Engelhardtff67e4e2010-03-19 21:08:16 +0100416 pr_info("no such interface %s\n",
417 e->ip.iniface);
Jan Engelhardt4a5a5c72010-03-19 17:32:59 +0100418 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 }
420
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900421 config = clusterip_config_init(cipinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 e->ip.dst.s_addr, dev);
423 if (!config) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 dev_put(dev);
Jan Engelhardt4a5a5c72010-03-19 17:32:59 +0100425 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 }
Jiri Pirko22bedad32010-04-01 21:22:57 +0000427 dev_mc_add(config->dev, config->clustermac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 }
429 }
Patrick McHardyd3c3f422007-07-07 22:38:30 -0700430 cipinfo->config = config;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Jan Engelhardt4a5a5c72010-03-19 17:32:59 +0100432 ret = nf_ct_l3proto_try_module_get(par->family);
Jan Engelhardtf95c74e2010-03-21 04:05:56 +0100433 if (ret < 0)
Jan Engelhardtff67e4e2010-03-19 21:08:16 +0100434 pr_info("cannot load conntrack support for proto=%u\n",
435 par->family);
Pablo Neira Ayuso43270b12015-03-02 14:40:39 +0100436
437 if (!par->net->xt.clusterip_deprecated_warning) {
438 pr_info("ipt_CLUSTERIP is deprecated and it will removed soon, "
439 "use xt_cluster instead\n");
440 par->net->xt.clusterip_deprecated_warning = true;
441 }
442
Jan Engelhardtf95c74e2010-03-21 04:05:56 +0100443 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444}
445
446/* drop reference count of cluster config when rule is deleted */
Jan Engelhardta2df1642008-10-08 11:35:19 +0200447static void clusterip_tg_destroy(const struct xt_tgdtor_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
Jan Engelhardta2df1642008-10-08 11:35:19 +0200449 const struct ipt_clusterip_tgt_info *cipinfo = par->targinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
KOVACS Krisztian44513622005-09-16 16:59:46 -0700451 /* if no more entries are referencing the config, remove it
452 * from the list and destroy the proc entry */
453 clusterip_config_entry_put(cipinfo->config);
454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 clusterip_config_put(cipinfo->config);
Yasuyuki Kozakai11078c32006-12-12 00:29:02 -0800456
Jan Engelhardt0d345452010-03-19 18:47:51 +0100457 nf_ct_l3proto_module_put(par->family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458}
459
Patrick McHardyd3c3f422007-07-07 22:38:30 -0700460#ifdef CONFIG_COMPAT
461struct compat_ipt_clusterip_tgt_info
462{
463 u_int32_t flags;
464 u_int8_t clustermac[6];
465 u_int16_t num_total_nodes;
466 u_int16_t num_local_nodes;
467 u_int16_t local_nodes[CLUSTERIP_MAX_NODES];
468 u_int32_t hash_mode;
469 u_int32_t hash_initval;
470 compat_uptr_t config;
471};
472#endif /* CONFIG_COMPAT */
473
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800474static struct xt_target clusterip_tg_reg __read_mostly = {
Patrick McHardy1d5cd902006-03-20 18:01:14 -0800475 .name = "CLUSTERIP",
Jan Engelhardtee999d82008-10-08 11:35:01 +0200476 .family = NFPROTO_IPV4,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800477 .target = clusterip_tg,
478 .checkentry = clusterip_tg_check,
479 .destroy = clusterip_tg_destroy,
Patrick McHardyd3c3f422007-07-07 22:38:30 -0700480 .targetsize = sizeof(struct ipt_clusterip_tgt_info),
481#ifdef CONFIG_COMPAT
482 .compatsize = sizeof(struct compat_ipt_clusterip_tgt_info),
483#endif /* CONFIG_COMPAT */
Patrick McHardy1d5cd902006-03-20 18:01:14 -0800484 .me = THIS_MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485};
486
487
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900488/***********************************************************************
489 * ARP MANGLING CODE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 ***********************************************************************/
491
492/* hardcoded for 48bit ethernet and 32bit ipv4 addresses */
493struct arp_payload {
494 u_int8_t src_hw[ETH_ALEN];
Al Viro6a19d612006-09-28 14:22:24 -0700495 __be32 src_ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 u_int8_t dst_hw[ETH_ALEN];
Al Viro6a19d612006-09-28 14:22:24 -0700497 __be32 dst_ip;
Gustavo F. Padovan3f30fc12010-07-21 10:59:58 +0000498} __packed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Patrick McHardy0d537782007-07-07 22:39:38 -0700500#ifdef DEBUG
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900501static void arp_print(struct arp_payload *payload)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
503#define HBUFFERLEN 30
504 char hbuffer[HBUFFERLEN];
Ian Morrisc8d71d02015-10-14 23:17:08 +0100505 int j, k;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Ian Morrisc8d71d02015-10-14 23:17:08 +0100507 for (k = 0, j = 0; k < HBUFFERLEN - 3 && j < ETH_ALEN; j++) {
Harvey Harrison6a8341b2008-07-30 16:30:15 -0700508 hbuffer[k++] = hex_asc_hi(payload->src_hw[j]);
509 hbuffer[k++] = hex_asc_lo(payload->src_hw[j]);
Ian Morrisc8d71d02015-10-14 23:17:08 +0100510 hbuffer[k++] = ':';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 }
Ian Morrisc8d71d02015-10-14 23:17:08 +0100512 hbuffer[--k] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Jan Engelhardtff67e4e2010-03-19 21:08:16 +0100514 pr_debug("src %pI4@%s, dst %pI4\n",
515 &payload->src_ip, hbuffer, &payload->dst_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516}
517#endif
518
519static unsigned int
Eric W. Biederman06198b32015-09-18 14:33:06 -0500520arp_mangle(void *priv,
Herbert Xu3db05fe2007-10-15 00:53:15 -0700521 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -0400522 const struct nf_hook_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
Herbert Xu3db05fe2007-10-15 00:53:15 -0700524 struct arphdr *arp = arp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 struct arp_payload *payload;
526 struct clusterip_config *c;
Eric W. Biederman9dff2c92015-09-15 20:04:17 -0500527 struct net *net = state->net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529 /* we don't care about non-ethernet and non-ipv4 ARP */
Joe Perches3666ed12009-11-23 23:17:06 +0100530 if (arp->ar_hrd != htons(ARPHRD_ETHER) ||
531 arp->ar_pro != htons(ETH_P_IP) ||
532 arp->ar_pln != 4 || arp->ar_hln != ETH_ALEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 return NF_ACCEPT;
534
Harald Welte4095ebf2005-06-28 12:49:30 -0700535 /* we only want to mangle arp requests and replies */
Joe Perches3666ed12009-11-23 23:17:06 +0100536 if (arp->ar_op != htons(ARPOP_REPLY) &&
537 arp->ar_op != htons(ARPOP_REQUEST))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 return NF_ACCEPT;
539
540 payload = (void *)(arp+1);
541
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900542 /* if there is no clusterip configuration for the arp reply's
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 * source ip, we don't want to mangle it */
Gao fengd86946d2013-09-25 15:38:49 +0800544 c = clusterip_config_find_get(net, payload->src_ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 if (!c)
546 return NF_ACCEPT;
547
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900548 /* normally the linux kernel always replies to arp queries of
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 * addresses on different interfacs. However, in the CLUSTERIP case
550 * this wouldn't work, since we didn't subscribe the mcast group on
551 * other interfaces */
David S. Miller238e54c2015-04-03 20:32:56 -0400552 if (c->dev != state->out) {
Jan Engelhardtff67e4e2010-03-19 21:08:16 +0100553 pr_debug("not mangling arp reply on different "
Patrick McHardy0d537782007-07-07 22:39:38 -0700554 "interface: cip'%s'-skb'%s'\n",
David S. Miller238e54c2015-04-03 20:32:56 -0400555 c->dev->name, state->out->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 clusterip_config_put(c);
557 return NF_ACCEPT;
558 }
559
560 /* mangle reply hardware address */
561 memcpy(payload->src_hw, c->clustermac, arp->ar_hln);
562
Patrick McHardy0d537782007-07-07 22:39:38 -0700563#ifdef DEBUG
Jan Engelhardtff67e4e2010-03-19 21:08:16 +0100564 pr_debug("mangled arp reply: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 arp_print(payload);
566#endif
567
568 clusterip_config_put(c);
569
570 return NF_ACCEPT;
571}
572
Patrick McHardy19994142007-12-05 01:23:00 -0800573static struct nf_hook_ops cip_arp_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 .hook = arp_mangle,
Jan Engelhardtee999d82008-10-08 11:35:01 +0200575 .pf = NFPROTO_ARP,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 .hooknum = NF_ARP_OUT,
577 .priority = -1
578};
579
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900580/***********************************************************************
581 * PROC DIR HANDLING
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 ***********************************************************************/
583
584#ifdef CONFIG_PROC_FS
585
KOVACS Krisztian136e92b2005-09-16 17:00:04 -0700586struct clusterip_seq_position {
587 unsigned int pos; /* position */
588 unsigned int weight; /* number of bits set == size */
589 unsigned int bit; /* current bit */
590 unsigned long val; /* current value */
591};
592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593static void *clusterip_seq_start(struct seq_file *s, loff_t *pos)
594{
Alexey Dobriyan47778142010-01-22 22:21:18 +0100595 struct clusterip_config *c = s->private;
KOVACS Krisztian136e92b2005-09-16 17:00:04 -0700596 unsigned int weight;
597 u_int32_t local_nodes;
598 struct clusterip_seq_position *idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
KOVACS Krisztian136e92b2005-09-16 17:00:04 -0700600 /* FIXME: possible race */
601 local_nodes = c->local_nodes;
602 weight = hweight32(local_nodes);
603 if (*pos >= weight)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 return NULL;
605
KOVACS Krisztian136e92b2005-09-16 17:00:04 -0700606 idx = kmalloc(sizeof(struct clusterip_seq_position), GFP_KERNEL);
607 if (!idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 return ERR_PTR(-ENOMEM);
609
KOVACS Krisztian136e92b2005-09-16 17:00:04 -0700610 idx->pos = *pos;
611 idx->weight = weight;
612 idx->bit = ffs(local_nodes);
613 idx->val = local_nodes;
614 clear_bit(idx->bit - 1, &idx->val);
615
616 return idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617}
618
619static void *clusterip_seq_next(struct seq_file *s, void *v, loff_t *pos)
620{
Jan Engelhardt3cf93c92008-04-14 09:56:05 +0200621 struct clusterip_seq_position *idx = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
KOVACS Krisztian136e92b2005-09-16 17:00:04 -0700623 *pos = ++idx->pos;
624 if (*pos >= idx->weight) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 kfree(v);
626 return NULL;
627 }
KOVACS Krisztian136e92b2005-09-16 17:00:04 -0700628 idx->bit = ffs(idx->val);
629 clear_bit(idx->bit - 1, &idx->val);
630 return idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631}
632
633static void clusterip_seq_stop(struct seq_file *s, void *v)
634{
Eric Dumazet902a3dd2010-04-01 12:54:09 +0200635 if (!IS_ERR(v))
636 kfree(v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637}
638
639static int clusterip_seq_show(struct seq_file *s, void *v)
640{
Jan Engelhardt3cf93c92008-04-14 09:56:05 +0200641 struct clusterip_seq_position *idx = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900643 if (idx->pos != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 seq_putc(s, ',');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
KOVACS Krisztian136e92b2005-09-16 17:00:04 -0700646 seq_printf(s, "%u", idx->bit);
647
648 if (idx->pos == idx->weight - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 seq_putc(s, '\n');
650
651 return 0;
652}
653
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700654static const struct seq_operations clusterip_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 .start = clusterip_seq_start,
656 .next = clusterip_seq_next,
657 .stop = clusterip_seq_stop,
658 .show = clusterip_seq_show,
659};
660
661static int clusterip_proc_open(struct inode *inode, struct file *file)
662{
663 int ret = seq_open(file, &clusterip_seq_ops);
664
665 if (!ret) {
666 struct seq_file *sf = file->private_data;
Al Virod9dda782013-03-31 18:16:14 -0400667 struct clusterip_config *c = PDE_DATA(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Alexey Dobriyan47778142010-01-22 22:21:18 +0100669 sf->private = c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671 clusterip_config_get(c);
672 }
673
674 return ret;
675}
676
677static int clusterip_proc_release(struct inode *inode, struct file *file)
678{
Al Virod9dda782013-03-31 18:16:14 -0400679 struct clusterip_config *c = PDE_DATA(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 int ret;
681
682 ret = seq_release(inode, file);
683
684 if (!ret)
685 clusterip_config_put(c);
686
687 return ret;
688}
689
690static ssize_t clusterip_proc_write(struct file *file, const char __user *input,
691 size_t size, loff_t *ofs)
692{
Al Virod9dda782013-03-31 18:16:14 -0400693 struct clusterip_config *c = PDE_DATA(file_inode(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694#define PROC_WRITELEN 10
695 char buffer[PROC_WRITELEN+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 unsigned long nodenum;
Abhijit Pawar4b5511e2012-12-09 23:12:28 +0000697 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Vasiliy Kulikov961ed182011-03-20 15:42:52 +0100699 if (size > PROC_WRITELEN)
700 return -EIO;
701 if (copy_from_user(buffer, input, size))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 return -EFAULT;
Vasiliy Kulikov961ed182011-03-20 15:42:52 +0100703 buffer[size] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705 if (*buffer == '+') {
Abhijit Pawar4b5511e2012-12-09 23:12:28 +0000706 rc = kstrtoul(buffer+1, 10, &nodenum);
707 if (rc)
708 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 if (clusterip_add_node(c, nodenum))
710 return -ENOMEM;
711 } else if (*buffer == '-') {
Abhijit Pawar4b5511e2012-12-09 23:12:28 +0000712 rc = kstrtoul(buffer+1, 10, &nodenum);
713 if (rc)
714 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 if (clusterip_del_node(c, nodenum))
716 return -ENOENT;
717 } else
718 return -EIO;
719
720 return size;
721}
722
Arjan van de Ven9a321442007-02-12 00:55:35 -0800723static const struct file_operations clusterip_proc_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 .owner = THIS_MODULE,
725 .open = clusterip_proc_open,
726 .read = seq_read,
727 .write = clusterip_proc_write,
728 .llseek = seq_lseek,
729 .release = clusterip_proc_release,
730};
731
732#endif /* CONFIG_PROC_FS */
733
Gao fengce4ff762013-09-25 15:38:44 +0800734static int clusterip_net_init(struct net *net)
735{
Gao fengce4ff762013-09-25 15:38:44 +0800736 struct clusterip_net *cn = net_generic(net, clusterip_net_id);
737
Gao feng26a89e42013-09-25 15:38:45 +0800738 INIT_LIST_HEAD(&cn->configs);
739
Gao fengf1e80772013-09-25 15:38:46 +0800740 spin_lock_init(&cn->lock);
741
Gao feng26a89e42013-09-25 15:38:45 +0800742#ifdef CONFIG_PROC_FS
Gao fengce4ff762013-09-25 15:38:44 +0800743 cn->procdir = proc_mkdir("ipt_CLUSTERIP", net->proc_net);
744 if (!cn->procdir) {
745 pr_err("Unable to proc dir entry\n");
746 return -ENOMEM;
747 }
748#endif /* CONFIG_PROC_FS */
749
750 return 0;
751}
752
753static void clusterip_net_exit(struct net *net)
754{
755#ifdef CONFIG_PROC_FS
756 struct clusterip_net *cn = net_generic(net, clusterip_net_id);
757 proc_remove(cn->procdir);
758#endif
759}
760
761static struct pernet_operations clusterip_net_ops = {
762 .init = clusterip_net_init,
763 .exit = clusterip_net_exit,
764 .id = &clusterip_net_id,
765 .size = sizeof(struct clusterip_net),
766};
767
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800768static int __init clusterip_tg_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
770 int ret;
771
Gao fengce4ff762013-09-25 15:38:44 +0800772 ret = register_pernet_subsys(&clusterip_net_ops);
Patrick McHardy32292a72006-04-06 14:11:30 -0700773 if (ret < 0)
774 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Gao fengce4ff762013-09-25 15:38:44 +0800776 ret = xt_register_target(&clusterip_tg_reg);
777 if (ret < 0)
778 goto cleanup_subsys;
779
Patrick McHardy32292a72006-04-06 14:11:30 -0700780 ret = nf_register_hook(&cip_arp_ops);
781 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 goto cleanup_target;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Jan Engelhardtff67e4e2010-03-19 21:08:16 +0100784 pr_info("ClusterIP Version %s loaded successfully\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 CLUSTERIP_VERSION);
Gao fengce4ff762013-09-25 15:38:44 +0800786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 return 0;
788
Patrick McHardy32292a72006-04-06 14:11:30 -0700789cleanup_target:
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800790 xt_unregister_target(&clusterip_tg_reg);
Gao fengce4ff762013-09-25 15:38:44 +0800791cleanup_subsys:
792 unregister_pernet_subsys(&clusterip_net_ops);
Patrick McHardy32292a72006-04-06 14:11:30 -0700793 return ret;
794}
795
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800796static void __exit clusterip_tg_exit(void)
Patrick McHardy32292a72006-04-06 14:11:30 -0700797{
Jan Engelhardtff67e4e2010-03-19 21:08:16 +0100798 pr_info("ClusterIP Version %s unloading\n", CLUSTERIP_VERSION);
Gao fengce4ff762013-09-25 15:38:44 +0800799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 nf_unregister_hook(&cip_arp_ops);
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800801 xt_unregister_target(&clusterip_tg_reg);
Gao fengce4ff762013-09-25 15:38:44 +0800802 unregister_pernet_subsys(&clusterip_net_ops);
Eric Dumazetd73f33b2010-06-15 13:08:51 +0200803
804 /* Wait for completion of call_rcu_bh()'s (clusterip_config_rcu_free) */
805 rcu_barrier_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806}
807
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800808module_init(clusterip_tg_init);
809module_exit(clusterip_tg_exit);