nft_log.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. [NFTA_LOG_SNAPLEN] = { .type = NLA_U32 },
  37. [NFTA_LOG_QTHRESHOLD] = { .type = NLA_U16 },
  38. [NFTA_LOG_LEVEL] = { .type = NLA_U32 },
  39. [NFTA_LOG_FLAGS] = { .type = NLA_U32 },
  40. };
  41. static int nft_log_init(const struct nft_ctx *ctx,
  42. const struct nft_expr *expr,
  43. const struct nlattr * const tb[])
  44. {
  45. struct nft_log *priv = nft_expr_priv(expr);
  46. struct nf_loginfo *li = &priv->loginfo;
  47. const struct nlattr *nla;
  48. int ret;
  49. nla = tb[NFTA_LOG_PREFIX];
  50. if (nla != NULL) {
  51. priv->prefix = kmalloc(nla_len(nla) + 1, GFP_KERNEL);
  52. if (priv->prefix == NULL)
  53. return -ENOMEM;
  54. nla_strlcpy(priv->prefix, nla, nla_len(nla) + 1);
  55. } else {
  56. priv->prefix = (char *)nft_log_null_prefix;
  57. }
  58. li->type = NF_LOG_TYPE_LOG;
  59. if (tb[NFTA_LOG_LEVEL] != NULL &&
  60. tb[NFTA_LOG_GROUP] != NULL)
  61. return -EINVAL;
  62. if (tb[NFTA_LOG_GROUP] != NULL)
  63. li->type = NF_LOG_TYPE_ULOG;
  64. switch (li->type) {
  65. case NF_LOG_TYPE_LOG:
  66. if (tb[NFTA_LOG_LEVEL] != NULL) {
  67. li->u.log.level =
  68. ntohl(nla_get_be32(tb[NFTA_LOG_LEVEL]));
  69. } else {
  70. li->u.log.level = LOGLEVEL_WARNING;
  71. }
  72. if (tb[NFTA_LOG_FLAGS] != NULL) {
  73. li->u.log.logflags =
  74. ntohl(nla_get_be32(tb[NFTA_LOG_FLAGS]));
  75. }
  76. break;
  77. case NF_LOG_TYPE_ULOG:
  78. li->u.ulog.group = ntohs(nla_get_be16(tb[NFTA_LOG_GROUP]));
  79. if (tb[NFTA_LOG_SNAPLEN] != NULL) {
  80. li->u.ulog.copy_len =
  81. ntohl(nla_get_be32(tb[NFTA_LOG_SNAPLEN]));
  82. }
  83. if (tb[NFTA_LOG_QTHRESHOLD] != NULL) {
  84. li->u.ulog.qthreshold =
  85. ntohs(nla_get_be16(tb[NFTA_LOG_QTHRESHOLD]));
  86. }
  87. break;
  88. }
  89. if (ctx->afi->family == NFPROTO_INET) {
  90. ret = nf_logger_find_get(NFPROTO_IPV4, li->type);
  91. if (ret < 0)
  92. return ret;
  93. ret = nf_logger_find_get(NFPROTO_IPV6, li->type);
  94. if (ret < 0) {
  95. nf_logger_put(NFPROTO_IPV4, li->type);
  96. return ret;
  97. }
  98. return 0;
  99. }
  100. return nf_logger_find_get(ctx->afi->family, li->type);
  101. }
  102. static void nft_log_destroy(const struct nft_ctx *ctx,
  103. const struct nft_expr *expr)
  104. {
  105. struct nft_log *priv = nft_expr_priv(expr);
  106. struct nf_loginfo *li = &priv->loginfo;
  107. if (priv->prefix != nft_log_null_prefix)
  108. kfree(priv->prefix);
  109. if (ctx->afi->family == NFPROTO_INET) {
  110. nf_logger_put(NFPROTO_IPV4, li->type);
  111. nf_logger_put(NFPROTO_IPV6, li->type);
  112. } else {
  113. nf_logger_put(ctx->afi->family, li->type);
  114. }
  115. }
  116. static int nft_log_dump(struct sk_buff *skb, const struct nft_expr *expr)
  117. {
  118. const struct nft_log *priv = nft_expr_priv(expr);
  119. const struct nf_loginfo *li = &priv->loginfo;
  120. if (priv->prefix != nft_log_null_prefix)
  121. if (nla_put_string(skb, NFTA_LOG_PREFIX, priv->prefix))
  122. goto nla_put_failure;
  123. switch (li->type) {
  124. case NF_LOG_TYPE_LOG:
  125. if (nla_put_be32(skb, NFTA_LOG_LEVEL, htonl(li->u.log.level)))
  126. goto nla_put_failure;
  127. if (li->u.log.logflags) {
  128. if (nla_put_be32(skb, NFTA_LOG_FLAGS,
  129. htonl(li->u.log.logflags)))
  130. goto nla_put_failure;
  131. }
  132. break;
  133. case NF_LOG_TYPE_ULOG:
  134. if (nla_put_be16(skb, NFTA_LOG_GROUP, htons(li->u.ulog.group)))
  135. goto nla_put_failure;
  136. if (li->u.ulog.copy_len) {
  137. if (nla_put_be32(skb, NFTA_LOG_SNAPLEN,
  138. htonl(li->u.ulog.copy_len)))
  139. goto nla_put_failure;
  140. }
  141. if (li->u.ulog.qthreshold) {
  142. if (nla_put_be16(skb, NFTA_LOG_QTHRESHOLD,
  143. htons(li->u.ulog.qthreshold)))
  144. goto nla_put_failure;
  145. }
  146. break;
  147. }
  148. return 0;
  149. nla_put_failure:
  150. return -1;
  151. }
  152. static struct nft_expr_type nft_log_type;
  153. static const struct nft_expr_ops nft_log_ops = {
  154. .type = &nft_log_type,
  155. .size = NFT_EXPR_SIZE(sizeof(struct nft_log)),
  156. .eval = nft_log_eval,
  157. .init = nft_log_init,
  158. .destroy = nft_log_destroy,
  159. .dump = nft_log_dump,
  160. };
  161. static struct nft_expr_type nft_log_type __read_mostly = {
  162. .name = "log",
  163. .ops = &nft_log_ops,
  164. .policy = nft_log_policy,
  165. .maxattr = NFTA_LOG_MAX,
  166. .owner = THIS_MODULE,
  167. };
  168. static int __init nft_log_module_init(void)
  169. {
  170. return nft_register_expr(&nft_log_type);
  171. }
  172. static void __exit nft_log_module_exit(void)
  173. {
  174. nft_unregister_expr(&nft_log_type);
  175. }
  176. module_init(nft_log_module_init);
  177. module_exit(nft_log_module_exit);
  178. MODULE_LICENSE("GPL");
  179. MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
  180. MODULE_ALIAS_NFT_EXPR("log");