~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/ipv4/netfilter/ipt_CLUSTERIP.c

Version: ~ [ linux-5.5-rc7 ] ~ [ linux-5.4.13 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.97 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.166 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.210 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.210 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.81 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp