nft_log.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
  3. * Copyright (c) 2012-2014 Pablo Neira Ayuso <pablo@netfilter.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Development of this code funded by Astaro AG (http://www.astaro.com/)
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/init.h>
  13. #include <linux/module.h>
  14. #include <linux/netlink.h>
  15. #include <linux/netfilter.h>
  16. #include <linux/netfilter/nf_tables.h>
  17. #include <net/netfilter/nf_tables.h>
  18. #include <net/netfilter/nf_log.h>
  19. #include <linux/netdevice.h>
  20. static const char *nft_log_null_prefix = "";
  21. struct nft_log {
  22. struct nf_loginfo loginfo;
  23. char *prefix;
  24. };
  25. static void nft_log_eval(const struct nft_expr *expr,
  26. struct nft_regs *regs,
  27. const struct nft_pktinfo *pkt)
  28. {
  29. const struct nft_log *priv = nft_expr_priv(expr);
  30. nf_log_packet(pkt->net, pkt->pf, pkt->hook, pkt->skb, pkt->in,
  31. pkt->out, &priv->loginfo, "%s", priv->prefix);
  32. }
  33. static const struct nla_policy nft_log_policy[NFTA_LOG_MAX + 1] = {
  34. [NFTA_LOG_GROUP] = { .type = NLA_U16 },
  35. [NFTA_LOG_PREFIX] = { .type = NLA_STRING,
  36. .len = NF_LOG_PREFIXLEN - 1 },
  37. [NFTA_LOG_SNAPLEN] = { .type = NLA_U32 },
  38. [NFTA_LOG_QTHRESHOLD] = { .type = NLA_U16 },
  39. [NFTA_LOG_LEVEL] = { .type = NLA_U32 },
  40. [NFTA_LOG_FLAGS] = { .type = NLA_U32 },
  41. };
  42. static int nft_log_init(const struct nft_ctx *ctx,
  43. const struct nft_expr *expr,
  44. const struct nlattr * const tb[])
  45. {
  46. struct nft_log *priv = nft_expr_priv(expr);
  47. struct nf_loginfo *li = &priv->loginfo;
  48. const struct nlattr *nla;
  49. int err;
  50. li->type = NF_LOG_TYPE_LOG;
  51. if (tb[NFTA_LOG_LEVEL] != NULL &&
  52. tb[NFTA_LOG_GROUP] != NULL)
  53. return -EINVAL;
  54. if (tb[NFTA_LOG_GROUP] != NULL) {
  55. li->type = NF_LOG_TYPE_ULOG;
  56. if (tb[NFTA_LOG_FLAGS] != NULL)
  57. return -EINVAL;
  58. }
  59. nla = tb[NFTA_LOG_PREFIX];
  60. if (nla != NULL) {
  61. priv->prefix = kmalloc(nla_len(nla) + 1, GFP_KERNEL);
  62. if (priv->prefix == NULL)
  63. return -ENOMEM;
  64. nla_strlcpy(priv->prefix, nla, nla_len(nla) + 1);
  65. } else {
  66. priv->prefix = (char *)nft_log_null_prefix;
  67. }
  68. switch (li->type) {
  69. case NF_LOG_TYPE_LOG:
  70. if (tb[NFTA_LOG_LEVEL] != NULL) {
  71. li->u.log.level =
  72. ntohl(nla_get_be32(tb[NFTA_LOG_LEVEL]));
  73. } else {
  74. li->u.log.level = LOGLEVEL_WARNING;
  75. }
  76. if (li->u.log.level > LOGLEVEL_DEBUG) {
  77. err = -EINVAL;
  78. goto err1;
  79. }
  80. if (tb[NFTA_LOG_FLAGS] != NULL) {
  81. li->u.log.logflags =
  82. ntohl(nla_get_be32(tb[NFTA_LOG_FLAGS]));
  83. if (li->u.log.logflags & ~NF_LOG_MASK) {
  84. err = -EINVAL;
  85. goto err1;
  86. }
  87. }
  88. break;
  89. case NF_LOG_TYPE_ULOG:
  90. li->u.ulog.group = ntohs(nla_get_be16(tb[NFTA_LOG_GROUP]));
  91. if (tb[NFTA_LOG_SNAPLEN] != NULL) {
  92. li->u.ulog.flags |= NF_LOG_F_COPY_LEN;
  93. li->u.ulog.copy_len =
  94. ntohl(nla_get_be32(tb[NFTA_LOG_SNAPLEN]));
  95. }
  96. if (tb[NFTA_LOG_QTHRESHOLD] != NULL) {
  97. li->u.ulog.qthreshold =
  98. ntohs(nla_get_be16(tb[NFTA_LOG_QTHRESHOLD]));
  99. }
  100. break;
  101. }
  102. err = nf_logger_find_get(ctx->afi->family, li->type);
  103. if (err < 0)
  104. goto err1;
  105. return 0;
  106. err1:
  107. if (priv->prefix != nft_log_null_prefix)
  108. kfree(priv->prefix);
  109. return err;
  110. }
  111. static void nft_log_destroy(const struct nft_ctx *ctx,
  112. const struct nft_expr *expr)
  113. {
  114. struct nft_log *priv = nft_expr_priv(expr);
  115. struct nf_loginfo *li = &priv->loginfo;
  116. if (priv->prefix != nft_log_null_prefix)
  117. kfree(priv->prefix);
  118. nf_logger_put(ctx->afi->family, li->type);
  119. }
  120. static int nft_log_dump(struct sk_buff *skb, const struct nft_expr *expr)
  121. {
  122. const struct nft_log *priv = nft_expr_priv(expr);
  123. const struct nf_loginfo *li = &priv->loginfo;
  124. if (priv->prefix != nft_log_null_prefix)
  125. if (nla_put_string(skb, NFTA_LOG_PREFIX, priv->prefix))
  126. goto nla_put_failure;
  127. switch (li->type) {
  128. case NF_LOG_TYPE_LOG:
  129. if (nla_put_be32(skb, NFTA_LOG_LEVEL, htonl(li->u.log.level)))
  130. goto nla_put_failure;
  131. if (li->u.log.logflags) {
  132. if (nla_put_be32(skb, NFTA_LOG_FLAGS,
  133. htonl(li->u.log.logflags)))
  134. goto nla_put_failure;
  135. }
  136. break;
  137. case NF_LOG_TYPE_ULOG:
  138. if (nla_put_be16(skb, NFTA_LOG_GROUP, htons(li->u.ulog.group)))
  139. goto nla_put_failure;
  140. if (li->u.ulog.flags & NF_LOG_F_COPY_LEN) {
  141. if (nla_put_be32(skb, NFTA_LOG_SNAPLEN,
  142. htonl(li->u.ulog.copy_len)))
  143. goto nla_put_failure;
  144. }
  145. if (li->u.ulog.qthreshold) {
  146. if (nla_put_be16(skb, NFTA_LOG_QTHRESHOLD,
  147. htons(li->u.ulog.qthreshold)))
  148. goto nla_put_failure;
  149. }
  150. break;
  151. }
  152. return 0;
  153. nla_put_failure:
  154. return -1;
  155. }
  156. static struct nft_expr_type nft_log_type;
  157. static const struct nft_expr_ops nft_log_ops = {
  158. .type = &nft_log_type,
  159. .size = NFT_EXPR_SIZE(sizeof(struct nft_log)),
  160. .eval = nft_log_eval,
  161. .init = nft_log_init,
  162. .destroy = nft_log_destroy,
  163. .dump = nft_log_dump,
  164. };
  165. static struct nft_expr_type nft_log_type __read_mostly = {
  166. .name = "log",
  167. .ops = &nft_log_ops,
  168. .policy = nft_log_policy,
  169. .maxattr = NFTA_LOG_MAX,
  170. .owner = THIS_MODULE,
  171. };
  172. static int __init nft_log_module_init(void)
  173. {
  174. return nft_register_expr(&nft_log_type);
  175. }
  176. static void __exit nft_log_module_exit(void)
  177. {
  178. nft_unregister_expr(&nft_log_type);
  179. }
  180. module_init(nft_log_module_init);
  181. module_exit(nft_log_module_exit);
  182. MODULE_LICENSE("GPL");
  183. MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
  184. MODULE_ALIAS_NFT_EXPR("log");