2007-07-15 05:47:26 +02:00
|
|
|
/*
|
|
|
|
* netfilter module to limit the number of parallel tcp
|
|
|
|
* connections per IP address.
|
|
|
|
* (c) 2000 Gerd Knorr <kraxel@bytesex.org>
|
|
|
|
* Nov 2002: Martin Bene <martin.bene@icomedias.com>:
|
|
|
|
* only ignore TIME_WAIT or gone connections
|
2007-11-06 05:35:56 +01:00
|
|
|
* (C) CC Computer Consultants GmbH, 2007
|
2007-07-15 05:47:26 +02:00
|
|
|
*
|
|
|
|
* based on ...
|
|
|
|
*
|
|
|
|
* Kernel module to match connection tracking information.
|
|
|
|
* GPL (C) 1999 Rusty Russell (rusty@rustcorp.com.au).
|
|
|
|
*/
|
2010-03-17 16:04:40 +01:00
|
|
|
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
2017-12-09 21:01:08 +01:00
|
|
|
|
2007-07-15 05:47:26 +02:00
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/skbuff.h>
|
|
|
|
#include <linux/netfilter/x_tables.h>
|
|
|
|
#include <linux/netfilter/xt_connlimit.h>
|
2017-12-09 21:01:08 +01:00
|
|
|
|
2007-07-15 05:47:26 +02:00
|
|
|
#include <net/netfilter/nf_conntrack.h>
|
|
|
|
#include <net/netfilter/nf_conntrack_core.h>
|
|
|
|
#include <net/netfilter/nf_conntrack_tuple.h>
|
2010-02-15 18:13:33 +01:00
|
|
|
#include <net/netfilter/nf_conntrack_zones.h>
|
2017-12-09 21:01:08 +01:00
|
|
|
#include <net/netfilter/nf_conntrack_count.h>
|
2014-03-07 14:37:09 +01:00
|
|
|
|
2007-12-05 08:24:03 +01:00
|
|
|
static bool
|
2009-07-07 20:42:08 +02:00
|
|
|
connlimit_mt(const struct sk_buff *skb, struct xt_action_param *par)
|
2007-07-15 05:47:26 +02:00
|
|
|
{
|
2016-11-03 10:56:21 +01:00
|
|
|
struct net *net = xt_net(par);
|
2008-10-08 11:35:18 +02:00
|
|
|
const struct xt_connlimit_info *info = par->matchinfo;
|
2007-07-15 05:47:26 +02:00
|
|
|
struct nf_conntrack_tuple tuple;
|
|
|
|
const struct nf_conntrack_tuple *tuple_ptr = &tuple;
|
2015-08-08 21:40:01 +02:00
|
|
|
const struct nf_conntrack_zone *zone = &nf_ct_zone_dflt;
|
2007-07-15 05:47:26 +02:00
|
|
|
enum ip_conntrack_info ctinfo;
|
|
|
|
const struct nf_conn *ct;
|
2014-03-12 23:49:51 +01:00
|
|
|
unsigned int connections;
|
2017-12-09 21:01:08 +01:00
|
|
|
u32 key[5];
|
2007-07-15 05:47:26 +02:00
|
|
|
|
|
|
|
ct = nf_ct_get(skb, &ctinfo);
|
2014-11-14 13:21:48 +01:00
|
|
|
if (ct != NULL) {
|
2011-03-15 13:23:28 +01:00
|
|
|
tuple_ptr = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
|
2014-11-14 13:21:48 +01:00
|
|
|
zone = nf_ct_zone(ct);
|
|
|
|
} else if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb),
|
2016-11-03 10:56:21 +01:00
|
|
|
xt_family(par), net, &tuple)) {
|
2007-07-15 05:47:26 +02:00
|
|
|
goto hotdrop;
|
2014-11-14 13:21:48 +01:00
|
|
|
}
|
2007-07-15 05:47:26 +02:00
|
|
|
|
2016-11-03 10:56:21 +01:00
|
|
|
if (xt_family(par) == NFPROTO_IPV6) {
|
2007-07-15 05:47:26 +02:00
|
|
|
const struct ipv6hdr *iph = ipv6_hdr(skb);
|
2017-12-09 21:01:08 +01:00
|
|
|
union nf_inet_addr addr;
|
2017-11-02 12:50:38 +01:00
|
|
|
unsigned int i;
|
|
|
|
|
2011-01-18 17:32:40 +01:00
|
|
|
memcpy(&addr.ip6, (info->flags & XT_CONNLIMIT_DADDR) ?
|
|
|
|
&iph->daddr : &iph->saddr, sizeof(addr.ip6));
|
2017-11-02 12:50:38 +01:00
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(addr.ip6); ++i)
|
|
|
|
addr.ip6[i] &= info->mask.ip6[i];
|
2017-12-09 21:01:08 +01:00
|
|
|
memcpy(key, &addr, sizeof(addr.ip6));
|
|
|
|
key[4] = zone->id;
|
2007-07-15 05:47:26 +02:00
|
|
|
} else {
|
|
|
|
const struct iphdr *iph = ip_hdr(skb);
|
2017-12-09 21:01:08 +01:00
|
|
|
key[0] = (info->flags & XT_CONNLIMIT_DADDR) ?
|
2011-01-18 17:32:40 +01:00
|
|
|
iph->daddr : iph->saddr;
|
2017-11-02 12:50:38 +01:00
|
|
|
|
2017-12-09 21:01:08 +01:00
|
|
|
key[0] &= info->mask.ip;
|
|
|
|
key[1] = zone->id;
|
2007-07-15 05:47:26 +02:00
|
|
|
}
|
|
|
|
|
2018-03-05 00:29:51 +01:00
|
|
|
connections = nf_conncount_count(net, info->data, key, tuple_ptr,
|
|
|
|
zone);
|
2014-03-12 23:49:51 +01:00
|
|
|
if (connections == 0)
|
2007-07-15 05:47:26 +02:00
|
|
|
/* kmalloc failed, drop it entirely */
|
2011-01-18 01:36:57 +01:00
|
|
|
goto hotdrop;
|
2007-07-15 05:47:26 +02:00
|
|
|
|
2017-12-09 21:01:08 +01:00
|
|
|
return (connections > info->limit) ^ !!(info->flags & XT_CONNLIMIT_INVERT);
|
2007-07-15 05:47:26 +02:00
|
|
|
|
|
|
|
hotdrop:
|
2009-07-07 20:54:30 +02:00
|
|
|
par->hotdrop = true;
|
2007-07-15 05:47:26 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-03-19 17:16:42 +01:00
|
|
|
static int connlimit_mt_check(const struct xt_mtchk_param *par)
|
2007-07-15 05:47:26 +02:00
|
|
|
{
|
2008-10-08 11:35:18 +02:00
|
|
|
struct xt_connlimit_info *info = par->matchinfo;
|
2017-12-09 21:01:08 +01:00
|
|
|
unsigned int keylen;
|
2007-07-15 05:47:26 +02:00
|
|
|
|
2017-12-09 21:01:08 +01:00
|
|
|
keylen = sizeof(u32);
|
|
|
|
if (par->family == NFPROTO_IPV6)
|
|
|
|
keylen += sizeof(struct in6_addr);
|
|
|
|
else
|
|
|
|
keylen += sizeof(struct in_addr);
|
2007-07-15 05:47:26 +02:00
|
|
|
|
|
|
|
/* init private data */
|
2017-12-09 21:01:08 +01:00
|
|
|
info->data = nf_conncount_init(par->net, par->family, keylen);
|
2007-07-15 05:47:26 +02:00
|
|
|
|
2018-07-19 15:20:09 +02:00
|
|
|
return PTR_ERR_OR_ZERO(info->data);
|
2007-07-15 05:47:26 +02:00
|
|
|
}
|
|
|
|
|
2014-03-12 23:49:51 +01:00
|
|
|
static void connlimit_mt_destroy(const struct xt_mtdtor_param *par)
|
|
|
|
{
|
|
|
|
const struct xt_connlimit_info *info = par->matchinfo;
|
2007-07-15 05:47:26 +02:00
|
|
|
|
2017-12-09 21:01:08 +01:00
|
|
|
nf_conncount_destroy(par->net, par->family, info->data);
|
2007-07-15 05:47:26 +02:00
|
|
|
}
|
|
|
|
|
2012-05-19 06:39:01 +02:00
|
|
|
static struct xt_match connlimit_mt_reg __read_mostly = {
|
|
|
|
.name = "connlimit",
|
|
|
|
.revision = 1,
|
|
|
|
.family = NFPROTO_UNSPEC,
|
|
|
|
.checkentry = connlimit_mt_check,
|
|
|
|
.match = connlimit_mt,
|
|
|
|
.matchsize = sizeof(struct xt_connlimit_info),
|
2017-01-02 23:19:46 +01:00
|
|
|
.usersize = offsetof(struct xt_connlimit_info, data),
|
2012-05-19 06:39:01 +02:00
|
|
|
.destroy = connlimit_mt_destroy,
|
|
|
|
.me = THIS_MODULE,
|
2007-07-15 05:47:26 +02:00
|
|
|
};
|
|
|
|
|
2007-12-05 08:24:03 +01:00
|
|
|
static int __init connlimit_mt_init(void)
|
2007-07-15 05:47:26 +02:00
|
|
|
{
|
2017-12-09 21:01:08 +01:00
|
|
|
return xt_register_match(&connlimit_mt_reg);
|
2007-07-15 05:47:26 +02:00
|
|
|
}
|
|
|
|
|
2007-12-05 08:24:03 +01:00
|
|
|
static void __exit connlimit_mt_exit(void)
|
2007-07-15 05:47:26 +02:00
|
|
|
{
|
2012-05-19 06:39:01 +02:00
|
|
|
xt_unregister_match(&connlimit_mt_reg);
|
2007-07-15 05:47:26 +02:00
|
|
|
}
|
|
|
|
|
2007-12-05 08:24:03 +01:00
|
|
|
module_init(connlimit_mt_init);
|
|
|
|
module_exit(connlimit_mt_exit);
|
2008-10-08 11:35:20 +02:00
|
|
|
MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>");
|
2008-01-15 08:42:28 +01:00
|
|
|
MODULE_DESCRIPTION("Xtables: Number of connections matching");
|
2007-07-15 05:47:26 +02:00
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_ALIAS("ipt_connlimit");
|
|
|
|
MODULE_ALIAS("ip6t_connlimit");
|