netfilter: nf_conntrack: fix conntrack lookup race
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / net / netfilter / xt_cluster.c
CommitLineData
0269ea49
PNA
1/*
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 */
8#include <linux/module.h>
9#include <linux/skbuff.h>
10#include <linux/jhash.h>
11#include <linux/ip.h>
12#include <net/ipv6.h>
13
14#include <linux/netfilter/x_tables.h>
15#include <net/netfilter/nf_conntrack.h>
16#include <linux/netfilter/xt_cluster.h>
17
18static inline u_int32_t nf_ct_orig_ipv4_src(const struct nf_conn *ct)
19{
20 return ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.ip;
21}
22
23static inline const void *nf_ct_orig_ipv6_src(const struct nf_conn *ct)
24{
25 return ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.u3.ip6;
26}
27
28static inline u_int32_t
29xt_cluster_hash_ipv4(u_int32_t ip, const struct xt_cluster_match_info *info)
30{
31 return jhash_1word(ip, info->hash_seed);
32}
33
34static inline u_int32_t
35xt_cluster_hash_ipv6(const void *ip, const struct xt_cluster_match_info *info)
36{
37 return jhash2(ip, NF_CT_TUPLE_L3SIZE / sizeof(__u32), info->hash_seed);
38}
39
40static inline u_int32_t
41xt_cluster_hash(const struct nf_conn *ct,
42 const struct xt_cluster_match_info *info)
43{
44 u_int32_t hash = 0;
45
46 switch(nf_ct_l3num(ct)) {
47 case AF_INET:
48 hash = xt_cluster_hash_ipv4(nf_ct_orig_ipv4_src(ct), info);
49 break;
50 case AF_INET6:
51 hash = xt_cluster_hash_ipv6(nf_ct_orig_ipv6_src(ct), info);
52 break;
53 default:
54 WARN_ON(1);
55 break;
56 }
57 return (((u64)hash * info->total_nodes) >> 32);
58}
59
424b86a6
PNA
60static inline bool
61xt_cluster_ipv6_is_multicast(const struct in6_addr *addr)
62{
63 __be32 st = addr->s6_addr32[0];
64 return ((st & htonl(0xFF000000)) == htonl(0xFF000000));
65}
66
0269ea49
PNA
67static inline bool
68xt_cluster_is_multicast_addr(const struct sk_buff *skb, u_int8_t family)
69{
70 bool is_multicast = false;
71
72 switch(family) {
73 case NFPROTO_IPV4:
74 is_multicast = ipv4_is_multicast(ip_hdr(skb)->daddr);
75 break;
76 case NFPROTO_IPV6:
424b86a6
PNA
77 is_multicast =
78 xt_cluster_ipv6_is_multicast(&ipv6_hdr(skb)->daddr);
0269ea49
PNA
79 break;
80 default:
81 WARN_ON(1);
82 break;
83 }
84 return is_multicast;
85}
86
87static bool
88xt_cluster_mt(const struct sk_buff *skb, const struct xt_match_param *par)
89{
90 struct sk_buff *pskb = (struct sk_buff *)skb;
91 const struct xt_cluster_match_info *info = par->matchinfo;
92 const struct nf_conn *ct;
93 enum ip_conntrack_info ctinfo;
94 unsigned long hash;
95
96 /* This match assumes that all nodes see the same packets. This can be
97 * achieved if the switch that connects the cluster nodes support some
98 * sort of 'port mirroring'. However, if your switch does not support
99 * this, your cluster nodes can reply ARP request using a multicast MAC
100 * address. Thus, your switch will flood the same packets to the
101 * cluster nodes with the same multicast MAC address. Using a multicast
102 * link address is a RFC 1812 (section 3.3.2) violation, but this works
103 * fine in practise.
104 *
105 * Unfortunately, if you use the multicast MAC address, the link layer
106 * sets skbuff's pkt_type to PACKET_MULTICAST, which is not accepted
107 * by TCP and others for packets coming to this node. For that reason,
108 * this match mangles skbuff's pkt_type if it detects a packet
109 * addressed to a unicast address but using PACKET_MULTICAST. Yes, I
110 * know, matches should not alter packets, but we are doing this here
111 * because we would need to add a PKTTYPE target for this sole purpose.
112 */
113 if (!xt_cluster_is_multicast_addr(skb, par->family) &&
114 skb->pkt_type == PACKET_MULTICAST) {
115 pskb->pkt_type = PACKET_HOST;
116 }
117
118 ct = nf_ct_get(skb, &ctinfo);
119 if (ct == NULL)
120 return false;
121
122 if (ct == &nf_conntrack_untracked)
123 return false;
124
125 if (ct->master)
126 hash = xt_cluster_hash(ct->master, info);
127 else
128 hash = xt_cluster_hash(ct, info);
129
130 return !!((1 << hash) & info->node_mask) ^
131 !!(info->flags & XT_CLUSTER_F_INV);
132}
133
134static bool xt_cluster_mt_checkentry(const struct xt_mtchk_param *par)
135{
136 struct xt_cluster_match_info *info = par->matchinfo;
137
280f37af
PNA
138 if (info->total_nodes > XT_CLUSTER_NODES_MAX) {
139 printk(KERN_ERR "xt_cluster: you have exceeded the maximum "
140 "number of cluster nodes (%u > %u)\n",
141 info->total_nodes, XT_CLUSTER_NODES_MAX);
142 return false;
143 }
144 if (info->node_mask >= (1ULL << info->total_nodes)) {
0269ea49
PNA
145 printk(KERN_ERR "xt_cluster: this node mask cannot be "
146 "higher than the total number of nodes\n");
147 return false;
148 }
149 return true;
150}
151
152static struct xt_match xt_cluster_match __read_mostly = {
153 .name = "cluster",
154 .family = NFPROTO_UNSPEC,
155 .match = xt_cluster_mt,
156 .checkentry = xt_cluster_mt_checkentry,
157 .matchsize = sizeof(struct xt_cluster_match_info),
158 .me = THIS_MODULE,
159};
160
161static int __init xt_cluster_mt_init(void)
162{
163 return xt_register_match(&xt_cluster_match);
164}
165
166static void __exit xt_cluster_mt_fini(void)
167{
168 xt_unregister_match(&xt_cluster_match);
169}
170
171MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
172MODULE_LICENSE("GPL");
173MODULE_DESCRIPTION("Xtables: hash-based cluster match");
174MODULE_ALIAS("ipt_cluster");
175MODULE_ALIAS("ip6t_cluster");
176module_init(xt_cluster_mt_init);
177module_exit(xt_cluster_mt_fini);