Linux Audio

Check our new training course

Loading...
v5.14.15
  1// SPDX-License-Identifier: GPL-2.0-only
  2#include <net/ip.h>
  3#include <net/tcp.h>
  4
  5#include <net/netfilter/nf_tables.h>
  6#include <linux/netfilter/nfnetlink_osf.h>
  7
  8struct nft_osf {
  9	u8			dreg;
 10	u8			ttl;
 11	u32			flags;
 12};
 13
 14static const struct nla_policy nft_osf_policy[NFTA_OSF_MAX + 1] = {
 15	[NFTA_OSF_DREG]		= { .type = NLA_U32 },
 16	[NFTA_OSF_TTL]		= { .type = NLA_U8 },
 17	[NFTA_OSF_FLAGS]	= { .type = NLA_U32 },
 18};
 19
 20static void nft_osf_eval(const struct nft_expr *expr, struct nft_regs *regs,
 21			 const struct nft_pktinfo *pkt)
 22{
 23	struct nft_osf *priv = nft_expr_priv(expr);
 24	u32 *dest = &regs->data[priv->dreg];
 25	struct sk_buff *skb = pkt->skb;
 26	char os_match[NFT_OSF_MAXGENRELEN + 1];
 27	const struct tcphdr *tcp;
 28	struct nf_osf_data data;
 29	struct tcphdr _tcph;
 30
 31	if (pkt->tprot != IPPROTO_TCP) {
 32		regs->verdict.code = NFT_BREAK;
 33		return;
 34	}
 35
 36	tcp = skb_header_pointer(skb, ip_hdrlen(skb),
 37				 sizeof(struct tcphdr), &_tcph);
 38	if (!tcp) {
 39		regs->verdict.code = NFT_BREAK;
 40		return;
 41	}
 42	if (!tcp->syn) {
 43		regs->verdict.code = NFT_BREAK;
 44		return;
 45	}
 46
 47	if (!nf_osf_find(skb, nf_osf_fingers, priv->ttl, &data)) {
 48		strncpy((char *)dest, "unknown", NFT_OSF_MAXGENRELEN);
 49	} else {
 50		if (priv->flags & NFT_OSF_F_VERSION)
 51			snprintf(os_match, NFT_OSF_MAXGENRELEN, "%s:%s",
 52				 data.genre, data.version);
 53		else
 54			strlcpy(os_match, data.genre, NFT_OSF_MAXGENRELEN);
 55
 56		strncpy((char *)dest, os_match, NFT_OSF_MAXGENRELEN);
 57	}
 58}
 59
 60static int nft_osf_init(const struct nft_ctx *ctx,
 61			const struct nft_expr *expr,
 62			const struct nlattr * const tb[])
 63{
 64	struct nft_osf *priv = nft_expr_priv(expr);
 65	u32 flags;
 66	int err;
 67	u8 ttl;
 68
 69	if (!tb[NFTA_OSF_DREG])
 70		return -EINVAL;
 71
 72	if (tb[NFTA_OSF_TTL]) {
 73		ttl = nla_get_u8(tb[NFTA_OSF_TTL]);
 74		if (ttl > 2)
 75			return -EINVAL;
 76		priv->ttl = ttl;
 77	}
 78
 79	if (tb[NFTA_OSF_FLAGS]) {
 80		flags = ntohl(nla_get_be32(tb[NFTA_OSF_FLAGS]));
 81		if (flags != NFT_OSF_F_VERSION)
 82			return -EINVAL;
 83		priv->flags = flags;
 84	}
 85
 86	err = nft_parse_register_store(ctx, tb[NFTA_OSF_DREG], &priv->dreg,
 87				       NULL, NFT_DATA_VALUE,
 88				       NFT_OSF_MAXGENRELEN);
 89	if (err < 0)
 90		return err;
 91
 92	return 0;
 93}
 94
 95static int nft_osf_dump(struct sk_buff *skb, const struct nft_expr *expr)
 
 96{
 97	const struct nft_osf *priv = nft_expr_priv(expr);
 98
 99	if (nla_put_u8(skb, NFTA_OSF_TTL, priv->ttl))
100		goto nla_put_failure;
101
102	if (nla_put_be32(skb, NFTA_OSF_FLAGS, ntohl(priv->flags)))
103		goto nla_put_failure;
104
105	if (nft_dump_register(skb, NFTA_OSF_DREG, priv->dreg))
106		goto nla_put_failure;
107
108	return 0;
109
110nla_put_failure:
111	return -1;
112}
113
114static int nft_osf_validate(const struct nft_ctx *ctx,
115			    const struct nft_expr *expr,
116			    const struct nft_data **data)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117{
118	return nft_chain_validate_hooks(ctx->chain, (1 << NF_INET_LOCAL_IN) |
119						    (1 << NF_INET_PRE_ROUTING) |
120						    (1 << NF_INET_FORWARD));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121}
122
123static struct nft_expr_type nft_osf_type;
124static const struct nft_expr_ops nft_osf_op = {
125	.eval		= nft_osf_eval,
126	.size		= NFT_EXPR_SIZE(sizeof(struct nft_osf)),
127	.init		= nft_osf_init,
128	.dump		= nft_osf_dump,
129	.type		= &nft_osf_type,
130	.validate	= nft_osf_validate,
 
131};
132
133static struct nft_expr_type nft_osf_type __read_mostly = {
134	.ops		= &nft_osf_op,
135	.name		= "osf",
136	.owner		= THIS_MODULE,
137	.policy		= nft_osf_policy,
138	.maxattr	= NFTA_OSF_MAX,
139};
140
141static int __init nft_osf_module_init(void)
142{
143	return nft_register_expr(&nft_osf_type);
144}
145
146static void __exit nft_osf_module_exit(void)
147{
148	return nft_unregister_expr(&nft_osf_type);
149}
150
151module_init(nft_osf_module_init);
152module_exit(nft_osf_module_exit);
153
154MODULE_LICENSE("GPL");
155MODULE_AUTHOR("Fernando Fernandez <ffmancera@riseup.net>");
156MODULE_ALIAS_NFT_EXPR("osf");
157MODULE_DESCRIPTION("nftables passive OS fingerprint support");
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0-only
  2#include <net/ip.h>
  3#include <net/tcp.h>
  4
  5#include <net/netfilter/nf_tables.h>
  6#include <linux/netfilter/nfnetlink_osf.h>
  7
  8struct nft_osf {
  9	u8			dreg;
 10	u8			ttl;
 11	u32			flags;
 12};
 13
 14static const struct nla_policy nft_osf_policy[NFTA_OSF_MAX + 1] = {
 15	[NFTA_OSF_DREG]		= { .type = NLA_U32 },
 16	[NFTA_OSF_TTL]		= { .type = NLA_U8 },
 17	[NFTA_OSF_FLAGS]	= { .type = NLA_U32 },
 18};
 19
 20static void nft_osf_eval(const struct nft_expr *expr, struct nft_regs *regs,
 21			 const struct nft_pktinfo *pkt)
 22{
 23	struct nft_osf *priv = nft_expr_priv(expr);
 24	u32 *dest = &regs->data[priv->dreg];
 25	struct sk_buff *skb = pkt->skb;
 26	char os_match[NFT_OSF_MAXGENRELEN];
 27	const struct tcphdr *tcp;
 28	struct nf_osf_data data;
 29	struct tcphdr _tcph;
 30
 31	if (pkt->tprot != IPPROTO_TCP) {
 32		regs->verdict.code = NFT_BREAK;
 33		return;
 34	}
 35
 36	tcp = skb_header_pointer(skb, ip_hdrlen(skb),
 37				 sizeof(struct tcphdr), &_tcph);
 38	if (!tcp) {
 39		regs->verdict.code = NFT_BREAK;
 40		return;
 41	}
 42	if (!tcp->syn) {
 43		regs->verdict.code = NFT_BREAK;
 44		return;
 45	}
 46
 47	if (!nf_osf_find(skb, nf_osf_fingers, priv->ttl, &data)) {
 48		strscpy_pad((char *)dest, "unknown", NFT_OSF_MAXGENRELEN);
 49	} else {
 50		if (priv->flags & NFT_OSF_F_VERSION)
 51			snprintf(os_match, NFT_OSF_MAXGENRELEN, "%s:%s",
 52				 data.genre, data.version);
 53		else
 54			strscpy(os_match, data.genre, NFT_OSF_MAXGENRELEN);
 55
 56		strscpy_pad((char *)dest, os_match, NFT_OSF_MAXGENRELEN);
 57	}
 58}
 59
 60static int nft_osf_init(const struct nft_ctx *ctx,
 61			const struct nft_expr *expr,
 62			const struct nlattr * const tb[])
 63{
 64	struct nft_osf *priv = nft_expr_priv(expr);
 65	u32 flags;
 
 66	u8 ttl;
 67
 68	if (!tb[NFTA_OSF_DREG])
 69		return -EINVAL;
 70
 71	if (tb[NFTA_OSF_TTL]) {
 72		ttl = nla_get_u8(tb[NFTA_OSF_TTL]);
 73		if (ttl > 2)
 74			return -EINVAL;
 75		priv->ttl = ttl;
 76	}
 77
 78	if (tb[NFTA_OSF_FLAGS]) {
 79		flags = ntohl(nla_get_be32(tb[NFTA_OSF_FLAGS]));
 80		if (flags != NFT_OSF_F_VERSION)
 81			return -EINVAL;
 82		priv->flags = flags;
 83	}
 84
 85	return nft_parse_register_store(ctx, tb[NFTA_OSF_DREG], &priv->dreg,
 86					NULL, NFT_DATA_VALUE,
 87					NFT_OSF_MAXGENRELEN);
 
 
 
 
 88}
 89
 90static int nft_osf_dump(struct sk_buff *skb,
 91			const struct nft_expr *expr, bool reset)
 92{
 93	const struct nft_osf *priv = nft_expr_priv(expr);
 94
 95	if (nla_put_u8(skb, NFTA_OSF_TTL, priv->ttl))
 96		goto nla_put_failure;
 97
 98	if (nla_put_u32(skb, NFTA_OSF_FLAGS, ntohl((__force __be32)priv->flags)))
 99		goto nla_put_failure;
100
101	if (nft_dump_register(skb, NFTA_OSF_DREG, priv->dreg))
102		goto nla_put_failure;
103
104	return 0;
105
106nla_put_failure:
107	return -1;
108}
109
110static int nft_osf_validate(const struct nft_ctx *ctx,
111			    const struct nft_expr *expr)
112{
113	unsigned int hooks;
114
115	switch (ctx->family) {
116	case NFPROTO_IPV4:
117	case NFPROTO_IPV6:
118	case NFPROTO_INET:
119		hooks = (1 << NF_INET_LOCAL_IN) |
120			(1 << NF_INET_PRE_ROUTING) |
121			(1 << NF_INET_FORWARD);
122		break;
123	default:
124		return -EOPNOTSUPP;
125	}
126
127	return nft_chain_validate_hooks(ctx->chain, hooks);
128}
129
130static bool nft_osf_reduce(struct nft_regs_track *track,
131			   const struct nft_expr *expr)
132{
133	struct nft_osf *priv = nft_expr_priv(expr);
134	struct nft_osf *osf;
135
136	if (!nft_reg_track_cmp(track, expr, priv->dreg)) {
137		nft_reg_track_update(track, expr, priv->dreg, NFT_OSF_MAXGENRELEN);
138		return false;
139	}
140
141	osf = nft_expr_priv(track->regs[priv->dreg].selector);
142	if (priv->flags != osf->flags ||
143	    priv->ttl != osf->ttl) {
144		nft_reg_track_update(track, expr, priv->dreg, NFT_OSF_MAXGENRELEN);
145		return false;
146	}
147
148	if (!track->regs[priv->dreg].bitwise)
149		return true;
150
151	return false;
152}
153
154static struct nft_expr_type nft_osf_type;
155static const struct nft_expr_ops nft_osf_op = {
156	.eval		= nft_osf_eval,
157	.size		= NFT_EXPR_SIZE(sizeof(struct nft_osf)),
158	.init		= nft_osf_init,
159	.dump		= nft_osf_dump,
160	.type		= &nft_osf_type,
161	.validate	= nft_osf_validate,
162	.reduce		= nft_osf_reduce,
163};
164
165static struct nft_expr_type nft_osf_type __read_mostly = {
166	.ops		= &nft_osf_op,
167	.name		= "osf",
168	.owner		= THIS_MODULE,
169	.policy		= nft_osf_policy,
170	.maxattr	= NFTA_OSF_MAX,
171};
172
173static int __init nft_osf_module_init(void)
174{
175	return nft_register_expr(&nft_osf_type);
176}
177
178static void __exit nft_osf_module_exit(void)
179{
180	return nft_unregister_expr(&nft_osf_type);
181}
182
183module_init(nft_osf_module_init);
184module_exit(nft_osf_module_exit);
185
186MODULE_LICENSE("GPL");
187MODULE_AUTHOR("Fernando Fernandez <ffmancera@riseup.net>");
188MODULE_ALIAS_NFT_EXPR("osf");
189MODULE_DESCRIPTION("nftables passive OS fingerprint support");