[PATCH 1/2 WIP nf-next] netfilter: implement Passive OS fingerprint module in nft_osf

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Add basic module functions into nft_osf.[ch] in order to start the
implementation of OSF module in nf_tables.

Signed-off-by: Fernando Fernandez Mancera <ffmancera@xxxxxxxxxx>
---
 include/uapi/linux/netfilter/nf_osf.h |   7 ++
 include/uapi/linux/netfilter/xt_osf.h |   6 +-
 net/netfilter/Kconfig                 |   7 ++
 net/netfilter/Makefile                |   1 +
 net/netfilter/nft_osf.c               | 108 ++++++++++++++++++++++++++
 5 files changed, 128 insertions(+), 1 deletion(-)
 create mode 100644 net/netfilter/nft_osf.c

diff --git a/include/uapi/linux/netfilter/nf_osf.h b/include/uapi/linux/netfilter/nf_osf.h
index 8f2f2f403183..79882b2f7f8e 100644
--- a/include/uapi/linux/netfilter/nf_osf.h
+++ b/include/uapi/linux/netfilter/nf_osf.h
@@ -16,9 +16,16 @@
 
 #define NF_OSF_TTL_TRUE			0	/* True ip and fingerprint TTL comparison */
 
+/* Check if ip TTL is less than fingerprint one */
+#define NF_OSF_TTL_LESS			1
+
 /* Do not compare ip and fingerprint TTL at all */
 #define NF_OSF_TTL_NOCHECK		2
 
+#define NF_OSF_FLAGMASK		(NF_OSF_GENRE | \
+				 NF_OSF_TTL | \
+				 NF_OSF_LOG | \
+				 NF_OSF_INVERT)
 /* Wildcard MSS (kind of).
  * It is used to implement a state machine for the different wildcard values
  * of the MSS and window sizes.
diff --git a/include/uapi/linux/netfilter/xt_osf.h b/include/uapi/linux/netfilter/xt_osf.h
index 72956eceeb09..2f5d4e6d0434 100644
--- a/include/uapi/linux/netfilter/xt_osf.h
+++ b/include/uapi/linux/netfilter/xt_osf.h
@@ -37,8 +37,12 @@
 
 #define XT_OSF_TTL_TRUE		NF_OSF_TTL_TRUE
 #define XT_OSF_TTL_NOCHECK	NF_OSF_TTL_NOCHECK
+#define XT_OSF_TTL_LESS		NF_OSF_TTL_LESS
 
-#define XT_OSF_TTL_LESS	1	/* Check if ip TTL is less than fingerprint one */
+#define XT_OSF_FLAGMASK		(XT_OSF_GENRE | \
+				XT_OSF_TTL | \
+				XT_OSF_LOG | \
+				XT_OSF_INVERT)
 
 #define xt_osf_wc		nf_osf_wc
 #define xt_osf_opt		nf_osf_opt
diff --git a/net/netfilter/Kconfig b/net/netfilter/Kconfig
index dbd7d1fad277..e630aac8a081 100644
--- a/net/netfilter/Kconfig
+++ b/net/netfilter/Kconfig
@@ -631,6 +631,13 @@ config NFT_SOCKET
 	  This option allows matching for the presence or absence of a
 	  corresponding socket and its attributes.
 
+config NFT_OSF
+	tristate "Netfilter nf_tables passive OS fingerprinting support"
+	depends on NETFILTER_ADVANCED && NETFILTER_NETLINK
+	select NF_OSF
+	help
+	  This option allows matching packets from an specific OS.
+
 if NF_TABLES_NETDEV
 
 config NF_DUP_NETDEV
diff --git a/net/netfilter/Makefile b/net/netfilter/Makefile
index 44449389e527..d15bd858ecef 100644
--- a/net/netfilter/Makefile
+++ b/net/netfilter/Makefile
@@ -104,6 +104,7 @@ obj-$(CONFIG_NFT_FIB_INET)	+= nft_fib_inet.o
 obj-$(CONFIG_NFT_FIB_NETDEV)	+= nft_fib_netdev.o
 obj-$(CONFIG_NF_OSF)		+= nf_osf.o
 obj-$(CONFIG_NFT_SOCKET)	+= nft_socket.o
+obj-$(CONFIG_NFT_OSF)		+= nft_osf.o
 
 # nf_tables netdev
 obj-$(CONFIG_NFT_DUP_NETDEV)	+= nft_dup_netdev.o
