blob: 4a9e6db9df8d719a14b6aa129b78ba614587767f [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;
Jan Engelhardt4a5a5c72010-03-19 17:32:59 +0100368 int ret;
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 }
387
388 /* FIXME: further sanity checks */
389
Gao fengd86946d2013-09-25 15:38:49 +0800390 config = clusterip_config_find_get(par->net, e->ip.dst.s_addr, 1);
Patrick McHardyd3c3f422007-07-07 22:38:30 -0700391 if (!config) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 if (!(cipinfo->flags & CLUSTERIP_FLAG_NEW)) {
Jan Engelhardtff67e4e2010-03-19 21:08:16 +0100393 pr_info("no config found for %pI4, need 'new'\n",
394 &e->ip.dst.s_addr);
Jan Engelhardtd6b00a52010-03-25 16:34:45 +0100395 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 } else {
397 struct net_device *dev;
398
399 if (e->ip.iniface[0] == '\0') {
Jan Engelhardtff67e4e2010-03-19 21:08:16 +0100400 pr_info("Please specify an interface name\n");
Jan Engelhardtd6b00a52010-03-25 16:34:45 +0100401 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 }
403
Gao fengd86946d2013-09-25 15:38:49 +0800404 dev = dev_get_by_name(par->net, e->ip.iniface);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 if (!dev) {
Jan Engelhardtff67e4e2010-03-19 21:08:16 +0100406 pr_info("no such interface %s\n",
407 e->ip.iniface);
Jan Engelhardt4a5a5c72010-03-19 17:32:59 +0100408 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 }
410
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900411 config = clusterip_config_init(cipinfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 e->ip.dst.s_addr, dev);
413 if (!config) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 dev_put(dev);
Jan Engelhardt4a5a5c72010-03-19 17:32:59 +0100415 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 }
Jiri Pirko22bedad32010-04-01 21:22:57 +0000417 dev_mc_add(config->dev, config->clustermac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 }
419 }
Patrick McHardyd3c3f422007-07-07 22:38:30 -0700420 cipinfo->config = config;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Jan Engelhardt4a5a5c72010-03-19 17:32:59 +0100422 ret = nf_ct_l3proto_try_module_get(par->family);
Jan Engelhardtf95c74e2010-03-21 04:05:56 +0100423 if (ret < 0)
Jan Engelhardtff67e4e2010-03-19 21:08:16 +0100424 pr_info("cannot load conntrack support for proto=%u\n",
425 par->family);
Pablo Neira Ayuso43270b12015-03-02 14:40:39 +0100426
427 if (!par->net->xt.clusterip_deprecated_warning) {
428 pr_info("ipt_CLUSTERIP is deprecated and it will removed soon, "
429 "use xt_cluster instead\n");
430 par->net->xt.clusterip_deprecated_warning = true;
431 }
432
Jan Engelhardtf95c74e2010-03-21 04:05:56 +0100433 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435
436/* drop reference count of cluster config when rule is deleted */
Jan Engelhardta2df1642008-10-08 11:35:19 +0200437static void clusterip_tg_destroy(const struct xt_tgdtor_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Jan Engelhardta2df1642008-10-08 11:35:19 +0200439 const struct ipt_clusterip_tgt_info *cipinfo = par->targinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
KOVACS Krisztian44513622005-09-16 16:59:46 -0700441 /* if no more entries are referencing the config, remove it
442 * from the list and destroy the proc entry */
443 clusterip_config_entry_put(cipinfo->config);
444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 clusterip_config_put(cipinfo->config);
Yasuyuki Kozakai11078c32006-12-12 00:29:02 -0800446
Jan Engelhardt0d345452010-03-19 18:47:51 +0100447 nf_ct_l3proto_module_put(par->family);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
449
Patrick McHardyd3c3f422007-07-07 22:38:30 -0700450#ifdef CONFIG_COMPAT
451struct compat_ipt_clusterip_tgt_info
452{
453 u_int32_t flags;
454 u_int8_t clustermac[6];
455 u_int16_t num_total_nodes;
456 u_int16_t num_local_nodes;
457 u_int16_t local_nodes[CLUSTERIP_MAX_NODES];
458 u_int32_t hash_mode;
459 u_int32_t hash_initval;
460 compat_uptr_t config;
461};
462#endif /* CONFIG_COMPAT */
463
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800464static struct xt_target clusterip_tg_reg __read_mostly = {
Patrick McHardy1d5cd902006-03-20 18:01:14 -0800465 .name = "CLUSTERIP",
Jan Engelhardtee999d82008-10-08 11:35:01 +0200466 .family = NFPROTO_IPV4,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800467 .target = clusterip_tg,
468 .checkentry = clusterip_tg_check,
469 .destroy = clusterip_tg_destroy,
Patrick McHardyd3c3f422007-07-07 22:38:30 -0700470 .targetsize = sizeof(struct ipt_clusterip_tgt_info),
471#ifdef CONFIG_COMPAT
472 .compatsize = sizeof(struct compat_ipt_clusterip_tgt_info),
473#endif /* CONFIG_COMPAT */
Patrick McHardy1d5cd902006-03-20 18:01:14 -0800474 .me = THIS_MODULE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475};
476
477
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900478/***********************************************************************
479 * ARP MANGLING CODE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 ***********************************************************************/
481
482/* hardcoded for 48bit ethernet and 32bit ipv4 addresses */
483struct arp_payload {
484 u_int8_t src_hw[ETH_ALEN];
Al Viro6a19d612006-09-28 14:22:24 -0700485 __be32 src_ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 u_int8_t dst_hw[ETH_ALEN];
Al Viro6a19d612006-09-28 14:22:24 -0700487 __be32 dst_ip;
Gustavo F. Padovan3f30fc12010-07-21 10:59:58 +0000488} __packed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Patrick McHardy0d537782007-07-07 22:39:38 -0700490#ifdef DEBUG
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900491static void arp_print(struct arp_payload *payload)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
493#define HBUFFERLEN 30
494 char hbuffer[HBUFFERLEN];
Ian Morrisc8d71d02015-10-14 23:17:08 +0100495 int j, k;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Ian Morrisc8d71d02015-10-14 23:17:08 +0100497 for (k = 0, j = 0; k < HBUFFERLEN - 3 && j < ETH_ALEN; j++) {
Harvey Harrison6a8341b2008-07-30 16:30:15 -0700498 hbuffer[k++] = hex_asc_hi(payload->src_hw[j]);
499 hbuffer[k++] = hex_asc_lo(payload->src_hw[j]);
Ian Morrisc8d71d02015-10-14 23:17:08 +0100500 hbuffer[k++] = ':';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 }
Ian Morrisc8d71d02015-10-14 23:17:08 +0100502 hbuffer[--k] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Jan Engelhardtff67e4e2010-03-19 21:08:16 +0100504 pr_debug("src %pI4@%s, dst %pI4\n",
505 &payload->src_ip, hbuffer, &payload->dst_ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506}
507#endif
508
509static unsigned int
Eric W. Biederman06198b32015-09-18 14:33:06 -0500510arp_mangle(void *priv,
Herbert Xu3db05fe2007-10-15 00:53:15 -0700511 struct sk_buff *skb,
David S. Miller238e54c2015-04-03 20:32:56 -0400512 const struct nf_hook_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
Herbert Xu3db05fe2007-10-15 00:53:15 -0700514 struct arphdr *arp = arp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 struct arp_payload *payload;
516 struct clusterip_config *c;
Eric W. Biederman9dff2c92015-09-15 20:04:17 -0500517 struct net *net = state->net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519 /* we don't care about non-ethernet and non-ipv4 ARP */
Joe Perches3666ed12009-11-23 23:17:06 +0100520 if (arp->ar_hrd != htons(ARPHRD_ETHER) ||
521 arp->ar_pro != htons(ETH_P_IP) ||
522 arp->ar_pln != 4 || arp->ar_hln != ETH_ALEN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 return NF_ACCEPT;
524
Harald Welte4095ebf2005-06-28 12:49:30 -0700525 /* we only want to mangle arp requests and replies */
Joe Perches3666ed12009-11-23 23:17:06 +0100526 if (arp->ar_op != htons(ARPOP_REPLY) &&
527 arp->ar_op != htons(ARPOP_REQUEST))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 return NF_ACCEPT;
529
530 payload = (void *)(arp+1);
531
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900532 /* if there is no clusterip configuration for the arp reply's
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 * source ip, we don't want to mangle it */
Gao fengd86946d2013-09-25 15:38:49 +0800534 c = clusterip_config_find_get(net, payload->src_ip, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 if (!c)
536 return NF_ACCEPT;
537
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900538 /* normally the linux kernel always replies to arp queries of
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 * addresses on different interfacs. However, in the CLUSTERIP case
540 * this wouldn't work, since we didn't subscribe the mcast group on
541 * other interfaces */
David S. Miller238e54c2015-04-03 20:32:56 -0400542 if (c->dev != state->out) {
Jan Engelhardtff67e4e2010-03-19 21:08:16 +0100543 pr_debug("not mangling arp reply on different "
Patrick McHardy0d537782007-07-07 22:39:38 -0700544 "interface: cip'%s'-skb'%s'\n",
David S. Miller238e54c2015-04-03 20:32:56 -0400545 c->dev->name, state->out->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 clusterip_config_put(c);
547 return NF_ACCEPT;
548 }
549
550 /* mangle reply hardware address */
551 memcpy(payload->src_hw, c->clustermac, arp->ar_hln);
552
Patrick McHardy0d537782007-07-07 22:39:38 -0700553#ifdef DEBUG
Jan Engelhardtff67e4e2010-03-19 21:08:16 +0100554 pr_debug("mangled arp reply: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 arp_print(payload);
556#endif
557
558 clusterip_config_put(c);
559
560 return NF_ACCEPT;
561}
562
Patrick McHardy19994142007-12-05 01:23:00 -0800563static struct nf_hook_ops cip_arp_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 .hook = arp_mangle,
Jan Engelhardtee999d82008-10-08 11:35:01 +0200565 .pf = NFPROTO_ARP,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 .hooknum = NF_ARP_OUT,
567 .priority = -1
568};
569
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900570/***********************************************************************
571 * PROC DIR HANDLING
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 ***********************************************************************/
573
574#ifdef CONFIG_PROC_FS
575
KOVACS Krisztian136e92b2005-09-16 17:00:04 -0700576struct clusterip_seq_position {
577 unsigned int pos; /* position */
578 unsigned int weight; /* number of bits set == size */
579 unsigned int bit; /* current bit */
580 unsigned long val; /* current value */
581};
582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583static void *clusterip_seq_start(struct seq_file *s, loff_t *pos)
584{
Alexey Dobriyan47778142010-01-22 22:21:18 +0100585 struct clusterip_config *c = s->private;
KOVACS Krisztian136e92b2005-09-16 17:00:04 -0700586 unsigned int weight;
587 u_int32_t local_nodes;
588 struct clusterip_seq_position *idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
KOVACS Krisztian136e92b2005-09-16 17:00:04 -0700590 /* FIXME: possible race */
591 local_nodes = c->local_nodes;
592 weight = hweight32(local_nodes);
593 if (*pos >= weight)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 return NULL;
595
KOVACS Krisztian136e92b2005-09-16 17:00:04 -0700596 idx = kmalloc(sizeof(struct clusterip_seq_position), GFP_KERNEL);
597 if (!idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 return ERR_PTR(-ENOMEM);
599
KOVACS Krisztian136e92b2005-09-16 17:00:04 -0700600 idx->pos = *pos;
601 idx->weight = weight;
602 idx->bit = ffs(local_nodes);
603 idx->val = local_nodes;
604 clear_bit(idx->bit - 1, &idx->val);
605
606 return idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607}
608
609static void *clusterip_seq_next(struct seq_file *s, void *v, loff_t *pos)
610{
Jan Engelhardt3cf93c92008-04-14 09:56:05 +0200611 struct clusterip_seq_position *idx = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
KOVACS Krisztian136e92b2005-09-16 17:00:04 -0700613 *pos = ++idx->pos;
614 if (*pos >= idx->weight) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 kfree(v);
616 return NULL;
617 }
KOVACS Krisztian136e92b2005-09-16 17:00:04 -0700618 idx->bit = ffs(idx->val);
619 clear_bit(idx->bit - 1, &idx->val);
620 return idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621}
622
623static void clusterip_seq_stop(struct seq_file *s, void *v)
624{
Eric Dumazet902a3dd2010-04-01 12:54:09 +0200625 if (!IS_ERR(v))
626 kfree(v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627}
628
629static int clusterip_seq_show(struct seq_file *s, void *v)
630{
Jan Engelhardt3cf93c92008-04-14 09:56:05 +0200631 struct clusterip_seq_position *idx = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900633 if (idx->pos != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 seq_putc(s, ',');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
KOVACS Krisztian136e92b2005-09-16 17:00:04 -0700636 seq_printf(s, "%u", idx->bit);
637
638 if (idx->pos == idx->weight - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 seq_putc(s, '\n');
640
641 return 0;
642}
643
Philippe De Muyter56b3d972007-07-10 23:07:31 -0700644static const struct seq_operations clusterip_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 .start = clusterip_seq_start,
646 .next = clusterip_seq_next,
647 .stop = clusterip_seq_stop,
648 .show = clusterip_seq_show,
649};
650
651static int clusterip_proc_open(struct inode *inode, struct file *file)
652{
653 int ret = seq_open(file, &clusterip_seq_ops);
654
655 if (!ret) {
656 struct seq_file *sf = file->private_data;
Al Virod9dda782013-03-31 18:16:14 -0400657 struct clusterip_config *c = PDE_DATA(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Alexey Dobriyan47778142010-01-22 22:21:18 +0100659 sf->private = c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661 clusterip_config_get(c);
662 }
663
664 return ret;
665}
666
667static int clusterip_proc_release(struct inode *inode, struct file *file)
668{
Al Virod9dda782013-03-31 18:16:14 -0400669 struct clusterip_config *c = PDE_DATA(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 int ret;
671
672 ret = seq_release(inode, file);
673
674 if (!ret)
675 clusterip_config_put(c);
676
677 return ret;
678}
679
680static ssize_t clusterip_proc_write(struct file *file, const char __user *input,
681 size_t size, loff_t *ofs)
682{
Al Virod9dda782013-03-31 18:16:14 -0400683 struct clusterip_config *c = PDE_DATA(file_inode(file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684#define PROC_WRITELEN 10
685 char buffer[PROC_WRITELEN+1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 unsigned long nodenum;
Abhijit Pawar4b5511e2012-12-09 23:12:28 +0000687 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Vasiliy Kulikov961ed182011-03-20 15:42:52 +0100689 if (size > PROC_WRITELEN)
690 return -EIO;
691 if (copy_from_user(buffer, input, size))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 return -EFAULT;
Vasiliy Kulikov961ed182011-03-20 15:42:52 +0100693 buffer[size] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 if (*buffer == '+') {
Abhijit Pawar4b5511e2012-12-09 23:12:28 +0000696 rc = kstrtoul(buffer+1, 10, &nodenum);
697 if (rc)
698 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 if (clusterip_add_node(c, nodenum))
700 return -ENOMEM;
701 } else if (*buffer == '-') {
Abhijit Pawar4b5511e2012-12-09 23:12:28 +0000702 rc = kstrtoul(buffer+1, 10, &nodenum);
703 if (rc)
704 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 if (clusterip_del_node(c, nodenum))
706 return -ENOENT;
707 } else
708 return -EIO;
709
710 return size;
711}
712
Arjan van de Ven9a321442007-02-12 00:55:35 -0800713static const struct file_operations clusterip_proc_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 .owner = THIS_MODULE,
715 .open = clusterip_proc_open,
716 .read = seq_read,
717 .write = clusterip_proc_write,
718 .llseek = seq_lseek,
719 .release = clusterip_proc_release,
720};
721
722#endif /* CONFIG_PROC_FS */
723
Gao fengce4ff762013-09-25 15:38:44 +0800724static int clusterip_net_init(struct net *net)
725{
Gao fengce4ff762013-09-25 15:38:44 +0800726 struct clusterip_net *cn = net_generic(net, clusterip_net_id);
727
Gao feng26a89e42013-09-25 15:38:45 +0800728 INIT_LIST_HEAD(&cn->configs);
729
Gao fengf1e80772013-09-25 15:38:46 +0800730 spin_lock_init(&cn->lock);
731
Gao feng26a89e42013-09-25 15:38:45 +0800732#ifdef CONFIG_PROC_FS
Gao fengce4ff762013-09-25 15:38:44 +0800733 cn->procdir = proc_mkdir("ipt_CLUSTERIP", net->proc_net);
734 if (!cn->procdir) {
735 pr_err("Unable to proc dir entry\n");
736 return -ENOMEM;
737 }
738#endif /* CONFIG_PROC_FS */
739
740 return 0;
741}
742
743static void clusterip_net_exit(struct net *net)
744{
745#ifdef CONFIG_PROC_FS
746 struct clusterip_net *cn = net_generic(net, clusterip_net_id);
747 proc_remove(cn->procdir);
748#endif
749}
750
751static struct pernet_operations clusterip_net_ops = {
752 .init = clusterip_net_init,
753 .exit = clusterip_net_exit,
754 .id = &clusterip_net_id,
755 .size = sizeof(struct clusterip_net),
756};
757
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800758static int __init clusterip_tg_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759{
760 int ret;
761
Gao fengce4ff762013-09-25 15:38:44 +0800762 ret = register_pernet_subsys(&clusterip_net_ops);
Patrick McHardy32292a72006-04-06 14:11:30 -0700763 if (ret < 0)
764 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Gao fengce4ff762013-09-25 15:38:44 +0800766 ret = xt_register_target(&clusterip_tg_reg);
767 if (ret < 0)
768 goto cleanup_subsys;
769
Patrick McHardy32292a72006-04-06 14:11:30 -0700770 ret = nf_register_hook(&cip_arp_ops);
771 if (ret < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 goto cleanup_target;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Jan Engelhardtff67e4e2010-03-19 21:08:16 +0100774 pr_info("ClusterIP Version %s loaded successfully\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 CLUSTERIP_VERSION);
Gao fengce4ff762013-09-25 15:38:44 +0800776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 return 0;
778
Patrick McHardy32292a72006-04-06 14:11:30 -0700779cleanup_target:
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800780 xt_unregister_target(&clusterip_tg_reg);
Gao fengce4ff762013-09-25 15:38:44 +0800781cleanup_subsys:
782 unregister_pernet_subsys(&clusterip_net_ops);
Patrick McHardy32292a72006-04-06 14:11:30 -0700783 return ret;
784}
785
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800786static void __exit clusterip_tg_exit(void)
Patrick McHardy32292a72006-04-06 14:11:30 -0700787{
Jan Engelhardtff67e4e2010-03-19 21:08:16 +0100788 pr_info("ClusterIP Version %s unloading\n", CLUSTERIP_VERSION);
Gao fengce4ff762013-09-25 15:38:44 +0800789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 nf_unregister_hook(&cip_arp_ops);
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800791 xt_unregister_target(&clusterip_tg_reg);
Gao fengce4ff762013-09-25 15:38:44 +0800792 unregister_pernet_subsys(&clusterip_net_ops);
Eric Dumazetd73f33b2010-06-15 13:08:51 +0200793
794 /* Wait for completion of call_rcu_bh()'s (clusterip_config_rcu_free) */
795 rcu_barrier_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796}
797
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800798module_init(clusterip_tg_init);
799module_exit(clusterip_tg_exit);