nft_osf.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <net/ip.h>
  3. #include <net/tcp.h>
  4. #include <net/netfilter/nf_tables.h>
  5. #include <linux/netfilter/nfnetlink_osf.h>
  6. struct nft_osf {
  7. enum nft_registers dreg:8;
  8. u8 ttl;
  9. u32 flags;
  10. };
  11. static const struct nla_policy nft_osf_policy[NFTA_OSF_MAX + 1] = {
  12. [NFTA_OSF_DREG] = { .type = NLA_U32 },
  13. [NFTA_OSF_TTL] = { .type = NLA_U8 },
  14. [NFTA_OSF_FLAGS] = { .type = NLA_U32 },
  15. };
  16. static void nft_osf_eval(const struct nft_expr *expr, struct nft_regs *regs,
  17. const struct nft_pktinfo *pkt)
  18. {
  19. struct nft_osf *priv = nft_expr_priv(expr);
  20. u32 *dest = &regs->data[priv->dreg];
  21. struct sk_buff *skb = pkt->skb;
  22. char os_match[NFT_OSF_MAXGENRELEN + 1];
  23. const struct tcphdr *tcp;
  24. struct nf_osf_data data;
  25. struct tcphdr _tcph;
  26. if (pkt->tprot != IPPROTO_TCP) {
  27. regs->verdict.code = NFT_BREAK;
  28. return;
  29. }
  30. tcp = skb_header_pointer(skb, ip_hdrlen(skb),
  31. sizeof(struct tcphdr), &_tcph);
  32. if (!tcp) {
  33. regs->verdict.code = NFT_BREAK;
  34. return;
  35. }
  36. if (!tcp->syn) {
  37. regs->verdict.code = NFT_BREAK;
  38. return;
  39. }
  40. if (!nf_osf_find(skb, nf_osf_fingers, priv->ttl, &data)) {
  41. strncpy((char *)dest, "unknown", NFT_OSF_MAXGENRELEN);
  42. } else {
  43. if (priv->flags & NFT_OSF_F_VERSION)
  44. snprintf(os_match, NFT_OSF_MAXGENRELEN, "%s:%s",
  45. data.genre, data.version);
  46. else
  47. strlcpy(os_match, data.genre, NFT_OSF_MAXGENRELEN);
  48. strncpy((char *)dest, os_match, NFT_OSF_MAXGENRELEN);
  49. }
  50. }
  51. static int nft_osf_init(const struct nft_ctx *ctx,
  52. const struct nft_expr *expr,
  53. const struct nlattr * const tb[])
  54. {
  55. struct nft_osf *priv = nft_expr_priv(expr);
  56. u32 flags;
  57. int err;
  58. u8 ttl;
  59. if (!tb[NFTA_OSF_DREG])
  60. return -EINVAL;
  61. if (tb[NFTA_OSF_TTL]) {
  62. ttl = nla_get_u8(tb[NFTA_OSF_TTL]);
  63. if (ttl > 2)
  64. return -EINVAL;
  65. priv->ttl = ttl;
  66. }
  67. if (tb[NFTA_OSF_FLAGS]) {
  68. flags = ntohl(nla_get_be32(tb[NFTA_OSF_FLAGS]));
  69. if (flags != NFT_OSF_F_VERSION)
  70. return -EINVAL;
  71. priv->flags = flags;
  72. }
  73. priv->dreg = nft_parse_register(tb[NFTA_OSF_DREG]);
  74. err = nft_validate_register_store(ctx, priv->dreg, NULL,
  75. NFT_DATA_VALUE, NFT_OSF_MAXGENRELEN);
  76. if (err < 0)
  77. return err;
  78. return 0;
  79. }
  80. static int nft_osf_dump(struct sk_buff *skb, const struct nft_expr *expr)
  81. {
  82. const struct nft_osf *priv = nft_expr_priv(expr);
  83. if (nla_put_u8(skb, NFTA_OSF_TTL, priv->ttl))
  84. goto nla_put_failure;
  85. if (nla_put_be32(skb, NFTA_OSF_FLAGS, ntohl(priv->flags)))
  86. goto nla_put_failure;
  87. if (nft_dump_register(skb, NFTA_OSF_DREG, priv->dreg))
  88. goto nla_put_failure;
  89. return 0;
  90. nla_put_failure:
  91. return -1;
  92. }
  93. static int nft_osf_validate(const struct nft_ctx *ctx,
  94. const struct nft_expr *expr,
  95. const struct nft_data **data)
  96. {
  97. return nft_chain_validate_hooks(ctx->chain, (1 << NF_INET_LOCAL_IN) |
  98. (1 << NF_INET_PRE_ROUTING) |
  99. (1 << NF_INET_FORWARD));
  100. }
  101. static struct nft_expr_type nft_osf_type;
  102. static const struct nft_expr_ops nft_osf_op = {
  103. .eval = nft_osf_eval,
  104. .size = NFT_EXPR_SIZE(sizeof(struct nft_osf)),
  105. .init = nft_osf_init,
  106. .dump = nft_osf_dump,
  107. .type = &nft_osf_type,
  108. .validate = nft_osf_validate,
  109. };
  110. static struct nft_expr_type nft_osf_type __read_mostly = {
  111. .ops = &nft_osf_op,
  112. .name = "osf",
  113. .owner = THIS_MODULE,
  114. .policy = nft_osf_policy,
  115. .maxattr = NFTA_OSF_MAX,
  116. };
  117. static int __init nft_osf_module_init(void)
  118. {
  119. return nft_register_expr(&nft_osf_type);
  120. }
  121. static void __exit nft_osf_module_exit(void)
  122. {
  123. return nft_unregister_expr(&nft_osf_type);
  124. }
  125. module_init(nft_osf_module_init);
  126. module_exit(nft_osf_module_exit);
  127. MODULE_LICENSE("GPL");
  128. MODULE_AUTHOR("Fernando Fernandez <ffmancera@riseup.net>");
  129. MODULE_ALIAS_NFT_EXPR("osf");
  130. MODULE_DESCRIPTION("nftables passive OS fingerprint support");