xref: /openbmc/linux/net/netfilter/xt_connlimit.c (revision e190bfe5)
1 /*
2  * netfilter module to limit the number of parallel tcp
3  * connections per IP address.
4  *   (c) 2000 Gerd Knorr <kraxel@bytesex.org>
5  *   Nov 2002: Martin Bene <martin.bene@icomedias.com>:
6  *		only ignore TIME_WAIT or gone connections
7  *   (C) CC Computer Consultants GmbH, 2007
8  *
9  * based on ...
10  *
11  * Kernel module to match connection tracking information.
12  * GPL (C) 1999  Rusty Russell (rusty@rustcorp.com.au).
13  */
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/in.h>
16 #include <linux/in6.h>
17 #include <linux/ip.h>
18 #include <linux/ipv6.h>
19 #include <linux/jhash.h>
20 #include <linux/slab.h>
21 #include <linux/list.h>
22 #include <linux/module.h>
23 #include <linux/random.h>
24 #include <linux/skbuff.h>
25 #include <linux/spinlock.h>
26 #include <linux/netfilter/nf_conntrack_tcp.h>
27 #include <linux/netfilter/x_tables.h>
28 #include <linux/netfilter/xt_connlimit.h>
29 #include <net/netfilter/nf_conntrack.h>
30 #include <net/netfilter/nf_conntrack_core.h>
31 #include <net/netfilter/nf_conntrack_tuple.h>
32 #include <net/netfilter/nf_conntrack_zones.h>
33 
34 /* we will save the tuples of all connections we care about */
35 struct xt_connlimit_conn {
36 	struct list_head list;
37 	struct nf_conntrack_tuple tuple;
38 };
39 
40 struct xt_connlimit_data {
41 	struct list_head iphash[256];
42 	spinlock_t lock;
43 };
44 
45 static u_int32_t connlimit_rnd __read_mostly;
46 static bool connlimit_rnd_inited __read_mostly;
47 
48 static inline unsigned int connlimit_iphash(__be32 addr)
49 {
50 	return jhash_1word((__force __u32)addr, connlimit_rnd) & 0xFF;
51 }
52 
53 static inline unsigned int
54 connlimit_iphash6(const union nf_inet_addr *addr,
55                   const union nf_inet_addr *mask)
56 {
57 	union nf_inet_addr res;
58 	unsigned int i;
59 
60 	for (i = 0; i < ARRAY_SIZE(addr->ip6); ++i)
61 		res.ip6[i] = addr->ip6[i] & mask->ip6[i];
62 
63 	return jhash2((u32 *)res.ip6, ARRAY_SIZE(res.ip6), connlimit_rnd) & 0xFF;
64 }
65 
66 static inline bool already_closed(const struct nf_conn *conn)
67 {
68 	if (nf_ct_protonum(conn) == IPPROTO_TCP)
69 		return conn->proto.tcp.state == TCP_CONNTRACK_TIME_WAIT ||
70 		       conn->proto.tcp.state == TCP_CONNTRACK_CLOSE;
71 	else
72 		return 0;
73 }
74 
75 static inline unsigned int
76 same_source_net(const union nf_inet_addr *addr,
77 		const union nf_inet_addr *mask,
78 		const union nf_inet_addr *u3, u_int8_t family)
79 {
80 	if (family == NFPROTO_IPV4) {
81 		return (addr->ip & mask->ip) == (u3->ip & mask->ip);
82 	} else {
83 		union nf_inet_addr lh, rh;
84 		unsigned int i;
85 
86 		for (i = 0; i < ARRAY_SIZE(addr->ip6); ++i) {
87 			lh.ip6[i] = addr->ip6[i] & mask->ip6[i];
88 			rh.ip6[i] = u3->ip6[i] & mask->ip6[i];
89 		}
90 
91 		return memcmp(&lh.ip6, &rh.ip6, sizeof(lh.ip6)) == 0;
92 	}
93 }
94 
95 static int count_them(struct net *net,
96 		      struct xt_connlimit_data *data,
97 		      const struct nf_conntrack_tuple *tuple,
98 		      const union nf_inet_addr *addr,
99 		      const union nf_inet_addr *mask,
100 		      u_int8_t family)
101 {
102 	const struct nf_conntrack_tuple_hash *found;
103 	struct xt_connlimit_conn *conn;
104 	struct xt_connlimit_conn *tmp;
105 	struct nf_conn *found_ct;
106 	struct list_head *hash;
107 	bool addit = true;
108 	int matches = 0;
109 
110 	if (family == NFPROTO_IPV6)
111 		hash = &data->iphash[connlimit_iphash6(addr, mask)];
112 	else
113 		hash = &data->iphash[connlimit_iphash(addr->ip & mask->ip)];
114 
115 	rcu_read_lock();
116 
117 	/* check the saved connections */
118 	list_for_each_entry_safe(conn, tmp, hash, list) {
119 		found    = nf_conntrack_find_get(net, NF_CT_DEFAULT_ZONE,
120 						 &conn->tuple);
121 		found_ct = NULL;
122 
123 		if (found != NULL)
124 			found_ct = nf_ct_tuplehash_to_ctrack(found);
125 
126 		if (found_ct != NULL &&
127 		    nf_ct_tuple_equal(&conn->tuple, tuple) &&
128 		    !already_closed(found_ct))
129 			/*
130 			 * Just to be sure we have it only once in the list.
131 			 * We should not see tuples twice unless someone hooks
132 			 * this into a table without "-p tcp --syn".
133 			 */
134 			addit = false;
135 
136 		if (found == NULL) {
137 			/* this one is gone */
138 			list_del(&conn->list);
139 			kfree(conn);
140 			continue;
141 		}
142 
143 		if (already_closed(found_ct)) {
144 			/*
145 			 * we do not care about connections which are
146 			 * closed already -> ditch it
147 			 */
148 			nf_ct_put(found_ct);
149 			list_del(&conn->list);
150 			kfree(conn);
151 			continue;
152 		}
153 
154 		if (same_source_net(addr, mask, &conn->tuple.src.u3, family))
155 			/* same source network -> be counted! */
156 			++matches;
157 		nf_ct_put(found_ct);
158 	}
159 
160 	rcu_read_unlock();
161 
162 	if (addit) {
163 		/* save the new connection in our list */
164 		conn = kzalloc(sizeof(*conn), GFP_ATOMIC);
165 		if (conn == NULL)
166 			return -ENOMEM;
167 		conn->tuple = *tuple;
168 		list_add(&conn->list, hash);
169 		++matches;
170 	}
171 
172 	return matches;
173 }
174 
175 static bool
176 connlimit_mt(const struct sk_buff *skb, struct xt_action_param *par)
177 {
178 	struct net *net = dev_net(par->in ? par->in : par->out);
179 	const struct xt_connlimit_info *info = par->matchinfo;
180 	union nf_inet_addr addr;
181 	struct nf_conntrack_tuple tuple;
182 	const struct nf_conntrack_tuple *tuple_ptr = &tuple;
183 	enum ip_conntrack_info ctinfo;
184 	const struct nf_conn *ct;
185 	int connections;
186 
187 	ct = nf_ct_get(skb, &ctinfo);
188 	if (ct != NULL)
189 		tuple_ptr = &ct->tuplehash[0].tuple;
190 	else if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb),
191 				    par->family, &tuple))
192 		goto hotdrop;
193 
194 	if (par->family == NFPROTO_IPV6) {
195 		const struct ipv6hdr *iph = ipv6_hdr(skb);
196 		memcpy(&addr.ip6, &iph->saddr, sizeof(iph->saddr));
197 	} else {
198 		const struct iphdr *iph = ip_hdr(skb);
199 		addr.ip = iph->saddr;
200 	}
201 
202 	spin_lock_bh(&info->data->lock);
203 	connections = count_them(net, info->data, tuple_ptr, &addr,
204 	                         &info->mask, par->family);
205 	spin_unlock_bh(&info->data->lock);
206 
207 	if (connections < 0) {
208 		/* kmalloc failed, drop it entirely */
209 		par->hotdrop = true;
210 		return false;
211 	}
212 
213 	return (connections > info->limit) ^ info->inverse;
214 
215  hotdrop:
216 	par->hotdrop = true;
217 	return false;
218 }
219 
220 static int connlimit_mt_check(const struct xt_mtchk_param *par)
221 {
222 	struct xt_connlimit_info *info = par->matchinfo;
223 	unsigned int i;
224 	int ret;
225 
226 	if (unlikely(!connlimit_rnd_inited)) {
227 		get_random_bytes(&connlimit_rnd, sizeof(connlimit_rnd));
228 		connlimit_rnd_inited = true;
229 	}
230 	ret = nf_ct_l3proto_try_module_get(par->family);
231 	if (ret < 0) {
232 		pr_info("cannot load conntrack support for "
233 			"address family %u\n", par->family);
234 		return ret;
235 	}
236 
237 	/* init private data */
238 	info->data = kmalloc(sizeof(struct xt_connlimit_data), GFP_KERNEL);
239 	if (info->data == NULL) {
240 		nf_ct_l3proto_module_put(par->family);
241 		return -ENOMEM;
242 	}
243 
244 	spin_lock_init(&info->data->lock);
245 	for (i = 0; i < ARRAY_SIZE(info->data->iphash); ++i)
246 		INIT_LIST_HEAD(&info->data->iphash[i]);
247 
248 	return 0;
249 }
250 
251 static void connlimit_mt_destroy(const struct xt_mtdtor_param *par)
252 {
253 	const struct xt_connlimit_info *info = par->matchinfo;
254 	struct xt_connlimit_conn *conn;
255 	struct xt_connlimit_conn *tmp;
256 	struct list_head *hash = info->data->iphash;
257 	unsigned int i;
258 
259 	nf_ct_l3proto_module_put(par->family);
260 
261 	for (i = 0; i < ARRAY_SIZE(info->data->iphash); ++i) {
262 		list_for_each_entry_safe(conn, tmp, &hash[i], list) {
263 			list_del(&conn->list);
264 			kfree(conn);
265 		}
266 	}
267 
268 	kfree(info->data);
269 }
270 
271 static struct xt_match connlimit_mt_reg __read_mostly = {
272 	.name       = "connlimit",
273 	.revision   = 0,
274 	.family     = NFPROTO_UNSPEC,
275 	.checkentry = connlimit_mt_check,
276 	.match      = connlimit_mt,
277 	.matchsize  = sizeof(struct xt_connlimit_info),
278 	.destroy    = connlimit_mt_destroy,
279 	.me         = THIS_MODULE,
280 };
281 
282 static int __init connlimit_mt_init(void)
283 {
284 	return xt_register_match(&connlimit_mt_reg);
285 }
286 
287 static void __exit connlimit_mt_exit(void)
288 {
289 	xt_unregister_match(&connlimit_mt_reg);
290 }
291 
292 module_init(connlimit_mt_init);
293 module_exit(connlimit_mt_exit);
294 MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>");
295 MODULE_DESCRIPTION("Xtables: Number of connections matching");
296 MODULE_LICENSE("GPL");
297 MODULE_ALIAS("ipt_connlimit");
298 MODULE_ALIAS("ip6t_connlimit");
299