nft_log.c 5.3 KB

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