blob: f4af1bfafb1c61642ddb56ac490f7e161df5d39d [file] [log] [blame]
Pablo Neira Ayuso0269ea42009-03-16 17:10:36 +01001/*
2 * (C) 2008-2009 Pablo Neira Ayuso <pablo@netfilter.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
Jan Engelhardt8bee4ba2010-03-17 16:04:40 +01008#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Pablo Neira Ayuso0269ea42009-03-16 17:10:36 +01009#include <linux/module.h>
10#include <linux/skbuff.h>
11#include <linux/jhash.h>
12#include <linux/ip.h>
13#include <net/ipv6.h>
14
15#include <linux/netfilter/x_tables.h>
16#include <net/netfilter/nf_conntrack.h>
17#include <linux/netfilter/xt_cluster.h>
18
Patrick McHardyf9ffc3122009-06-22 14:15:02 +020019static inline u32 nf_ct_orig_ipv4_src(const struct nf_conn *ct)
Pablo Neira Ayuso0269ea42009-03-16 17:10:36 +010020{
Patrick McHardyf9ffc3122009-06-22 14:15:02 +020021 return (__force u32)ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.ip;
Pablo Neira Ayuso0269ea42009-03-16 17:10:36 +010022}
23
Patrick McHardyf9ffc3122009-06-22 14:15:02 +020024static inline const u32 *nf_ct_orig_ipv6_src(const struct nf_conn *ct)
Pablo Neira Ayuso0269ea42009-03-16 17:10:36 +010025{
Patrick McHardyf9ffc3122009-06-22 14:15:02 +020026 return (__force u32 *)ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.ip6;
Pablo Neira Ayuso0269ea42009-03-16 17:10:36 +010027}
28
29static inline u_int32_t
30xt_cluster_hash_ipv4(u_int32_t ip, const struct xt_cluster_match_info *info)
31{
32 return jhash_1word(ip, info->hash_seed);
33}
34
35static inline u_int32_t
36xt_cluster_hash_ipv6(const void *ip, const struct xt_cluster_match_info *info)
37{
38 return jhash2(ip, NF_CT_TUPLE_L3SIZE / sizeof(__u32), info->hash_seed);
39}
40
41static inline u_int32_t
42xt_cluster_hash(const struct nf_conn *ct,
43 const struct xt_cluster_match_info *info)
44{
45 u_int32_t hash = 0;
46
47 switch(nf_ct_l3num(ct)) {
48 case AF_INET:
49 hash = xt_cluster_hash_ipv4(nf_ct_orig_ipv4_src(ct), info);
50 break;
51 case AF_INET6:
52 hash = xt_cluster_hash_ipv6(nf_ct_orig_ipv6_src(ct), info);
53 break;
54 default:
55 WARN_ON(1);
56 break;
57 }
58 return (((u64)hash * info->total_nodes) >> 32);
59}
60
61static inline bool
Pablo Neira Ayuso424b86a2009-03-29 13:46:01 -070062xt_cluster_ipv6_is_multicast(const struct in6_addr *addr)
63{
64 __be32 st = addr->s6_addr32[0];
65 return ((st & htonl(0xFF000000)) == htonl(0xFF000000));
66}
67
68static inline bool
Pablo Neira Ayuso0269ea42009-03-16 17:10:36 +010069xt_cluster_is_multicast_addr(const struct sk_buff *skb, u_int8_t family)
70{
71 bool is_multicast = false;
72
73 switch(family) {
74 case NFPROTO_IPV4:
75 is_multicast = ipv4_is_multicast(ip_hdr(skb)->daddr);
76 break;
77 case NFPROTO_IPV6:
Pablo Neira Ayuso424b86a2009-03-29 13:46:01 -070078 is_multicast =
79 xt_cluster_ipv6_is_multicast(&ipv6_hdr(skb)->daddr);
Pablo Neira Ayuso0269ea42009-03-16 17:10:36 +010080 break;
81 default:
82 WARN_ON(1);
83 break;
84 }
85 return is_multicast;
86}
87
88static bool
Jan Engelhardt62fc8052009-07-07 20:42:08 +020089xt_cluster_mt(const struct sk_buff *skb, struct xt_action_param *par)
Pablo Neira Ayuso0269ea42009-03-16 17:10:36 +010090{
91 struct sk_buff *pskb = (struct sk_buff *)skb;
92 const struct xt_cluster_match_info *info = par->matchinfo;
93 const struct nf_conn *ct;
94 enum ip_conntrack_info ctinfo;
95 unsigned long hash;
96
97 /* This match assumes that all nodes see the same packets. This can be
98 * achieved if the switch that connects the cluster nodes support some
99 * sort of 'port mirroring'. However, if your switch does not support
100 * this, your cluster nodes can reply ARP request using a multicast MAC
101 * address. Thus, your switch will flood the same packets to the
102 * cluster nodes with the same multicast MAC address. Using a multicast
103 * link address is a RFC 1812 (section 3.3.2) violation, but this works
104 * fine in practise.
105 *
106 * Unfortunately, if you use the multicast MAC address, the link layer
107 * sets skbuff's pkt_type to PACKET_MULTICAST, which is not accepted
108 * by TCP and others for packets coming to this node. For that reason,
109 * this match mangles skbuff's pkt_type if it detects a packet
110 * addressed to a unicast address but using PACKET_MULTICAST. Yes, I
111 * know, matches should not alter packets, but we are doing this here
112 * because we would need to add a PKTTYPE target for this sole purpose.
113 */
114 if (!xt_cluster_is_multicast_addr(skb, par->family) &&
115 skb->pkt_type == PACKET_MULTICAST) {
116 pskb->pkt_type = PACKET_HOST;
117 }
118
119 ct = nf_ct_get(skb, &ctinfo);
120 if (ct == NULL)
121 return false;
122
Eric Dumazet5bfddbd2010-06-08 16:09:52 +0200123 if (nf_ct_is_untracked(ct))
Pablo Neira Ayuso0269ea42009-03-16 17:10:36 +0100124 return false;
125
126 if (ct->master)
127 hash = xt_cluster_hash(ct->master, info);
128 else
129 hash = xt_cluster_hash(ct, info);
130
131 return !!((1 << hash) & info->node_mask) ^
132 !!(info->flags & XT_CLUSTER_F_INV);
133}
134
Jan Engelhardtb0f38452010-03-19 17:16:42 +0100135static int xt_cluster_mt_checkentry(const struct xt_mtchk_param *par)
Pablo Neira Ayuso0269ea42009-03-16 17:10:36 +0100136{
137 struct xt_cluster_match_info *info = par->matchinfo;
138
Pablo Neira Ayuso280f37a2009-05-05 17:46:07 +0200139 if (info->total_nodes > XT_CLUSTER_NODES_MAX) {
Jan Engelhardt8bee4ba2010-03-17 16:04:40 +0100140 pr_info("you have exceeded the maximum "
141 "number of cluster nodes (%u > %u)\n",
142 info->total_nodes, XT_CLUSTER_NODES_MAX);
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100143 return -EINVAL;
Pablo Neira Ayuso280f37a2009-05-05 17:46:07 +0200144 }
145 if (info->node_mask >= (1ULL << info->total_nodes)) {
Jan Engelhardt8bee4ba2010-03-17 16:04:40 +0100146 pr_info("this node mask cannot be "
147 "higher than the total number of nodes\n");
Jan Engelhardt4a5a5c72010-03-19 17:32:59 +0100148 return -EDOM;
Pablo Neira Ayuso0269ea42009-03-16 17:10:36 +0100149 }
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100150 return 0;
Pablo Neira Ayuso0269ea42009-03-16 17:10:36 +0100151}
152
153static struct xt_match xt_cluster_match __read_mostly = {
154 .name = "cluster",
155 .family = NFPROTO_UNSPEC,
156 .match = xt_cluster_mt,
157 .checkentry = xt_cluster_mt_checkentry,
158 .matchsize = sizeof(struct xt_cluster_match_info),
159 .me = THIS_MODULE,
160};
161
162static int __init xt_cluster_mt_init(void)
163{
164 return xt_register_match(&xt_cluster_match);
165}
166
167static void __exit xt_cluster_mt_fini(void)
168{
169 xt_unregister_match(&xt_cluster_match);
170}
171
172MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
173MODULE_LICENSE("GPL");
174MODULE_DESCRIPTION("Xtables: hash-based cluster match");
175MODULE_ALIAS("ipt_cluster");
176MODULE_ALIAS("ip6t_cluster");
177module_init(xt_cluster_mt_init);
178module_exit(xt_cluster_mt_fini);