diff --git a/net/netfilter/nft_osf.c b/net/netfilter/nft_osf.c
new file mode 100644
index 000000000000..30c503f2bf53
--- /dev/null
+++ b/net/netfilter/nft_osf.c
@@ -0,0 +1,108 @@
+#include <net/netfilter/nf_tables.h>
+#include <linux/netfilter/nf_osf.h>
+
+#define OSF_GENRE_SIZE 32
+
+struct nft_osf {
+	char	genre[OSF_GENRE_SIZE];
+	__u32	flags;
+	__u32	loglevel;
+	__u32	ttl;
+	__u32	len;
+};
+
+/* placeholder function WIP */
+static inline bool match_packet(struct nft_osf *priv, struct sk_buff *skb)
+{
+	return 1;
+}
+
+static const struct nla_policy nft_osf_policy[NFTA_OSF_MAX + 1] = {
+	[NFTA_OSF_GENRE]	= { .type = NLA_STRING, .len = OSF_GENRE_SIZE },
+	[NFTA_OSF_FLAGS]	= { .type = NLA_U32 },
+	[NFTA_OSF_LOGLEVEL]	= { .type = NLA_U32 },
+	[NFTA_OSF_TTL]		= { .type = NLA_U32 },
+};
+
+static void nft_osf_eval(const struct nft_expr *expr, struct nft_regs *regs,
+			 const struct nft_pktinfo *pkt)
+{
+	struct nft_osf *priv = nft_expr_priv(expr);
+	struct sk_buff *skb = pkt->skb;
+
+	if (!match_packet(priv, skb))
+		regs->verdict.code = NFT_BREAK;
+}
+
+static int nft_osf_init(const struct nft_ctx *ctx,
+			const struct nft_expr *expr,
+			const struct nlattr * const tb[])
+{
+	struct nft_osf *priv = nft_expr_priv(expr);
+
+	if (tb[NFTA_OSF_GENRE] == NULL)
+		return -EINVAL;
+	nla_strlcpy(priv->genre, tb[NFTA_OSF_GENRE], OSF_GENRE_SIZE);
+	if (ntohl(nla_get_be32(tb[NFTA_OSF_FLAGS])) & ~NF_OSF_FLAGMASK)
+		return -EINVAL;
+	priv->flags	= ntohl(nla_get_be32(tb[NFTA_OSF_FLAGS]));
+	if (ntohl(nla_get_be32(tb[NFTA_OSF_LOGLEVEL])) >=
+			       NF_OSF_LOGLEVEL_ALL_KNOWN)
+		return -EINVAL;
+	priv->loglevel	= ntohl(nla_get_be32(tb[NFTA_OSF_LOGLEVEL]));
+	if (ntohl(nla_get_be32(tb[NFTA_OSF_TTL])) >= NF_OSF_TTL_NOCHECK)
+		return -EINVAL;
+	priv->ttl	= ntohl(nla_get_be32(tb[NFTA_OSF_TTL]));
+	priv->len = strlen(priv->genre);
+	return 0;
+}
+
+static int nft_osf_dump(struct sk_buff *skb, const struct nft_expr *expr)
+{
+	const struct nft_osf *priv = nft_expr_priv(expr);
+
+	if (nla_put_string(skb, NFTA_OSF_GENRE, priv->genre) ||
+	    nla_put_be32(skb, NFTA_OSF_FLAGS, htonl(priv->flags)) ||
+	    nla_put_be32(skb, NFTA_OSF_LOGLEVEL, htonl(priv->loglevel)) ||
+	    nla_put_be32(skb, NFTA_OSF_TTL, htonl(priv->ttl)))
+		goto nla_put_failure;
+	return 0;
+
+nla_put_failure:
+	return -1;
+}
+
+static struct nft_expr_type nft_osf_type;
+
+static const struct nft_expr_ops nft_osf_op = {
+	.eval = nft_osf_eval,
+	.size = NFT_EXPR_SIZE(sizeof(struct nft_osf)),
+	.init = nft_osf_init,
+	.dump = nft_osf_dump,
+	.type = &nft_osf_type,
+};
+
+static struct nft_expr_type nft_osf_type __read_mostly = {
+	.ops = &nft_osf_op,
+	.name = "osf",
+	.owner = THIS_MODULE,
+	.policy = nft_osf_policy,
+	.maxattr = NFTA_OSF_MAX,
+};
+
+static int __init nft_osf_module_init(void)
+{
+	return nft_register_expr(&nft_osf_type);
+}
+
+static void __exit nft_osf_module_exit(void)
+{
+	return nft_unregister_expr(&nft_osf_type);
+}
+
+module_init(nft_osf_module_init);
+module_exit(nft_osf_module_exit);
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Fernando Fernandez <ffmancera@xxxxxxxxxx>");
+MODULE_ALIAS_NFT_EXPR("osf");
-- 
2.18.0

--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[Index of Archives]     [Netfitler Users]     [LARTC]     [Bugtraq]     [Yosemite Forum]

  Powered by Linux