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