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