mirror of
git://git.code.sf.net/p/xtables-addons/xtables-addons
synced 2025-09-21 12:04:56 +02:00
Compare commits
40 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
2f1e094125 | ||
![]() |
37b000182f | ||
![]() |
255a310536 | ||
![]() |
3a6e73e986 | ||
![]() |
0a97126f5b | ||
![]() |
2ba833fe47 | ||
![]() |
77240e0918 | ||
![]() |
651e60f8d7 | ||
![]() |
54ac2a899a | ||
![]() |
61d2be172d | ||
![]() |
093f3b0a97 | ||
![]() |
57d25f22f1 | ||
![]() |
2f18ab31ec | ||
![]() |
12d0a8702c | ||
![]() |
35ce1adf5e | ||
![]() |
e5fe0b9c14 | ||
![]() |
cd7fc84b29 | ||
![]() |
4ff5a8fbf6 | ||
![]() |
37e3a543a9 | ||
![]() |
5f6cbbc663 | ||
![]() |
a2676585da | ||
![]() |
4a8aab6aed | ||
![]() |
dec7d7fc4d | ||
![]() |
5c615a3c73 | ||
![]() |
0c1375414d | ||
![]() |
7e5a8b0501 | ||
![]() |
cb8050172a | ||
![]() |
5be4ac8b2b | ||
![]() |
8d5b7c5b7d | ||
![]() |
f9aca7621c | ||
![]() |
af940bcbae | ||
![]() |
06b82c649d | ||
![]() |
e5093b61cd | ||
![]() |
7cd01e0b14 | ||
![]() |
4eb97c7a01 | ||
![]() |
48fbc6783e | ||
![]() |
a9f383daf8 | ||
![]() |
cbe58f55d0 | ||
![]() |
d11218815f | ||
![]() |
911d3d146a |
@@ -1,4 +1,4 @@
|
|||||||
AC_INIT([xtables-addons], [1.43])
|
AC_INIT([xtables-addons], [1.47])
|
||||||
AC_CONFIG_AUX_DIR([build-aux])
|
AC_CONFIG_AUX_DIR([build-aux])
|
||||||
AC_CONFIG_HEADERS([config.h])
|
AC_CONFIG_HEADERS([config.h])
|
||||||
AC_CONFIG_MACRO_DIR([m4])
|
AC_CONFIG_MACRO_DIR([m4])
|
||||||
@@ -31,7 +31,7 @@ xtlibdir="$(pkg-config --variable=xtlibdir xtables)"
|
|||||||
|
|
||||||
AC_ARG_WITH([xtlibdir],
|
AC_ARG_WITH([xtlibdir],
|
||||||
AS_HELP_STRING([--with-xtlibdir=PATH],
|
AS_HELP_STRING([--with-xtlibdir=PATH],
|
||||||
[Path where to install Xtables extensions [[autodetect]]]]),
|
[Path where to install Xtables extensions [[autodetect]]]),
|
||||||
[xtlibdir="$withval"])
|
[xtlibdir="$withval"])
|
||||||
AC_MSG_CHECKING([Xtables module directory])
|
AC_MSG_CHECKING([Xtables module directory])
|
||||||
AC_MSG_RESULT([$xtlibdir])
|
AC_MSG_RESULT([$xtlibdir])
|
||||||
@@ -63,8 +63,8 @@ if test -n "$kbuilddir"; then
|
|||||||
echo "WARNING: Version detection did not succeed. Continue at own luck.";
|
echo "WARNING: Version detection did not succeed. Continue at own luck.";
|
||||||
else
|
else
|
||||||
echo "$kmajor.$kminor.$kmicro.$kstable in $kbuilddir";
|
echo "$kmajor.$kminor.$kmicro.$kstable in $kbuilddir";
|
||||||
if test "$kmajor" -gt 3 -o "$kmajor" -eq 3 -a "$kminor" -gt 4; then
|
if test "$kmajor" -gt 3 -o "$kmajor" -eq 3 -a "$kminor" -gt 6; then
|
||||||
echo "WARNING: You are trying a newer kernel. Results may vary. :-)";
|
echo "WARNING: That kernel version is not officially supported.";
|
||||||
elif test "$kmajor" -eq 3; then
|
elif test "$kmajor" -eq 3; then
|
||||||
:;
|
:;
|
||||||
elif test "$kmajor" -eq 2 -a "$kminor" -eq 6 -a "$kmicro" -ge 32; then
|
elif test "$kmajor" -eq 2 -a "$kminor" -eq 6 -a "$kmicro" -ge 32; then
|
||||||
|
@@ -3,6 +3,44 @@ HEAD
|
|||||||
====
|
====
|
||||||
|
|
||||||
|
|
||||||
|
v1.47 (2010-10-15)
|
||||||
|
==================
|
||||||
|
Enhancements:
|
||||||
|
- xt_psd gained IPv6 support
|
||||||
|
Notes for this release:
|
||||||
|
- Linux 3.7+ is expressly unsupported by this release.
|
||||||
|
|
||||||
|
|
||||||
|
v1.46 (2012-08-23)
|
||||||
|
==================
|
||||||
|
Fixes:
|
||||||
|
- length2, SYSRQ, RAWNAT: preinitialize values for ipv6_find_hdr
|
||||||
|
- TARPIT: fix memory leak when tarpit_generic() fails
|
||||||
|
- build: remove extraneous closing bracket in configure.ac
|
||||||
|
- doc: update xt_SYSRQ.man to reflect that the full IPv6 address is needed
|
||||||
|
Enhancements:
|
||||||
|
- Support for Linux 3.6
|
||||||
|
|
||||||
|
|
||||||
|
v1.45 (2012-07-16)
|
||||||
|
==================
|
||||||
|
Fixes:
|
||||||
|
- build: export missing functions
|
||||||
|
(fixes: "WARNING 'xtnu_ipv6_find_hdr' [xt_TARPIT.ko] not found")
|
||||||
|
- build: avoid use of unexported functions
|
||||||
|
(fixes: "WARNING 'ipv6_find_hdr' [xt_TARPIT.ko] not found"
|
||||||
|
in <= linux-2.6.37)
|
||||||
|
|
||||||
|
|
||||||
|
v1.44 (2012-07-15)
|
||||||
|
==================
|
||||||
|
Fixes:
|
||||||
|
- SYSRQ: fix double target initialization at module load
|
||||||
|
- build: do not attempt to build IPv6 parts if CONFIG_IP6_NF_IPTABLES=n
|
||||||
|
Enhancements:
|
||||||
|
- TARPIT gained IPv6 support
|
||||||
|
|
||||||
|
|
||||||
v1.43 (2012-06-30)
|
v1.43 (2012-06-30)
|
||||||
==================
|
==================
|
||||||
Fixes:
|
Fixes:
|
||||||
|
@@ -17,7 +17,7 @@ endif
|
|||||||
obj-${build_IPMARK} += xt_IPMARK.o
|
obj-${build_IPMARK} += xt_IPMARK.o
|
||||||
obj-${build_LOGMARK} += xt_LOGMARK.o
|
obj-${build_LOGMARK} += xt_LOGMARK.o
|
||||||
obj-${build_RAWNAT} += xt_RAWNAT.o iptable_rawpost.o
|
obj-${build_RAWNAT} += xt_RAWNAT.o iptable_rawpost.o
|
||||||
ifneq (${CONFIG_IPV6},)
|
ifneq (${CONFIG_IP6_NF_IPTABLES},)
|
||||||
obj-${build_RAWNAT} += ip6table_rawpost.o
|
obj-${build_RAWNAT} += ip6table_rawpost.o
|
||||||
endif
|
endif
|
||||||
obj-${build_SYSRQ} += xt_SYSRQ.o
|
obj-${build_SYSRQ} += xt_SYSRQ.o
|
||||||
|
@@ -19,12 +19,16 @@
|
|||||||
#include <linux/netfilter_ipv6/ip6_tables.h>
|
#include <linux/netfilter_ipv6/ip6_tables.h>
|
||||||
#include <linux/netfilter_arp.h>
|
#include <linux/netfilter_arp.h>
|
||||||
#include <net/ip.h>
|
#include <net/ip.h>
|
||||||
|
#include <net/ipv6.h>
|
||||||
#include <net/route.h>
|
#include <net/route.h>
|
||||||
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 2, 0)
|
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 2, 0)
|
||||||
# include <linux/export.h>
|
# include <linux/export.h>
|
||||||
#endif
|
#endif
|
||||||
#include "compat_skbuff.h"
|
#include "compat_skbuff.h"
|
||||||
#include "compat_xtnu.h"
|
#include "compat_xtnu.h"
|
||||||
|
#if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE)
|
||||||
|
# define WITH_IPV6 1
|
||||||
|
#endif
|
||||||
|
|
||||||
#if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 22)
|
#if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 22)
|
||||||
static int xtnu_match_run(const struct sk_buff *skb,
|
static int xtnu_match_run(const struct sk_buff *skb,
|
||||||
@@ -613,12 +617,22 @@ void *HX_memmem(const void *space, size_t spacesize,
|
|||||||
}
|
}
|
||||||
EXPORT_SYMBOL_GPL(HX_memmem);
|
EXPORT_SYMBOL_GPL(HX_memmem);
|
||||||
|
|
||||||
#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 5, 0)
|
#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 3, 0) && defined(WITH_IPV6)
|
||||||
|
int xtnu_ipv6_skip_exthdr(const struct sk_buff *skb, int start,
|
||||||
|
uint8_t *nexthdrp, __be16 *fragoffp)
|
||||||
|
{
|
||||||
|
return ipv6_skip_exthdr(skb, start, nexthdrp);
|
||||||
|
}
|
||||||
|
EXPORT_SYMBOL_GPL(xtnu_ipv6_skip_exthdr);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 5, 0) && defined(WITH_IPV6)
|
||||||
int xtnu_ipv6_find_hdr(const struct sk_buff *skb, unsigned int *offset,
|
int xtnu_ipv6_find_hdr(const struct sk_buff *skb, unsigned int *offset,
|
||||||
int target, unsigned short *fragoff, int *fragflg)
|
int target, unsigned short *fragoff, int *fragflg)
|
||||||
{
|
{
|
||||||
return ipv6_find_hdr(skb, offset, target, fragoff);
|
return ipv6_find_hdr(skb, offset, target, fragoff);
|
||||||
}
|
}
|
||||||
|
EXPORT_SYMBOL_GPL(xtnu_ipv6_find_hdr);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
MODULE_LICENSE("GPL");
|
MODULE_LICENSE("GPL");
|
||||||
|
@@ -96,6 +96,7 @@
|
|||||||
# define nf_nat_ipv4_multi_range_compat nf_nat_multi_range_compat
|
# define nf_nat_ipv4_multi_range_compat nf_nat_multi_range_compat
|
||||||
# define nf_nat_ipv4_range nf_nat_range
|
# define nf_nat_ipv4_range nf_nat_range
|
||||||
# define NF_NAT_RANGE_MAP_IPS IP_NAT_RANGE_MAP_IPS
|
# define NF_NAT_RANGE_MAP_IPS IP_NAT_RANGE_MAP_IPS
|
||||||
|
# define ipv6_skip_exthdr xtnu_ipv6_skip_exthdr
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if !defined(NIP6) && !defined(NIP6_FMT)
|
#if !defined(NIP6) && !defined(NIP6_FMT)
|
||||||
|
@@ -162,6 +162,8 @@ extern void xtnu_csum_replace4(__u16 __bitwise *, __be32, __be32);
|
|||||||
extern void xtnu_proto_csum_replace4(__u16 __bitwise *, struct sk_buff *,
|
extern void xtnu_proto_csum_replace4(__u16 __bitwise *, struct sk_buff *,
|
||||||
__be32, __be32, bool);
|
__be32, __be32, bool);
|
||||||
extern int xtnu_skb_linearize(struct sk_buff *);
|
extern int xtnu_skb_linearize(struct sk_buff *);
|
||||||
|
extern int xtnu_ipv6_skip_exthdr(const struct sk_buff *, int,
|
||||||
|
uint8_t *, __be16 *);
|
||||||
extern int xtnu_ipv6_find_hdr(const struct sk_buff *, unsigned int *,
|
extern int xtnu_ipv6_find_hdr(const struct sk_buff *, unsigned int *,
|
||||||
int, unsigned short *, int *);
|
int, unsigned short *, int *);
|
||||||
|
|
||||||
|
@@ -62,7 +62,7 @@ password="password"
|
|||||||
seqno="$(date +%s)"
|
seqno="$(date +%s)"
|
||||||
salt="$(dd bs=12 count=1 if=/dev/urandom 2>/dev/null |
|
salt="$(dd bs=12 count=1 if=/dev/urandom 2>/dev/null |
|
||||||
openssl enc \-base64)"
|
openssl enc \-base64)"
|
||||||
ipaddr=10.10.25.7
|
ipaddr="2001:0db8:0000:0000:0000:ff00:0042:8329"
|
||||||
req="$sysrq_key,$seqno,$salt"
|
req="$sysrq_key,$seqno,$salt"
|
||||||
req="$req,$(echo \-n "$req,$ipaddr,$password" | sha1sum | cut \-c1\-40)"
|
req="$req,$(echo \-n "$req,$ipaddr,$password" | sha1sum | cut \-c1\-40)"
|
||||||
|
|
||||||
@@ -75,8 +75,8 @@ sysrq key can be used at once, but bear in mind that, for example, a sync may
|
|||||||
not complete before a subsequent reboot or poweroff.
|
not complete before a subsequent reboot or poweroff.
|
||||||
.PP
|
.PP
|
||||||
An IPv4 address should have no leading zeros, an IPv6 address should
|
An IPv4 address should have no leading zeros, an IPv6 address should
|
||||||
be in the form recommended by RFC 5952. The debug option will log the
|
be in the full expanded form (as shown above). The debug option will cause
|
||||||
correct form of the address.
|
output to be emitted in the same form.
|
||||||
.PP
|
.PP
|
||||||
The hashing scheme should be enough to prevent mis-use of SYSRQ in many
|
The hashing scheme should be enough to prevent mis-use of SYSRQ in many
|
||||||
environments, but it is not perfect: take reasonable precautions to
|
environments, but it is not perfect: take reasonable precautions to
|
||||||
|
@@ -106,7 +106,7 @@ static void tarpit_tg_save(const void *ip,
|
|||||||
static struct xtables_target tarpit_tg_reg = {
|
static struct xtables_target tarpit_tg_reg = {
|
||||||
.version = XTABLES_VERSION,
|
.version = XTABLES_VERSION,
|
||||||
.name = "TARPIT",
|
.name = "TARPIT",
|
||||||
.family = NFPROTO_IPV4,
|
.family = NFPROTO_UNSPEC,
|
||||||
.size = XT_ALIGN(sizeof(struct xt_tarpit_tginfo)),
|
.size = XT_ALIGN(sizeof(struct xt_tarpit_tginfo)),
|
||||||
.userspacesize = XT_ALIGN(sizeof(struct xt_tarpit_tginfo)),
|
.userspacesize = XT_ALIGN(sizeof(struct xt_tarpit_tginfo)),
|
||||||
.help = tarpit_tg_help,
|
.help = tarpit_tg_help,
|
||||||
|
@@ -142,7 +142,7 @@ static struct xtables_match psd_mt_reg = {
|
|||||||
.name = "psd",
|
.name = "psd",
|
||||||
.version = XTABLES_VERSION,
|
.version = XTABLES_VERSION,
|
||||||
.revision = 1,
|
.revision = 1,
|
||||||
.family = NFPROTO_IPV4,
|
.family = NFPROTO_UNSPEC,
|
||||||
.size = XT_ALIGN(sizeof(struct xt_psd_info)),
|
.size = XT_ALIGN(sizeof(struct xt_psd_info)),
|
||||||
.userspacesize = XT_ALIGN(sizeof(struct xt_psd_info)),
|
.userspacesize = XT_ALIGN(sizeof(struct xt_psd_info)),
|
||||||
.help = psd_mt_help,
|
.help = psd_mt_help,
|
||||||
|
@@ -18,6 +18,7 @@
|
|||||||
# include <linux/netfilter_bridge.h>
|
# include <linux/netfilter_bridge.h>
|
||||||
#endif
|
#endif
|
||||||
#include <net/ip.h>
|
#include <net/ip.h>
|
||||||
|
#include <net/ip6_checksum.h>
|
||||||
#include <net/ip6_route.h>
|
#include <net/ip6_route.h>
|
||||||
#include <net/route.h>
|
#include <net/route.h>
|
||||||
#include "compat_xtables.h"
|
#include "compat_xtables.h"
|
||||||
|
@@ -22,8 +22,7 @@
|
|||||||
#include <net/ipv6.h>
|
#include <net/ipv6.h>
|
||||||
#include "compat_xtables.h"
|
#include "compat_xtables.h"
|
||||||
#include "xt_RAWNAT.h"
|
#include "xt_RAWNAT.h"
|
||||||
|
#if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE)
|
||||||
#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
|
|
||||||
# define WITH_IPV6 1
|
# define WITH_IPV6 1
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -245,7 +244,7 @@ static unsigned int
|
|||||||
rawsnat_tg6(struct sk_buff **pskb, const struct xt_action_param *par)
|
rawsnat_tg6(struct sk_buff **pskb, const struct xt_action_param *par)
|
||||||
{
|
{
|
||||||
const struct xt_rawnat_tginfo *info = par->targinfo;
|
const struct xt_rawnat_tginfo *info = par->targinfo;
|
||||||
unsigned int l4offset, l4proto;
|
unsigned int l4offset = 0, l4proto;
|
||||||
struct ipv6hdr *iph;
|
struct ipv6hdr *iph;
|
||||||
struct in6_addr new_addr;
|
struct in6_addr new_addr;
|
||||||
|
|
||||||
@@ -266,7 +265,7 @@ static unsigned int
|
|||||||
rawdnat_tg6(struct sk_buff **pskb, const struct xt_action_param *par)
|
rawdnat_tg6(struct sk_buff **pskb, const struct xt_action_param *par)
|
||||||
{
|
{
|
||||||
const struct xt_rawnat_tginfo *info = par->targinfo;
|
const struct xt_rawnat_tginfo *info = par->targinfo;
|
||||||
unsigned int l4offset, l4proto;
|
unsigned int l4offset = 0, l4proto;
|
||||||
struct ipv6hdr *iph;
|
struct ipv6hdr *iph;
|
||||||
struct in6_addr new_addr;
|
struct in6_addr new_addr;
|
||||||
|
|
||||||
|
@@ -30,7 +30,7 @@
|
|||||||
(defined(CONFIG_CRYPTO) || defined(CONFIG_CRYPTO_MODULE))
|
(defined(CONFIG_CRYPTO) || defined(CONFIG_CRYPTO_MODULE))
|
||||||
# define WITH_CRYPTO 1
|
# define WITH_CRYPTO 1
|
||||||
#endif
|
#endif
|
||||||
#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
|
#if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE)
|
||||||
# define WITH_IPV6 1
|
# define WITH_IPV6 1
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -240,7 +240,7 @@ sysrq_tg6(struct sk_buff **pskb, const struct xt_action_param *par)
|
|||||||
const struct ipv6hdr *iph;
|
const struct ipv6hdr *iph;
|
||||||
const struct udphdr *udph;
|
const struct udphdr *udph;
|
||||||
unsigned short frag_off;
|
unsigned short frag_off;
|
||||||
unsigned int th_off;
|
unsigned int th_off = 0;
|
||||||
uint16_t len;
|
uint16_t len;
|
||||||
|
|
||||||
if (skb_linearize(skb) < 0)
|
if (skb_linearize(skb) < 0)
|
||||||
@@ -359,10 +359,7 @@ static int __init sysrq_crypto_init(void)
|
|||||||
goto fail;
|
goto fail;
|
||||||
do_gettimeofday(&now);
|
do_gettimeofday(&now);
|
||||||
sysrq_seqno = now.tv_sec;
|
sysrq_seqno = now.tv_sec;
|
||||||
ret = xt_register_targets(sysrq_tg_reg, ARRAY_SIZE(sysrq_tg_reg));
|
return 0;
|
||||||
if (ret < 0)
|
|
||||||
goto fail;
|
|
||||||
return ret;
|
|
||||||
|
|
||||||
fail:
|
fail:
|
||||||
sysrq_crypto_exit();
|
sysrq_crypto_exit();
|
||||||
|
@@ -42,19 +42,139 @@
|
|||||||
#include <linux/module.h>
|
#include <linux/module.h>
|
||||||
#include <linux/skbuff.h>
|
#include <linux/skbuff.h>
|
||||||
#include <linux/version.h>
|
#include <linux/version.h>
|
||||||
|
#include <linux/netfilter_ipv6.h>
|
||||||
#include <linux/netfilter/x_tables.h>
|
#include <linux/netfilter/x_tables.h>
|
||||||
#ifdef CONFIG_BRIDGE_NETFILTER
|
#ifdef CONFIG_BRIDGE_NETFILTER
|
||||||
# include <linux/netfilter_bridge.h>
|
# include <linux/netfilter_bridge.h>
|
||||||
#endif
|
#endif
|
||||||
|
#include <net/addrconf.h>
|
||||||
|
#include <net/ip6_checksum.h>
|
||||||
|
#include <net/ip6_route.h>
|
||||||
|
#include <net/ipv6.h>
|
||||||
#include <net/route.h>
|
#include <net/route.h>
|
||||||
#include <net/tcp.h>
|
#include <net/tcp.h>
|
||||||
#include "compat_xtables.h"
|
#include "compat_xtables.h"
|
||||||
#include "xt_TARPIT.h"
|
#include "xt_TARPIT.h"
|
||||||
|
#if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE)
|
||||||
|
# define WITH_IPV6 1
|
||||||
|
#endif
|
||||||
|
|
||||||
static void tarpit_tcp(struct sk_buff *oldskb, unsigned int hook,
|
static bool xttarpit_tarpit(struct tcphdr *tcph, const struct tcphdr *oth)
|
||||||
|
{
|
||||||
|
/* No replies for RST, FIN or !SYN,!ACK */
|
||||||
|
if (oth->rst || oth->fin || (!oth->syn && !oth->ack))
|
||||||
|
return false;
|
||||||
|
tcph->seq = oth->ack ? oth->ack_seq : 0;
|
||||||
|
|
||||||
|
/* Our SYN-ACKs must have a >0 window */
|
||||||
|
tcph->window = (oth->syn && !oth->ack) ? htons(5) : 0;
|
||||||
|
if (oth->syn && oth->ack) {
|
||||||
|
tcph->rst = true;
|
||||||
|
tcph->ack_seq = false;
|
||||||
|
} else {
|
||||||
|
tcph->syn = oth->syn;
|
||||||
|
tcph->ack = true;
|
||||||
|
tcph->ack_seq = htonl(ntohl(oth->seq) + oth->syn);
|
||||||
|
}
|
||||||
|
#if 0
|
||||||
|
/* Rate-limit replies to !SYN,ACKs */
|
||||||
|
if (!oth->syn && oth->ack)
|
||||||
|
if (!xrlim_allow(rt_dst(ort), HZ))
|
||||||
|
return false;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool xttarpit_honeypot(struct tcphdr *tcph, const struct tcphdr *oth,
|
||||||
|
uint16_t payload)
|
||||||
|
{
|
||||||
|
/* Do not answer any resets regardless of combination */
|
||||||
|
if (oth->rst || oth->seq == 0xDEADBEEF)
|
||||||
|
return false;
|
||||||
|
/* Send a reset to scanners. They like that. */
|
||||||
|
if (oth->syn && oth->ack) {
|
||||||
|
tcph->window = 0;
|
||||||
|
tcph->ack = false;
|
||||||
|
tcph->psh = true;
|
||||||
|
tcph->ack_seq = 0xdeadbeef; /* see if they ack it */
|
||||||
|
tcph->seq = oth->ack_seq;
|
||||||
|
tcph->rst = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* SYN > SYN-ACK */
|
||||||
|
if (oth->syn && !oth->ack) {
|
||||||
|
tcph->syn = true;
|
||||||
|
tcph->ack = true;
|
||||||
|
tcph->window = oth->window &
|
||||||
|
((net_random() & 0x1f) - 0xf);
|
||||||
|
tcph->seq = htonl(net_random() & ~oth->seq);
|
||||||
|
tcph->ack_seq = htonl(ntohl(oth->seq) + oth->syn);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ACK > ACK */
|
||||||
|
if (oth->ack && (!(oth->fin || oth->syn))) {
|
||||||
|
tcph->syn = false;
|
||||||
|
tcph->ack = true;
|
||||||
|
tcph->window = oth->window &
|
||||||
|
((net_random() & 0x1f) - 0xf);
|
||||||
|
tcph->ack_seq = payload > 100 ?
|
||||||
|
htonl(ntohl(oth->seq) + payload) :
|
||||||
|
oth->seq;
|
||||||
|
tcph->seq = oth->ack_seq;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* FIN > RST.
|
||||||
|
* We cannot terminate gracefully so just be abrupt.
|
||||||
|
*/
|
||||||
|
if (oth->fin) {
|
||||||
|
tcph->window = 0;
|
||||||
|
tcph->seq = oth->ack_seq;
|
||||||
|
tcph->ack_seq = oth->ack_seq;
|
||||||
|
tcph->fin = false;
|
||||||
|
tcph->ack = false;
|
||||||
|
tcph->rst = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void xttarpit_reset(struct tcphdr *tcph, const struct tcphdr *oth)
|
||||||
|
{
|
||||||
|
tcph->window = 0;
|
||||||
|
tcph->ack = false;
|
||||||
|
tcph->syn = false;
|
||||||
|
tcph->rst = true;
|
||||||
|
tcph->seq = oth->ack_seq;
|
||||||
|
tcph->ack_seq = oth->seq;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool tarpit_generic(struct tcphdr *tcph, const struct tcphdr *oth,
|
||||||
|
uint16_t payload, unsigned int mode)
|
||||||
|
{
|
||||||
|
switch(mode) {
|
||||||
|
case XTTARPIT_TARPIT:
|
||||||
|
if (!xttarpit_tarpit(tcph, oth))
|
||||||
|
return false;
|
||||||
|
break;
|
||||||
|
case XTTARPIT_HONEYPOT:
|
||||||
|
if (!xttarpit_honeypot(tcph, oth, payload))
|
||||||
|
return false;
|
||||||
|
break;
|
||||||
|
case XTTARPIT_RESET:
|
||||||
|
xttarpit_reset(tcph, oth);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void tarpit_tcp4(struct sk_buff *oldskb, unsigned int hook,
|
||||||
unsigned int mode)
|
unsigned int mode)
|
||||||
{
|
{
|
||||||
struct tcphdr _otcph, *oth, *tcph;
|
struct tcphdr _otcph, *tcph;
|
||||||
|
const struct tcphdr *oth;
|
||||||
unsigned int addr_type = RTN_UNSPEC;
|
unsigned int addr_type = RTN_UNSPEC;
|
||||||
struct sk_buff *nskb;
|
struct sk_buff *nskb;
|
||||||
const struct iphdr *oldhdr;
|
const struct iphdr *oldhdr;
|
||||||
@@ -116,84 +236,8 @@ static void tarpit_tcp(struct sk_buff *oldskb, unsigned int hook,
|
|||||||
/* Reset flags */
|
/* Reset flags */
|
||||||
((u_int8_t *)tcph)[13] = 0;
|
((u_int8_t *)tcph)[13] = 0;
|
||||||
|
|
||||||
if (mode == XTTARPIT_TARPIT) {
|
if (!tarpit_generic(tcph, oth, payload, mode))
|
||||||
/* No replies for RST, FIN or !SYN,!ACK */
|
goto free_nskb;
|
||||||
if (oth->rst || oth->fin || (!oth->syn && !oth->ack))
|
|
||||||
return;
|
|
||||||
tcph->seq = oth->ack ? oth->ack_seq : 0;
|
|
||||||
|
|
||||||
/* Our SYN-ACKs must have a >0 window */
|
|
||||||
tcph->window = (oth->syn && !oth->ack) ? htons(5) : 0;
|
|
||||||
if (oth->syn && oth->ack) {
|
|
||||||
tcph->rst = true;
|
|
||||||
tcph->ack_seq = false;
|
|
||||||
} else {
|
|
||||||
tcph->syn = oth->syn;
|
|
||||||
tcph->ack = true;
|
|
||||||
tcph->ack_seq = htonl(ntohl(oth->seq) + oth->syn);
|
|
||||||
}
|
|
||||||
#if 0
|
|
||||||
/* Rate-limit replies to !SYN,ACKs */
|
|
||||||
if (!oth->syn && oth->ack)
|
|
||||||
if (!xrlim_allow(rt_dst(ort), HZ))
|
|
||||||
return;
|
|
||||||
#endif
|
|
||||||
} else if (mode == XTTARPIT_HONEYPOT) {
|
|
||||||
/* Do not answer any resets regardless of combination */
|
|
||||||
if (oth->rst || oth->seq == 0xDEADBEEF)
|
|
||||||
return;
|
|
||||||
/* Send a reset to scanners. They like that. */
|
|
||||||
if (oth->syn && oth->ack) {
|
|
||||||
tcph->window = 0;
|
|
||||||
tcph->ack = false;
|
|
||||||
tcph->psh = true;
|
|
||||||
tcph->ack_seq = 0xdeadbeef; /* see if they ack it */
|
|
||||||
tcph->seq = oth->ack_seq;
|
|
||||||
tcph->rst = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* SYN > SYN-ACK */
|
|
||||||
if (oth->syn && !oth->ack) {
|
|
||||||
tcph->syn = true;
|
|
||||||
tcph->ack = true;
|
|
||||||
tcph->window = oth->window &
|
|
||||||
((net_random() & 0x1f) - 0xf);
|
|
||||||
tcph->seq = htonl(net_random() & ~oth->seq);
|
|
||||||
tcph->ack_seq = htonl(ntohl(oth->seq) + oth->syn);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* ACK > ACK */
|
|
||||||
if (oth->ack && (!(oth->fin || oth->syn))) {
|
|
||||||
tcph->syn = false;
|
|
||||||
tcph->ack = true;
|
|
||||||
tcph->window = oth->window &
|
|
||||||
((net_random() & 0x1f) - 0xf);
|
|
||||||
tcph->ack_seq = payload > 100 ?
|
|
||||||
htonl(ntohl(oth->seq) + payload) :
|
|
||||||
oth->seq;
|
|
||||||
tcph->seq = oth->ack_seq;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* FIN > RST.
|
|
||||||
* We cannot terminate gracefully so just be abrupt.
|
|
||||||
*/
|
|
||||||
if (oth->fin) {
|
|
||||||
tcph->window = 0;
|
|
||||||
tcph->seq = oth->ack_seq;
|
|
||||||
tcph->ack_seq = oth->ack_seq;
|
|
||||||
tcph->fin = false;
|
|
||||||
tcph->ack = false;
|
|
||||||
tcph->rst = true;
|
|
||||||
}
|
|
||||||
} else if (mode == XTTARPIT_RESET) {
|
|
||||||
tcph->window = 0;
|
|
||||||
tcph->ack = false;
|
|
||||||
tcph->syn = false;
|
|
||||||
tcph->rst = true;
|
|
||||||
tcph->seq = oth->ack_seq;
|
|
||||||
tcph->ack_seq = oth->seq;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Adjust TCP checksum */
|
/* Adjust TCP checksum */
|
||||||
tcph->check = 0;
|
tcph->check = 0;
|
||||||
@@ -257,8 +301,132 @@ static void tarpit_tcp(struct sk_buff *oldskb, unsigned int hook,
|
|||||||
kfree_skb(nskb);
|
kfree_skb(nskb);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef WITH_IPV6
|
||||||
|
static void tarpit_tcp6(struct sk_buff *oldskb, unsigned int hook,
|
||||||
|
unsigned int mode)
|
||||||
|
{
|
||||||
|
struct sk_buff *nskb;
|
||||||
|
struct tcphdr *tcph, oth;
|
||||||
|
unsigned int otcplen;
|
||||||
|
int tcphoff;
|
||||||
|
const struct ipv6hdr *oip6h = ipv6_hdr(oldskb);
|
||||||
|
struct ipv6hdr *ip6h;
|
||||||
|
const uint8_t tclass = 0;
|
||||||
|
uint8_t proto;
|
||||||
|
uint16_t payload;
|
||||||
|
__be16 frag_off;
|
||||||
|
|
||||||
|
proto = oip6h->nexthdr;
|
||||||
|
tcphoff = ipv6_skip_exthdr(oldskb,
|
||||||
|
(uint8_t *)(oip6h + 1) - oldskb->data, &proto, &frag_off);
|
||||||
|
|
||||||
|
if (tcphoff < 0 || tcphoff > oldskb->len) {
|
||||||
|
pr_debug("Cannot get TCP header.\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
otcplen = oldskb->len - tcphoff;
|
||||||
|
|
||||||
|
/* IP header checks: fragment, too short. */
|
||||||
|
if (proto != IPPROTO_TCP || otcplen < sizeof(struct tcphdr)) {
|
||||||
|
pr_debug("proto(%d) != IPPROTO_TCP, "
|
||||||
|
"or too short. otcplen = %d\n",
|
||||||
|
proto, otcplen);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (skb_copy_bits(oldskb, tcphoff, &oth, sizeof(struct tcphdr))) {
|
||||||
|
WARN_ON(1);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Check checksum. */
|
||||||
|
if (csum_ipv6_magic(&oip6h->saddr, &oip6h->daddr, otcplen, IPPROTO_TCP,
|
||||||
|
skb_checksum(oldskb, tcphoff, otcplen, 0))) {
|
||||||
|
pr_debug("TCP checksum is invalid\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
nskb = skb_copy_expand(oldskb, LL_MAX_HEADER,
|
||||||
|
skb_tailroom(oldskb), GFP_ATOMIC);
|
||||||
|
if (nskb == NULL) {
|
||||||
|
if (net_ratelimit())
|
||||||
|
pr_debug("cannot alloc skb\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* This packet will not be the same as the other: clear nf fields */
|
||||||
|
nf_reset(nskb);
|
||||||
|
skb_nfmark(nskb) = 0;
|
||||||
|
skb_init_secmark(nskb);
|
||||||
|
|
||||||
|
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 18)
|
||||||
|
skb_shinfo(nskb)->gso_size = 0;
|
||||||
|
skb_shinfo(nskb)->gso_segs = 0;
|
||||||
|
skb_shinfo(nskb)->gso_type = 0;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
skb_put(nskb, sizeof(struct ipv6hdr));
|
||||||
|
ip6h = ipv6_hdr(nskb);
|
||||||
|
*(__be32 *)ip6h = htonl(0x60000000 | (tclass << 20));
|
||||||
|
ip6h->nexthdr = IPPROTO_TCP;
|
||||||
|
ip6h->saddr = oip6h->daddr;
|
||||||
|
ip6h->daddr = oip6h->saddr;
|
||||||
|
|
||||||
|
/* Adjust IP TTL */
|
||||||
|
if (mode == XTTARPIT_HONEYPOT) {
|
||||||
|
ip6h->hop_limit = 128;
|
||||||
|
} else {
|
||||||
|
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 38)
|
||||||
|
ip6h->hop_limit = ip6_dst_hoplimit(skb_dst(nskb));
|
||||||
|
#else
|
||||||
|
ip6h->hop_limit = dst_metric(skb_dst(nskb), RTAX_HOPLIMIT);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
tcph = (struct tcphdr *)(skb_network_header(nskb) +
|
||||||
|
sizeof(struct ipv6hdr));
|
||||||
|
|
||||||
|
/* Truncate to length (no data) */
|
||||||
|
skb_trim(nskb, sizeof(struct ipv6hdr) + sizeof(struct tcphdr));
|
||||||
|
tcph->doff = sizeof(struct tcphdr)/4;
|
||||||
|
tcph->source = oth.dest;
|
||||||
|
tcph->dest = oth.source;
|
||||||
|
tcph->urg_ptr = 0;
|
||||||
|
/* Reset flags */
|
||||||
|
((uint8_t *)tcph)[13] = 0;
|
||||||
|
|
||||||
|
payload = nskb->len - sizeof(struct ipv6hdr) - sizeof(struct tcphdr);
|
||||||
|
if (!tarpit_generic(&oth, tcph, payload, mode))
|
||||||
|
goto free_nskb;
|
||||||
|
|
||||||
|
ip6h->payload_len = htons(sizeof(struct tcphdr));
|
||||||
|
tcph->check = 0;
|
||||||
|
|
||||||
|
/* Adjust TCP checksum */
|
||||||
|
tcph->check = csum_ipv6_magic(&ipv6_hdr(nskb)->saddr,
|
||||||
|
&ipv6_hdr(nskb)->daddr, sizeof(struct tcphdr),
|
||||||
|
IPPROTO_TCP,
|
||||||
|
csum_partial(tcph, sizeof(struct tcphdr), 0));
|
||||||
|
|
||||||
|
if (ip6_route_me_harder(nskb))
|
||||||
|
goto free_nskb;
|
||||||
|
|
||||||
|
nskb->ip_summed = CHECKSUM_NONE;
|
||||||
|
|
||||||
|
nf_ct_attach(nskb, oldskb);
|
||||||
|
|
||||||
|
NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, nskb, NULL,
|
||||||
|
skb_dst(nskb)->dev, dst_output);
|
||||||
|
return;
|
||||||
|
|
||||||
|
free_nskb:
|
||||||
|
kfree_skb(nskb);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
static unsigned int
|
static unsigned int
|
||||||
tarpit_tg(struct sk_buff **pskb, const struct xt_action_param *par)
|
tarpit_tg4(struct sk_buff **pskb, const struct xt_action_param *par)
|
||||||
{
|
{
|
||||||
const struct sk_buff *skb = *pskb;
|
const struct sk_buff *skb = *pskb;
|
||||||
const struct iphdr *iph = ip_hdr(skb);
|
const struct iphdr *iph = ip_hdr(skb);
|
||||||
@@ -289,29 +457,87 @@ tarpit_tg(struct sk_buff **pskb, const struct xt_action_param *par)
|
|||||||
if (iph->frag_off & htons(IP_OFFSET))
|
if (iph->frag_off & htons(IP_OFFSET))
|
||||||
return NF_DROP;
|
return NF_DROP;
|
||||||
|
|
||||||
tarpit_tcp(*pskb, par->hooknum, info->variant);
|
tarpit_tcp4(*pskb, par->hooknum, info->variant);
|
||||||
return NF_DROP;
|
return NF_DROP;
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct xt_target tarpit_tg_reg __read_mostly = {
|
#ifdef WITH_IPV6
|
||||||
|
static unsigned int
|
||||||
|
tarpit_tg6(struct sk_buff **pskb, const struct xt_action_param *par)
|
||||||
|
{
|
||||||
|
const struct sk_buff *skb = *pskb;
|
||||||
|
const struct ipv6hdr *iph = ipv6_hdr(skb);
|
||||||
|
const struct rt6_info *rt = (struct rt6_info *)skb_dst(skb);
|
||||||
|
const struct xt_tarpit_tginfo *info = par->targinfo;
|
||||||
|
uint8_t proto;
|
||||||
|
__be16 frag_off;
|
||||||
|
|
||||||
|
/* Do we have an input route cache entry? (Not in PREROUTING.) */
|
||||||
|
if (rt == NULL) {
|
||||||
|
pr_debug("Dropping no input route cache entry\n");
|
||||||
|
return NF_DROP;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* No replies to physical multicast/broadcast */
|
||||||
|
/* skb != PACKET_OTHERHOST handled by ip_rcv() */
|
||||||
|
if (skb->pkt_type != PACKET_HOST) {
|
||||||
|
pr_debug("type != PACKET_HOST");
|
||||||
|
return NF_DROP;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Our naive response construction does not deal with IP
|
||||||
|
* options, and probably should not try.
|
||||||
|
*/
|
||||||
|
proto = iph->nexthdr;
|
||||||
|
if (ipv6_skip_exthdr(skb, skb_network_header_len(skb), &proto,
|
||||||
|
&frag_off) != sizeof(struct ipv6hdr))
|
||||||
|
return NF_DROP;
|
||||||
|
|
||||||
|
if ((!(ipv6_addr_type(&iph->saddr) & IPV6_ADDR_UNICAST)) ||
|
||||||
|
(!(ipv6_addr_type(&iph->daddr) & IPV6_ADDR_UNICAST))) {
|
||||||
|
pr_debug("addr is not unicast.\n");
|
||||||
|
return NF_DROP;
|
||||||
|
}
|
||||||
|
|
||||||
|
tarpit_tcp6(*pskb, par->hooknum, info->variant);
|
||||||
|
return NF_DROP;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static struct xt_target tarpit_tg_reg[] __read_mostly = {
|
||||||
|
{
|
||||||
.name = "TARPIT",
|
.name = "TARPIT",
|
||||||
.revision = 0,
|
.revision = 0,
|
||||||
.family = NFPROTO_IPV4,
|
.family = NFPROTO_IPV4,
|
||||||
.hooks = (1 << NF_INET_LOCAL_IN) | (1 << NF_INET_FORWARD),
|
.hooks = (1 << NF_INET_LOCAL_IN) | (1 << NF_INET_FORWARD),
|
||||||
.proto = IPPROTO_TCP,
|
.proto = IPPROTO_TCP,
|
||||||
.target = tarpit_tg,
|
.target = tarpit_tg4,
|
||||||
.targetsize = sizeof(struct xt_tarpit_tginfo),
|
.targetsize = sizeof(struct xt_tarpit_tginfo),
|
||||||
.me = THIS_MODULE,
|
.me = THIS_MODULE,
|
||||||
|
},
|
||||||
|
#ifdef WITH_IPV6
|
||||||
|
{
|
||||||
|
.name = "TARPIT",
|
||||||
|
.revision = 0,
|
||||||
|
.family = NFPROTO_IPV6,
|
||||||
|
.hooks = (1 << NF_INET_LOCAL_IN) | (1 << NF_INET_FORWARD),
|
||||||
|
.proto = IPPROTO_TCP,
|
||||||
|
.target = tarpit_tg6,
|
||||||
|
.targetsize = sizeof(struct xt_tarpit_tginfo),
|
||||||
|
.me = THIS_MODULE,
|
||||||
|
},
|
||||||
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
static int __init tarpit_tg_init(void)
|
static int __init tarpit_tg_init(void)
|
||||||
{
|
{
|
||||||
return xt_register_target(&tarpit_tg_reg);
|
return xt_register_targets(tarpit_tg_reg, ARRAY_SIZE(tarpit_tg_reg));
|
||||||
}
|
}
|
||||||
|
|
||||||
static void __exit tarpit_tg_exit(void)
|
static void __exit tarpit_tg_exit(void)
|
||||||
{
|
{
|
||||||
xt_unregister_target(&tarpit_tg_reg);
|
xt_unregister_targets(tarpit_tg_reg, ARRAY_SIZE(tarpit_tg_reg));
|
||||||
}
|
}
|
||||||
|
|
||||||
module_init(tarpit_tg_init);
|
module_init(tarpit_tg_init);
|
||||||
@@ -320,3 +546,4 @@ MODULE_DESCRIPTION("Xtables: \"TARPIT\", capture and hold TCP connections");
|
|||||||
MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>");
|
MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>");
|
||||||
MODULE_LICENSE("GPL");
|
MODULE_LICENSE("GPL");
|
||||||
MODULE_ALIAS("ipt_TARPIT");
|
MODULE_ALIAS("ipt_TARPIT");
|
||||||
|
MODULE_ALIAS("ip6t_TARPIT");
|
||||||
|
@@ -33,7 +33,7 @@
|
|||||||
# define WITH_CONNTRACK 1
|
# define WITH_CONNTRACK 1
|
||||||
# include <net/netfilter/nf_conntrack.h>
|
# include <net/netfilter/nf_conntrack.h>
|
||||||
#endif
|
#endif
|
||||||
#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
|
#if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE)
|
||||||
# define WITH_IPV6 1
|
# define WITH_IPV6 1
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@@ -203,7 +203,8 @@ length2_mt6(const struct sk_buff *skb, struct xt_action_param *par)
|
|||||||
const struct xt_length_mtinfo2 *info = par->matchinfo;
|
const struct xt_length_mtinfo2 *info = par->matchinfo;
|
||||||
const struct ipv6hdr *iph = ipv6_hdr(skb);
|
const struct ipv6hdr *iph = ipv6_hdr(skb);
|
||||||
unsigned int len = 0, l4proto;
|
unsigned int len = 0, l4proto;
|
||||||
unsigned int thoff = par->thoff;
|
/* par->thoff would only set if ip6tables -p was used; so just use 0 */
|
||||||
|
unsigned int thoff = 0;
|
||||||
bool hit = true;
|
bool hit = true;
|
||||||
|
|
||||||
if (info->flags & XT_LENGTH_LAYER3) {
|
if (info->flags & XT_LENGTH_LAYER3) {
|
||||||
|
@@ -22,13 +22,14 @@
|
|||||||
|
|
||||||
#define pr_fmt(x) KBUILD_MODNAME ": " x
|
#define pr_fmt(x) KBUILD_MODNAME ": " x
|
||||||
#include <linux/module.h>
|
#include <linux/module.h>
|
||||||
#include <linux/moduleparam.h>
|
|
||||||
#include <linux/skbuff.h>
|
#include <linux/skbuff.h>
|
||||||
#include <linux/ip.h>
|
#include <linux/types.h>
|
||||||
#include <net/tcp.h>
|
#include <linux/tcp.h>
|
||||||
#include <linux/spinlock.h>
|
#include <linux/spinlock.h>
|
||||||
#include <linux/netfilter_ipv4/ip_tables.h>
|
|
||||||
#include <linux/netfilter/x_tables.h>
|
#include <linux/netfilter/x_tables.h>
|
||||||
|
#include <linux/netfilter_ipv6/ip6_tables.h>
|
||||||
|
#include <net/ip.h>
|
||||||
|
#include <net/ipv6.h>
|
||||||
#include "xt_psd.h"
|
#include "xt_psd.h"
|
||||||
#include "compat_xtables.h"
|
#include "compat_xtables.h"
|
||||||
|
|
||||||
@@ -39,6 +40,7 @@ MODULE_AUTHOR("Jan Rekorajski <baggins@pld.org.pl>");
|
|||||||
MODULE_AUTHOR(" Mohd Nawawi Mohamad Jamili <nawawi@tracenetworkcorporation.com>");
|
MODULE_AUTHOR(" Mohd Nawawi Mohamad Jamili <nawawi@tracenetworkcorporation.com>");
|
||||||
MODULE_DESCRIPTION("Xtables: PSD - portscan detection");
|
MODULE_DESCRIPTION("Xtables: PSD - portscan detection");
|
||||||
MODULE_ALIAS("ipt_psd");
|
MODULE_ALIAS("ipt_psd");
|
||||||
|
MODULE_ALIAS("ip6t_psd");
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Keep track of up to LIST_SIZE source addresses, using a hash table of
|
* Keep track of up to LIST_SIZE source addresses, using a hash table of
|
||||||
@@ -50,6 +52,10 @@ MODULE_ALIAS("ipt_psd");
|
|||||||
#define HASH_SIZE (1 << HASH_LOG)
|
#define HASH_SIZE (1 << HASH_LOG)
|
||||||
#define HASH_MAX 0x10
|
#define HASH_MAX 0x10
|
||||||
|
|
||||||
|
#if defined(CONFIG_IP6_NF_IPTABLES) || defined(CONFIG_IP6_NF_IPTABLES_MODULE)
|
||||||
|
# define WITH_IPV6 1
|
||||||
|
#endif
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Information we keep per each target port
|
* Information we keep per each target port
|
||||||
*/
|
*/
|
||||||
@@ -68,8 +74,6 @@ struct port {
|
|||||||
struct host {
|
struct host {
|
||||||
struct host *next;
|
struct host *next;
|
||||||
unsigned long timestamp;
|
unsigned long timestamp;
|
||||||
struct in_addr src_addr;
|
|
||||||
struct in_addr dest_addr;
|
|
||||||
__be16 src_port;
|
__be16 src_port;
|
||||||
uint16_t count;
|
uint16_t count;
|
||||||
uint8_t weight;
|
uint8_t weight;
|
||||||
@@ -77,27 +81,87 @@ struct host {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* State information.
|
* Information we keep per ipv4 source address.
|
||||||
|
*/
|
||||||
|
struct host4 {
|
||||||
|
struct host host;
|
||||||
|
__be32 saddr;
|
||||||
|
};
|
||||||
|
|
||||||
|
static struct host4 *host_to_host4(const struct host *h)
|
||||||
|
{
|
||||||
|
return (struct host4 *)h;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct host6 {
|
||||||
|
struct host host;
|
||||||
|
struct in6_addr saddr;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* State information for IPv4 portscan detection.
|
||||||
* @list: list of source addresses
|
* @list: list of source addresses
|
||||||
* @hash: pointers into the list
|
* @hash: pointers into the list
|
||||||
* @index: oldest entry to be replaced
|
* @index: oldest entry to be replaced
|
||||||
*/
|
*/
|
||||||
static struct {
|
static struct {
|
||||||
spinlock_t lock;
|
spinlock_t lock;
|
||||||
struct host list[LIST_SIZE];
|
struct host4 list[LIST_SIZE];
|
||||||
struct host *hash[HASH_SIZE];
|
struct host *hash[HASH_SIZE];
|
||||||
int index;
|
int index;
|
||||||
} state;
|
} state;
|
||||||
|
|
||||||
|
#ifdef WITH_IPV6
|
||||||
|
/**
|
||||||
|
* State information for IPv6 portscan detection.
|
||||||
|
* @list: list of source addresses
|
||||||
|
* @hash: pointers into the list
|
||||||
|
* @index: oldest entry to be replaced
|
||||||
|
*/
|
||||||
|
static struct {
|
||||||
|
spinlock_t lock;
|
||||||
|
struct host6 *list;
|
||||||
|
struct host **hash;
|
||||||
|
int index;
|
||||||
|
} state6;
|
||||||
|
|
||||||
|
static struct host6 *host_to_host6(const struct host *h)
|
||||||
|
{
|
||||||
|
return (struct host6 *) h;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* allocate state6 memory only when needed
|
||||||
|
*/
|
||||||
|
static bool state6_alloc_mem(void)
|
||||||
|
{
|
||||||
|
if (state6.hash != NULL)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
state6.list = vmalloc(LIST_SIZE * sizeof(struct host6));
|
||||||
|
if (state6.list == NULL)
|
||||||
|
return false;
|
||||||
|
memset(state6.list, 0, LIST_SIZE * sizeof(struct host6));
|
||||||
|
|
||||||
|
state6.hash = vmalloc(HASH_SIZE * sizeof(struct host*));
|
||||||
|
if (state6.hash == NULL) {
|
||||||
|
vfree(state6.list);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
memset(state6.hash, 0, HASH_SIZE * sizeof(struct host *));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Convert an IP address into a hash table index.
|
* Convert an IP address into a hash table index.
|
||||||
*/
|
*/
|
||||||
static inline int hashfunc(struct in_addr addr)
|
static unsigned int hashfunc(__be32 addr)
|
||||||
{
|
{
|
||||||
unsigned int value;
|
unsigned int value;
|
||||||
int hash;
|
unsigned int hash;
|
||||||
|
|
||||||
value = addr.s_addr;
|
value = addr;
|
||||||
hash = 0;
|
hash = 0;
|
||||||
do {
|
do {
|
||||||
hash ^= value;
|
hash ^= value;
|
||||||
@@ -106,133 +170,169 @@ static inline int hashfunc(struct in_addr addr)
|
|||||||
return hash & (HASH_SIZE - 1);
|
return hash & (HASH_SIZE - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool
|
static inline unsigned int hashfunc6(const struct in6_addr *addr)
|
||||||
xt_psd_match(const struct sk_buff *pskb, struct xt_action_param *match)
|
|
||||||
{
|
{
|
||||||
const struct iphdr *iph;
|
__be32 h = addr->s6_addr32[0] ^ addr->s6_addr32[1];
|
||||||
const struct tcphdr *tcph = NULL;
|
return hashfunc(h ^ addr->s6_addr32[2] ^ addr->s6_addr32[3]);
|
||||||
const struct udphdr *udph;
|
}
|
||||||
union {
|
|
||||||
struct tcphdr tcph;
|
|
||||||
struct udphdr udph;
|
|
||||||
} _buf;
|
|
||||||
struct in_addr addr;
|
|
||||||
u_int16_t src_port,dest_port;
|
|
||||||
u_int8_t proto;
|
|
||||||
unsigned long now;
|
|
||||||
struct host *curr, *last, **head;
|
|
||||||
int hash, index, count;
|
|
||||||
/* Parameters from userspace */
|
|
||||||
const struct xt_psd_info *psdinfo = match->matchinfo;
|
|
||||||
|
|
||||||
iph = ip_hdr(pskb);
|
static bool port_in_list(struct host *host, uint8_t proto, uint16_t port)
|
||||||
if (iph->frag_off & htons(IP_OFFSET)) {
|
{
|
||||||
pr_debug("sanity check failed\n");
|
unsigned int i;
|
||||||
return false;
|
|
||||||
|
for (i = 0; i < host->count; ++i) {
|
||||||
|
if (host->ports[i].proto != proto)
|
||||||
|
continue;
|
||||||
|
if (host->ports[i].number == port)
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
proto = iph->protocol;
|
|
||||||
addr.s_addr = iph->saddr;
|
|
||||||
/* We're using IP address 0.0.0.0 for a special purpose here, so don't let
|
|
||||||
* them spoof us. [DHCP needs this feature - HW] */
|
|
||||||
if (addr.s_addr == 0) {
|
|
||||||
pr_debug("spoofed source address (0.0.0.0)\n");
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (proto == IPPROTO_TCP) {
|
static uint16_t get_port_weight(const struct xt_psd_info *psd, __be16 port)
|
||||||
tcph = skb_header_pointer(pskb, match->thoff,
|
{
|
||||||
sizeof(_buf.tcph), &_buf.tcph);
|
return ntohs(port) < 1024 ? psd->lo_ports_weight : psd->hi_ports_weight;
|
||||||
if (tcph == NULL)
|
}
|
||||||
|
|
||||||
|
static bool
|
||||||
|
is_portscan(struct host *host, const struct xt_psd_info *psdinfo,
|
||||||
|
const struct tcphdr *tcph, uint8_t proto)
|
||||||
|
{
|
||||||
|
if (port_in_list(host, proto, tcph->dest))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
/* Yep, it's dirty */
|
/*
|
||||||
src_port = tcph->source;
|
* TCP/ACK and/or TCP/RST to a new port? This could be an
|
||||||
dest_port = tcph->dest;
|
* outgoing connection.
|
||||||
} else if (proto == IPPROTO_UDP || proto == IPPROTO_UDPLITE) {
|
*/
|
||||||
udph = skb_header_pointer(pskb, match->thoff,
|
|
||||||
sizeof(_buf.udph), &_buf.udph);
|
|
||||||
if (udph == NULL)
|
|
||||||
return false;
|
|
||||||
src_port = udph->source;
|
|
||||||
dest_port = udph->dest;
|
|
||||||
} else {
|
|
||||||
pr_debug("protocol not supported\n");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
now = jiffies;
|
|
||||||
|
|
||||||
spin_lock(&state.lock);
|
|
||||||
|
|
||||||
/* Do we know this source address already? */
|
|
||||||
count = 0;
|
|
||||||
last = NULL;
|
|
||||||
if ((curr = *(head = &state.hash[hash = hashfunc(addr)])) != NULL)
|
|
||||||
do {
|
|
||||||
if (curr->src_addr.s_addr == addr.s_addr)
|
|
||||||
break;
|
|
||||||
count++;
|
|
||||||
if (curr->next != NULL)
|
|
||||||
last = curr;
|
|
||||||
} while ((curr = curr->next) != NULL);
|
|
||||||
|
|
||||||
if (curr != NULL) {
|
|
||||||
/* We know this address, and the entry isn't too old. Update it. */
|
|
||||||
if (now - curr->timestamp <= (psdinfo->delay_threshold*HZ)/100 &&
|
|
||||||
time_after_eq(now, curr->timestamp)) {
|
|
||||||
|
|
||||||
/* Just update the appropriate list entry if we've seen this port already */
|
|
||||||
for (index = 0; index < curr->count; index++) {
|
|
||||||
if (curr->ports[index].number == dest_port) {
|
|
||||||
curr->ports[index].proto = proto;
|
|
||||||
goto out_no_match;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* TCP/ACK and/or TCP/RST to a new port? This could be an outgoing connection. */
|
|
||||||
if (proto == IPPROTO_TCP && (tcph->ack || tcph->rst))
|
if (proto == IPPROTO_TCP && (tcph->ack || tcph->rst))
|
||||||
goto out_no_match;
|
return false;
|
||||||
|
|
||||||
/* Packet to a new port, and not TCP/ACK: update the timestamp */
|
host->timestamp = jiffies;
|
||||||
curr->timestamp = now;
|
|
||||||
|
|
||||||
/* Matched this scan already? Then Leave. */
|
if (host->weight >= psdinfo->weight_threshold) /* already matched */
|
||||||
if (curr->weight >= psdinfo->weight_threshold)
|
return true;
|
||||||
goto out_match;
|
|
||||||
|
|
||||||
/* Update the total weight */
|
/* Update the total weight */
|
||||||
curr->weight += (ntohs(dest_port) < 1024) ?
|
host->weight += get_port_weight(psdinfo, tcph->dest);
|
||||||
psdinfo->lo_ports_weight : psdinfo->hi_ports_weight;
|
|
||||||
|
|
||||||
/* Got enough destination ports to decide that this is a scan? */
|
/* Got enough destination ports to decide that this is a scan? */
|
||||||
if (curr->weight >= psdinfo->weight_threshold)
|
if (host->weight >= psdinfo->weight_threshold)
|
||||||
goto out_match;
|
return true;
|
||||||
|
|
||||||
/* Remember the new port */
|
/* Remember the new port */
|
||||||
if (curr->count < ARRAY_SIZE(curr->ports)) {
|
if (host->count < ARRAY_SIZE(host->ports)) {
|
||||||
curr->ports[curr->count].number = dest_port;
|
host->ports[host->count].number = tcph->dest;
|
||||||
curr->ports[curr->count].proto = proto;
|
host->ports[host->count].proto = proto;
|
||||||
curr->count++;
|
host->count++;
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
goto out_no_match;
|
static struct host *host_get_next(struct host *h, struct host **last)
|
||||||
}
|
{
|
||||||
|
if (h->next != NULL)
|
||||||
|
*last = h;
|
||||||
|
return h->next;
|
||||||
|
}
|
||||||
|
|
||||||
/* We know this address, but the entry is outdated. Mark it unused, and
|
static void ht_unlink(struct host **head, struct host *last)
|
||||||
* remove from the hash table. We'll allocate a new entry instead since
|
{
|
||||||
* this one might get re-used too soon. */
|
|
||||||
curr->src_addr.s_addr = 0;
|
|
||||||
if (last != NULL)
|
if (last != NULL)
|
||||||
last->next = last->next->next;
|
last->next = last->next->next;
|
||||||
else if (*head != NULL)
|
else if (*head != NULL)
|
||||||
*head = (*head)->next;
|
*head = (*head)->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool
|
||||||
|
entry_is_recent(const struct host *h, unsigned long delay_threshold,
|
||||||
|
unsigned long now)
|
||||||
|
{
|
||||||
|
return now - h->timestamp <= (delay_threshold * HZ) / 100 &&
|
||||||
|
time_after_eq(now, h->timestamp);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void remove_oldest(struct host **head, struct host *curr)
|
||||||
|
{
|
||||||
|
struct host *h, *last = NULL;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* We are going to re-use the oldest list entry, so remove it from the
|
||||||
|
* hash table first, if it is really already in use.
|
||||||
|
*/
|
||||||
|
h = *head;
|
||||||
|
while (h != NULL) {
|
||||||
|
if (curr == h)
|
||||||
|
break;
|
||||||
|
last = h;
|
||||||
|
h = h->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Then, remove it */
|
||||||
|
if (h != NULL)
|
||||||
|
ht_unlink(head, last);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void *
|
||||||
|
get_header_pointer4(const struct sk_buff *skb, unsigned int thoff, void *mem)
|
||||||
|
{
|
||||||
|
const struct iphdr *iph = ip_hdr(skb);
|
||||||
|
int hdrlen;
|
||||||
|
|
||||||
|
switch (iph->protocol) {
|
||||||
|
case IPPROTO_TCP:
|
||||||
|
hdrlen = sizeof(struct tcphdr);
|
||||||
|
break;
|
||||||
|
case IPPROTO_UDP:
|
||||||
|
case IPPROTO_UDPLITE:
|
||||||
|
hdrlen = sizeof(struct udphdr);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return skb_header_pointer(skb, thoff, hdrlen, mem);
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool
|
||||||
|
handle_packet4(const struct iphdr *iph, const struct tcphdr *tcph,
|
||||||
|
const struct xt_psd_info *psdinfo, unsigned int hash)
|
||||||
|
{
|
||||||
|
unsigned long now;
|
||||||
|
struct host *curr, *last = NULL, **head;
|
||||||
|
struct host4 *curr4;
|
||||||
|
int count = 0;
|
||||||
|
|
||||||
|
now = jiffies;
|
||||||
|
head = &state.hash[hash];
|
||||||
|
|
||||||
|
/* Do we know this source address already? */
|
||||||
|
curr = *head;
|
||||||
|
while (curr != NULL) {
|
||||||
|
curr4 = host_to_host4(curr);
|
||||||
|
if (curr4->saddr == iph->saddr)
|
||||||
|
break;
|
||||||
|
count++;
|
||||||
|
curr = host_get_next(curr, &last);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (curr != NULL) {
|
||||||
|
/* We know this address, and the entry isn't too old. Update it. */
|
||||||
|
if (entry_is_recent(curr, psdinfo->delay_threshold, now))
|
||||||
|
return is_portscan(curr, psdinfo, tcph, iph->protocol);
|
||||||
|
|
||||||
|
/* We know this address, but the entry is outdated. Mark it unused, and
|
||||||
|
* remove from the hash table. We'll allocate a new entry instead since
|
||||||
|
* this one might get re-used too soon. */
|
||||||
|
curr4 = host_to_host4(curr);
|
||||||
|
curr4->saddr = 0;
|
||||||
|
ht_unlink(head, last);
|
||||||
last = NULL;
|
last = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* We don't need an ACK from a new source address */
|
/* We don't need an ACK from a new source address */
|
||||||
if (proto == IPPROTO_TCP && tcph->ack)
|
if (iph->protocol == IPPROTO_TCP && tcph->ack)
|
||||||
goto out_no_match;
|
return false;
|
||||||
|
|
||||||
/* Got too many source addresses with the same hash value? Then remove the
|
/* Got too many source addresses with the same hash value? Then remove the
|
||||||
* oldest one from the hash table, so that they can't take too much of our
|
* oldest one from the hash table, so that they can't take too much of our
|
||||||
@@ -240,33 +340,15 @@ xt_psd_match(const struct sk_buff *pskb, struct xt_action_param *match)
|
|||||||
if (count >= HASH_MAX && last != NULL)
|
if (count >= HASH_MAX && last != NULL)
|
||||||
last->next = NULL;
|
last->next = NULL;
|
||||||
|
|
||||||
/* We're going to re-use the oldest list entry, so remove it from the hash
|
if (state.list[state.index].saddr != 0)
|
||||||
* table first (if it is really already in use, and isn't removed from the
|
head = &state.hash[hashfunc(state.list[state.index].saddr)];
|
||||||
* hash table already because of the HASH_MAX check above). */
|
|
||||||
|
|
||||||
/* First, find it */
|
|
||||||
if (state.list[state.index].src_addr.s_addr != 0)
|
|
||||||
head = &state.hash[hashfunc(state.list[state.index].src_addr)];
|
|
||||||
else
|
else
|
||||||
head = &last;
|
head = &last;
|
||||||
last = NULL;
|
|
||||||
if ((curr = *head) != NULL)
|
|
||||||
do {
|
|
||||||
if (curr == &state.list[state.index])
|
|
||||||
break;
|
|
||||||
last = curr;
|
|
||||||
} while ((curr = curr->next) != NULL);
|
|
||||||
|
|
||||||
/* Then, remove it */
|
|
||||||
if (curr != NULL) {
|
|
||||||
if (last != NULL)
|
|
||||||
last->next = last->next->next;
|
|
||||||
else if (*head != NULL)
|
|
||||||
*head = (*head)->next;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Get our list entry */
|
/* Get our list entry */
|
||||||
curr = &state.list[state.index++];
|
curr4 = &state.list[state.index++];
|
||||||
|
curr = &curr4->host;
|
||||||
|
remove_oldest(head, curr);
|
||||||
if (state.index >= LIST_SIZE)
|
if (state.index >= LIST_SIZE)
|
||||||
state.index = 0;
|
state.index = 0;
|
||||||
|
|
||||||
@@ -276,24 +358,173 @@ xt_psd_match(const struct sk_buff *pskb, struct xt_action_param *match)
|
|||||||
*head = curr;
|
*head = curr;
|
||||||
|
|
||||||
/* And fill in the fields */
|
/* And fill in the fields */
|
||||||
|
curr4 = host_to_host4(curr);
|
||||||
|
curr4->saddr = iph->saddr;
|
||||||
curr->timestamp = now;
|
curr->timestamp = now;
|
||||||
curr->src_addr = addr;
|
|
||||||
curr->dest_addr.s_addr = iph->daddr;
|
|
||||||
curr->src_port = src_port;
|
|
||||||
curr->count = 1;
|
curr->count = 1;
|
||||||
curr->weight = (ntohs(dest_port) < 1024) ? psdinfo->lo_ports_weight : psdinfo->hi_ports_weight;
|
curr->weight = get_port_weight(psdinfo, tcph->dest);
|
||||||
curr->ports[0].number = dest_port;
|
curr->ports[0].number = tcph->dest;
|
||||||
curr->ports[0].proto = proto;
|
curr->ports[0].proto = iph->protocol;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
out_no_match:
|
static bool
|
||||||
spin_unlock(&state.lock);
|
xt_psd_match(const struct sk_buff *pskb, struct xt_action_param *match)
|
||||||
|
{
|
||||||
|
struct iphdr *iph = ip_hdr(pskb);
|
||||||
|
struct tcphdr _tcph;
|
||||||
|
struct tcphdr *tcph;
|
||||||
|
bool matched;
|
||||||
|
unsigned int hash;
|
||||||
|
/* Parameters from userspace */
|
||||||
|
const struct xt_psd_info *psdinfo = match->matchinfo;
|
||||||
|
|
||||||
|
if (iph->frag_off & htons(IP_OFFSET)) {
|
||||||
|
pr_debug("sanity check failed\n");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* We are using IP address 0.0.0.0 for a special purpose here, so do
|
||||||
|
* not let them spoof us. [DHCP needs this feature - HW]
|
||||||
|
*/
|
||||||
|
if (iph->saddr == 0) {
|
||||||
|
pr_debug("spoofed source address (0.0.0.0)\n");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
tcph = get_header_pointer4(pskb, match->thoff, &_tcph);
|
||||||
|
if (tcph == NULL)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
out_match:
|
hash = hashfunc(iph->saddr);
|
||||||
|
|
||||||
|
spin_lock(&state.lock);
|
||||||
|
matched = handle_packet4(iph, tcph, psdinfo, hash);
|
||||||
spin_unlock(&state.lock);
|
spin_unlock(&state.lock);
|
||||||
return true;
|
return matched;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef WITH_IPV6
|
||||||
|
static bool
|
||||||
|
handle_packet6(const struct ipv6hdr *ip6h, const struct tcphdr *tcph,
|
||||||
|
const struct xt_psd_info *psdinfo, uint8_t proto, int hash)
|
||||||
|
{
|
||||||
|
unsigned long now;
|
||||||
|
struct host *curr, *last = NULL, **head;
|
||||||
|
struct host6 *curr6;
|
||||||
|
int count = 0;
|
||||||
|
|
||||||
|
now = jiffies;
|
||||||
|
head = &state6.hash[hash];
|
||||||
|
|
||||||
|
curr = *head;
|
||||||
|
while (curr != NULL) {
|
||||||
|
curr6 = host_to_host6(curr);
|
||||||
|
if (ipv6_addr_equal(&curr6->saddr, &ip6h->saddr))
|
||||||
|
break;
|
||||||
|
count++;
|
||||||
|
curr = host_get_next(curr, &last);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (curr != NULL) {
|
||||||
|
if (entry_is_recent(curr, psdinfo->delay_threshold, now))
|
||||||
|
return is_portscan(curr, psdinfo, tcph, proto);
|
||||||
|
curr6 = host_to_host6(curr);
|
||||||
|
memset(&curr6->saddr, 0, sizeof(curr6->saddr));
|
||||||
|
ht_unlink(head, last);
|
||||||
|
last = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (proto == IPPROTO_TCP && tcph->ack)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (count >= HASH_MAX && last != NULL)
|
||||||
|
last->next = NULL;
|
||||||
|
|
||||||
|
if (!ipv6_addr_any(&state6.list[state6.index].saddr))
|
||||||
|
head = &state6.hash[hashfunc6(&state6.list[state6.index].saddr)];
|
||||||
|
else
|
||||||
|
head = &last;
|
||||||
|
|
||||||
|
curr6 = &state6.list[state6.index++];
|
||||||
|
curr = &curr6->host;
|
||||||
|
remove_oldest(head, curr);
|
||||||
|
if (state6.index >= LIST_SIZE)
|
||||||
|
state6.index = 0;
|
||||||
|
|
||||||
|
head = &state6.hash[hash];
|
||||||
|
curr->next = *head;
|
||||||
|
*head = curr;
|
||||||
|
|
||||||
|
curr6 = host_to_host6(curr);
|
||||||
|
curr6->saddr = ip6h->saddr;
|
||||||
|
curr->timestamp = now;
|
||||||
|
curr->count = 1;
|
||||||
|
curr->weight = get_port_weight(psdinfo, tcph->dest);
|
||||||
|
curr->ports[0].number = tcph->dest;
|
||||||
|
curr->ports[0].proto = proto;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void *
|
||||||
|
get_header_pointer6(const struct sk_buff *skb, void *mem, uint8_t *proto)
|
||||||
|
{
|
||||||
|
static const uint8_t types[] = {IPPROTO_TCP,
|
||||||
|
IPPROTO_UDP, IPPROTO_UDPLITE};
|
||||||
|
unsigned int i, offset = 0;
|
||||||
|
int err;
|
||||||
|
size_t hdrlen;
|
||||||
|
|
||||||
|
for (i = 0; i < ARRAY_SIZE(types); ++i) {
|
||||||
|
err = ipv6_find_hdr(skb, &offset, types[i], NULL, NULL);
|
||||||
|
if (err < 0)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
switch (types[i]) {
|
||||||
|
case IPPROTO_TCP:
|
||||||
|
hdrlen = sizeof(struct tcphdr);
|
||||||
|
break;
|
||||||
|
case IPPROTO_UDP:
|
||||||
|
case IPPROTO_UDPLITE:
|
||||||
|
hdrlen = sizeof(struct udphdr);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
*proto = types[i];
|
||||||
|
return skb_header_pointer(skb, offset, hdrlen, mem);
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool
|
||||||
|
xt_psd_match6(const struct sk_buff *pskb, struct xt_action_param *match)
|
||||||
|
{
|
||||||
|
const struct ipv6hdr *ip6h = ipv6_hdr(pskb);
|
||||||
|
struct tcphdr _tcph;
|
||||||
|
struct tcphdr *tcph;
|
||||||
|
uint8_t proto = 0;
|
||||||
|
bool matched;
|
||||||
|
int hash;
|
||||||
|
const struct xt_psd_info *psdinfo = match->matchinfo;
|
||||||
|
|
||||||
|
if (ipv6_addr_any(&ip6h->saddr))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
tcph = get_header_pointer6(pskb, &_tcph, &proto);
|
||||||
|
if (tcph == NULL)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
hash = hashfunc6(&ip6h->saddr);
|
||||||
|
|
||||||
|
spin_lock(&state6.lock);
|
||||||
|
matched = handle_packet6(ip6h, tcph, psdinfo, proto, hash);
|
||||||
|
spin_unlock(&state6.lock);
|
||||||
|
return matched;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
static int psd_mt_check(const struct xt_mtchk_param *par)
|
static int psd_mt_check(const struct xt_mtchk_param *par)
|
||||||
{
|
{
|
||||||
const struct xt_psd_info *info = par->matchinfo;
|
const struct xt_psd_info *info = par->matchinfo;
|
||||||
@@ -315,7 +546,17 @@ static int psd_mt_check(const struct xt_mtchk_param *par)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct xt_match xt_psd_reg __read_mostly = {
|
#ifdef WITH_IPV6
|
||||||
|
static int psd_mt_check6(const struct xt_mtchk_param *par)
|
||||||
|
{
|
||||||
|
if (!state6_alloc_mem())
|
||||||
|
return -ENOMEM;
|
||||||
|
return psd_mt_check(par);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static struct xt_match xt_psd_reg[] __read_mostly = {
|
||||||
|
{
|
||||||
.name = "psd",
|
.name = "psd",
|
||||||
.family = NFPROTO_IPV4,
|
.family = NFPROTO_IPV4,
|
||||||
.revision = 1,
|
.revision = 1,
|
||||||
@@ -323,17 +564,35 @@ static struct xt_match xt_psd_reg __read_mostly = {
|
|||||||
.match = xt_psd_match,
|
.match = xt_psd_match,
|
||||||
.matchsize = sizeof(struct xt_psd_info),
|
.matchsize = sizeof(struct xt_psd_info),
|
||||||
.me = THIS_MODULE,
|
.me = THIS_MODULE,
|
||||||
|
#ifdef WITH_IPV6
|
||||||
|
}, {
|
||||||
|
.name = "psd",
|
||||||
|
.family = NFPROTO_IPV6,
|
||||||
|
.revision = 1,
|
||||||
|
.checkentry = psd_mt_check6,
|
||||||
|
.match = xt_psd_match6,
|
||||||
|
.matchsize = sizeof(struct xt_psd_info),
|
||||||
|
.me = THIS_MODULE,
|
||||||
|
#endif
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
static int __init xt_psd_init(void)
|
static int __init xt_psd_init(void)
|
||||||
{
|
{
|
||||||
spin_lock_init(&(state.lock));
|
spin_lock_init(&(state.lock));
|
||||||
return xt_register_match(&xt_psd_reg);
|
#ifdef WITH_IPV6
|
||||||
|
spin_lock_init(&(state6.lock));
|
||||||
|
#endif
|
||||||
|
return xt_register_matches(xt_psd_reg, ARRAY_SIZE(xt_psd_reg));
|
||||||
}
|
}
|
||||||
|
|
||||||
static void __exit xt_psd_exit(void)
|
static void __exit xt_psd_exit(void)
|
||||||
{
|
{
|
||||||
xt_unregister_match(&xt_psd_reg);
|
xt_unregister_matches(xt_psd_reg, ARRAY_SIZE(xt_psd_reg));
|
||||||
|
#ifdef WITH_IPV6
|
||||||
|
vfree(state6.list);
|
||||||
|
vfree(state6.hash);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
module_init(xt_psd_init);
|
module_init(xt_psd_init);
|
||||||
|
@@ -3,8 +3,6 @@
|
|||||||
# Converter for MaxMind CSV database to binary, for xt_geoip
|
# Converter for MaxMind CSV database to binary, for xt_geoip
|
||||||
# Copyright © Jan Engelhardt <jengelh@medozas.de>, 2008-2011
|
# Copyright © Jan Engelhardt <jengelh@medozas.de>, 2008-2011
|
||||||
#
|
#
|
||||||
# Use -b argument to create big-endian tables.
|
|
||||||
#
|
|
||||||
use Getopt::Long;
|
use Getopt::Long;
|
||||||
use IO::Handle;
|
use IO::Handle;
|
||||||
use Text::CSV_XS; # or trade for Text::CSV
|
use Text::CSV_XS; # or trade for Text::CSV
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
.TH xtables-addons 8 "v1.43 (2012-06-30)" "" "v1.43 (2012-06-30)"
|
.TH xtables-addons 8 "v1.47 (2012-10-15)" "" "v1.47 (2012-10-15)"
|
||||||
.SH Name
|
.SH Name
|
||||||
Xtables-addons \(em additional extensions for iptables, ip6tables, etc.
|
Xtables-addons \(em additional extensions for iptables, ip6tables, etc.
|
||||||
.SH Targets
|
.SH Targets
|
||||||
|
Reference in New Issue
Block a user