nft_osf.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include <net/ip.h>
  2. #include <net/tcp.h>
  3. #include <net/netfilter/nf_tables.h>
  4. #include <linux/netfilter/nfnetlink_osf.h>
  5. struct nft_osf {
  6. enum nft_registers dreg:8;
  7. };
  8. static const struct nla_policy nft_osf_policy[NFTA_OSF_MAX + 1] = {
  9. [NFTA_OSF_DREG] = { .type = NLA_U32 },
  10. };
  11. static void nft_osf_eval(const struct nft_expr *expr, struct nft_regs *regs,
  12. const struct nft_pktinfo *pkt)
  13. {
  14. struct nft_osf *priv = nft_expr_priv(expr);
  15. u32 *dest = &regs->data[priv->dreg];
  16. struct sk_buff *skb = pkt->skb;
  17. const struct tcphdr *tcp;
  18. struct tcphdr _tcph;
  19. const char *os_name;
  20. tcp = skb_header_pointer(skb, ip_hdrlen(skb),
  21. sizeof(struct tcphdr), &_tcph);
  22. if (!tcp) {
  23. regs->verdict.code = NFT_BREAK;
  24. return;
  25. }
  26. if (!tcp->syn) {
  27. regs->verdict.code = NFT_BREAK;
  28. return;
  29. }
  30. os_name = nf_osf_find(skb, nf_osf_fingers);
  31. if (!os_name)
  32. strncpy((char *)dest, "unknown", NFT_OSF_MAXGENRELEN);
  33. else
  34. strncpy((char *)dest, os_name, NFT_OSF_MAXGENRELEN);
  35. }
  36. static int nft_osf_init(const struct nft_ctx *ctx,
  37. const struct nft_expr *expr,
  38. const struct nlattr * const tb[])
  39. {
  40. struct nft_osf *priv = nft_expr_priv(expr);
  41. int err;
  42. if (!tb[NFTA_OSF_DREG])
  43. return -EINVAL;
  44. priv->dreg = nft_parse_register(tb[NFTA_OSF_DREG]);
  45. err = nft_validate_register_store(ctx, priv->dreg, NULL,
  46. NFT_DATA_VALUE, NFT_OSF_MAXGENRELEN);
  47. if (err < 0)
  48. return err;
  49. return 0;
  50. }
  51. static int nft_osf_dump(struct sk_buff *skb, const struct nft_expr *expr)
  52. {
  53. const struct nft_osf *priv = nft_expr_priv(expr);
  54. if (nft_dump_register(skb, NFTA_OSF_DREG, priv->dreg))
  55. goto nla_put_failure;
  56. return 0;
  57. nla_put_failure:
  58. return -1;
  59. }
  60. static int nft_osf_validate(const struct nft_ctx *ctx,
  61. const struct nft_expr *expr,
  62. const struct nft_data **data)
  63. {
  64. return nft_chain_validate_hooks(ctx->chain, (1 << NF_INET_LOCAL_IN) |
  65. (1 << NF_INET_PRE_ROUTING) |
  66. (1 << NF_INET_FORWARD));
  67. }
  68. static struct nft_expr_type nft_osf_type;
  69. static const struct nft_expr_ops nft_osf_op = {
  70. .eval = nft_osf_eval,
  71. .size = NFT_EXPR_SIZE(sizeof(struct nft_osf)),
  72. .init = nft_osf_init,
  73. .dump = nft_osf_dump,
  74. .type = &nft_osf_type,
  75. .validate = nft_osf_validate,
  76. };
  77. static struct nft_expr_type nft_osf_type __read_mostly = {
  78. .ops = &nft_osf_op,
  79. .name = "osf",
  80. .owner = THIS_MODULE,
  81. .policy = nft_osf_policy,
  82. .maxattr = NFTA_OSF_MAX,
  83. };
  84. static int __init nft_osf_module_init(void)
  85. {
  86. return nft_register_expr(&nft_osf_type);
  87. }
  88. static void __exit nft_osf_module_exit(void)
  89. {
  90. return nft_unregister_expr(&nft_osf_type);
  91. }
  92. module_init(nft_osf_module_init);
  93. module_exit(nft_osf_module_exit);
  94. MODULE_LICENSE("GPL");
  95. MODULE_AUTHOR("Fernando Fernandez <ffmancera@riseup.net>");
  96. MODULE_ALIAS_NFT_EXPR("osf");