4 #include <net/netfilter/nf_tables.h>
5 #include <linux/netfilter/nfnetlink_osf.h>
8 enum nft_registers dreg:8;
13 static const struct nla_policy nft_osf_policy[NFTA_OSF_MAX + 1] = {
14 [NFTA_OSF_DREG] = { .type = NLA_U32 },
15 [NFTA_OSF_TTL] = { .type = NLA_U8 },
16 [NFTA_OSF_FLAGS] = { .type = NLA_U32 },
19 static void nft_osf_eval(const struct nft_expr *expr, struct nft_regs *regs,
20 const struct nft_pktinfo *pkt)
22 struct nft_osf *priv = nft_expr_priv(expr);
23 u32 *dest = ®s->data[priv->dreg];
24 struct sk_buff *skb = pkt->skb;
25 char os_match[NFT_OSF_MAXGENRELEN + 1];
26 const struct tcphdr *tcp;
27 struct nf_osf_data data;
30 tcp = skb_header_pointer(skb, ip_hdrlen(skb),
31 sizeof(struct tcphdr), &_tcph);
33 regs->verdict.code = NFT_BREAK;
37 regs->verdict.code = NFT_BREAK;
41 if (!nf_osf_find(skb, nf_osf_fingers, priv->ttl, &data)) {
42 strncpy((char *)dest, "unknown", NFT_OSF_MAXGENRELEN);
44 if (priv->flags & NFT_OSF_F_VERSION)
45 snprintf(os_match, NFT_OSF_MAXGENRELEN, "%s:%s",
46 data.genre, data.version);
48 strlcpy(os_match, data.genre, NFT_OSF_MAXGENRELEN);
50 strncpy((char *)dest, os_match, NFT_OSF_MAXGENRELEN);
54 static int nft_osf_init(const struct nft_ctx *ctx,
55 const struct nft_expr *expr,
56 const struct nlattr * const tb[])
58 struct nft_osf *priv = nft_expr_priv(expr);
63 if (tb[NFTA_OSF_TTL]) {
64 ttl = nla_get_u8(tb[NFTA_OSF_TTL]);
70 if (tb[NFTA_OSF_FLAGS]) {
71 flags = ntohl(nla_get_be32(tb[NFTA_OSF_FLAGS]));
72 if (flags != NFT_OSF_F_VERSION)
77 priv->dreg = nft_parse_register(tb[NFTA_OSF_DREG]);
78 err = nft_validate_register_store(ctx, priv->dreg, NULL,
79 NFT_DATA_VALUE, NFT_OSF_MAXGENRELEN);
86 static int nft_osf_dump(struct sk_buff *skb, const struct nft_expr *expr)
88 const struct nft_osf *priv = nft_expr_priv(expr);
90 if (nla_put_u8(skb, NFTA_OSF_TTL, priv->ttl))
93 if (nla_put_be32(skb, NFTA_OSF_FLAGS, ntohl(priv->flags)))
96 if (nft_dump_register(skb, NFTA_OSF_DREG, priv->dreg))
105 static int nft_osf_validate(const struct nft_ctx *ctx,
106 const struct nft_expr *expr,
107 const struct nft_data **data)
109 return nft_chain_validate_hooks(ctx->chain, (1 << NF_INET_LOCAL_IN) |
110 (1 << NF_INET_PRE_ROUTING) |
111 (1 << NF_INET_FORWARD));
114 static struct nft_expr_type nft_osf_type;
115 static const struct nft_expr_ops nft_osf_op = {
116 .eval = nft_osf_eval,
117 .size = NFT_EXPR_SIZE(sizeof(struct nft_osf)),
118 .init = nft_osf_init,
119 .dump = nft_osf_dump,
120 .type = &nft_osf_type,
121 .validate = nft_osf_validate,
124 static struct nft_expr_type nft_osf_type __read_mostly = {
127 .owner = THIS_MODULE,
128 .policy = nft_osf_policy,
129 .maxattr = NFTA_OSF_MAX,
132 static int __init nft_osf_module_init(void)
134 return nft_register_expr(&nft_osf_type);
137 static void __exit nft_osf_module_exit(void)
139 return nft_unregister_expr(&nft_osf_type);
142 module_init(nft_osf_module_init);
143 module_exit(nft_osf_module_exit);
145 MODULE_LICENSE("GPL");
146 MODULE_AUTHOR("Fernando Fernandez <ffmancera@riseup.net>");
147 MODULE_ALIAS_NFT_EXPR("osf");