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