blob: 5f6f088ff06e02411a3ab145718351cf68b673d2 [file] [log] [blame]
Patrick McHardy96518512013-10-14 11:00:02 +02001/*
Patrick McHardyef1f7df2013-10-10 11:41:20 +02002 * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
Pablo Neira Ayuso09d27b82014-06-25 13:37:13 +02003 * Copyright (c) 2012-2014 Pablo Neira Ayuso <pablo@netfilter.org>
Patrick McHardy96518512013-10-14 11:00:02 +02004 *
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
12#include <linux/kernel.h>
13#include <linux/init.h>
14#include <linux/module.h>
15#include <linux/netlink.h>
16#include <linux/netfilter.h>
17#include <linux/netfilter/nf_tables.h>
18#include <net/netfilter/nf_tables.h>
19#include <net/netfilter/nf_log.h>
20#include <linux/netdevice.h>
21
22static const char *nft_log_null_prefix = "";
23
24struct nft_log {
25 struct nf_loginfo loginfo;
26 char *prefix;
Patrick McHardy96518512013-10-14 11:00:02 +020027};
28
29static void nft_log_eval(const struct nft_expr *expr,
Patrick McHardya55e22e2015-04-11 02:27:31 +010030 struct nft_regs *regs,
Patrick McHardy96518512013-10-14 11:00:02 +020031 const struct nft_pktinfo *pkt)
32{
33 const struct nft_log *priv = nft_expr_priv(expr);
Patrick McHardy96518512013-10-14 11:00:02 +020034
Eric W. Biederman88182a02015-09-18 14:33:01 -050035 nf_log_packet(pkt->net, pkt->pf, pkt->hook, pkt->skb, pkt->in,
Patrick McHardy96518512013-10-14 11:00:02 +020036 pkt->out, &priv->loginfo, "%s", priv->prefix);
37}
38
39static const struct nla_policy nft_log_policy[NFTA_LOG_MAX + 1] = {
40 [NFTA_LOG_GROUP] = { .type = NLA_U16 },
41 [NFTA_LOG_PREFIX] = { .type = NLA_STRING },
42 [NFTA_LOG_SNAPLEN] = { .type = NLA_U32 },
43 [NFTA_LOG_QTHRESHOLD] = { .type = NLA_U16 },
Pablo Neira Ayuso09d27b82014-06-25 13:37:13 +020044 [NFTA_LOG_LEVEL] = { .type = NLA_U32 },
45 [NFTA_LOG_FLAGS] = { .type = NLA_U32 },
Patrick McHardy96518512013-10-14 11:00:02 +020046};
47
48static int nft_log_init(const struct nft_ctx *ctx,
49 const struct nft_expr *expr,
50 const struct nlattr * const tb[])
51{
52 struct nft_log *priv = nft_expr_priv(expr);
53 struct nf_loginfo *li = &priv->loginfo;
54 const struct nlattr *nla;
Liping Zhangc2d9a422016-07-18 20:44:15 +080055 int err;
56
57 li->type = NF_LOG_TYPE_LOG;
58 if (tb[NFTA_LOG_LEVEL] != NULL &&
59 tb[NFTA_LOG_GROUP] != NULL)
60 return -EINVAL;
61 if (tb[NFTA_LOG_GROUP] != NULL)
62 li->type = NF_LOG_TYPE_ULOG;
Patrick McHardy96518512013-10-14 11:00:02 +020063
Patrick McHardy96518512013-10-14 11:00:02 +020064 nla = tb[NFTA_LOG_PREFIX];
65 if (nla != NULL) {
66 priv->prefix = kmalloc(nla_len(nla) + 1, GFP_KERNEL);
67 if (priv->prefix == NULL)
68 return -ENOMEM;
69 nla_strlcpy(priv->prefix, nla, nla_len(nla) + 1);
Pablo Neira Ayuso09d27b82014-06-25 13:37:13 +020070 } else {
Patrick McHardy96518512013-10-14 11:00:02 +020071 priv->prefix = (char *)nft_log_null_prefix;
Pablo Neira Ayuso09d27b82014-06-25 13:37:13 +020072 }
Patrick McHardy96518512013-10-14 11:00:02 +020073
Pablo Neira Ayuso09d27b82014-06-25 13:37:13 +020074 switch (li->type) {
75 case NF_LOG_TYPE_LOG:
76 if (tb[NFTA_LOG_LEVEL] != NULL) {
77 li->u.log.level =
Fengguang Wu5cbfda22014-06-29 13:55:08 +020078 ntohl(nla_get_be32(tb[NFTA_LOG_LEVEL]));
Pablo Neira Ayuso09d27b82014-06-25 13:37:13 +020079 } else {
Joe Perchesa81b2ce2015-03-23 11:50:10 -070080 li->u.log.level = LOGLEVEL_WARNING;
Pablo Neira Ayuso09d27b82014-06-25 13:37:13 +020081 }
Liping Zhang1bc4e012016-07-18 20:44:16 +080082 if (li->u.log.level > LOGLEVEL_DEBUG) {
83 err = -EINVAL;
84 goto err1;
85 }
86
Pablo Neira Ayuso09d27b82014-06-25 13:37:13 +020087 if (tb[NFTA_LOG_FLAGS] != NULL) {
88 li->u.log.logflags =
89 ntohl(nla_get_be32(tb[NFTA_LOG_FLAGS]));
90 }
91 break;
92 case NF_LOG_TYPE_ULOG:
93 li->u.ulog.group = ntohs(nla_get_be16(tb[NFTA_LOG_GROUP]));
94 if (tb[NFTA_LOG_SNAPLEN] != NULL) {
95 li->u.ulog.copy_len =
96 ntohl(nla_get_be32(tb[NFTA_LOG_SNAPLEN]));
97 }
98 if (tb[NFTA_LOG_QTHRESHOLD] != NULL) {
99 li->u.ulog.qthreshold =
100 ntohs(nla_get_be16(tb[NFTA_LOG_QTHRESHOLD]));
101 }
102 break;
Patrick McHardy96518512013-10-14 11:00:02 +0200103 }
104
Liping Zhangc2d9a422016-07-18 20:44:15 +0800105 err = nf_logger_find_get(ctx->afi->family, li->type);
106 if (err < 0)
107 goto err1;
108
109 return 0;
110
111err1:
112 if (priv->prefix != nft_log_null_prefix)
113 kfree(priv->prefix);
114 return err;
Patrick McHardy96518512013-10-14 11:00:02 +0200115}
116
Patrick McHardy62472bc2014-03-07 19:08:30 +0100117static void nft_log_destroy(const struct nft_ctx *ctx,
118 const struct nft_expr *expr)
Patrick McHardy96518512013-10-14 11:00:02 +0200119{
120 struct nft_log *priv = nft_expr_priv(expr);
Pablo Neira Ayuso85d30e22014-06-25 13:29:15 +0200121 struct nf_loginfo *li = &priv->loginfo;
Patrick McHardy96518512013-10-14 11:00:02 +0200122
123 if (priv->prefix != nft_log_null_prefix)
124 kfree(priv->prefix);
Pablo Neira Ayuso85d30e22014-06-25 13:29:15 +0200125
Liping Zhangf3bb5332016-06-08 20:43:17 +0800126 nf_logger_put(ctx->afi->family, li->type);
Patrick McHardy96518512013-10-14 11:00:02 +0200127}
128
129static int nft_log_dump(struct sk_buff *skb, const struct nft_expr *expr)
130{
131 const struct nft_log *priv = nft_expr_priv(expr);
132 const struct nf_loginfo *li = &priv->loginfo;
133
134 if (priv->prefix != nft_log_null_prefix)
135 if (nla_put_string(skb, NFTA_LOG_PREFIX, priv->prefix))
136 goto nla_put_failure;
Pablo Neira Ayuso09d27b82014-06-25 13:37:13 +0200137 switch (li->type) {
138 case NF_LOG_TYPE_LOG:
139 if (nla_put_be32(skb, NFTA_LOG_LEVEL, htonl(li->u.log.level)))
140 goto nla_put_failure;
141
142 if (li->u.log.logflags) {
143 if (nla_put_be32(skb, NFTA_LOG_FLAGS,
144 htonl(li->u.log.logflags)))
145 goto nla_put_failure;
146 }
147 break;
148 case NF_LOG_TYPE_ULOG:
Patrick McHardy96518512013-10-14 11:00:02 +0200149 if (nla_put_be16(skb, NFTA_LOG_GROUP, htons(li->u.ulog.group)))
150 goto nla_put_failure;
Pablo Neira Ayuso09d27b82014-06-25 13:37:13 +0200151
152 if (li->u.ulog.copy_len) {
153 if (nla_put_be32(skb, NFTA_LOG_SNAPLEN,
154 htonl(li->u.ulog.copy_len)))
155 goto nla_put_failure;
156 }
157 if (li->u.ulog.qthreshold) {
158 if (nla_put_be16(skb, NFTA_LOG_QTHRESHOLD,
159 htons(li->u.ulog.qthreshold)))
160 goto nla_put_failure;
161 }
162 break;
163 }
Patrick McHardy96518512013-10-14 11:00:02 +0200164 return 0;
165
166nla_put_failure:
167 return -1;
168}
169
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200170static struct nft_expr_type nft_log_type;
171static const struct nft_expr_ops nft_log_ops = {
172 .type = &nft_log_type,
Patrick McHardy96518512013-10-14 11:00:02 +0200173 .size = NFT_EXPR_SIZE(sizeof(struct nft_log)),
Patrick McHardy96518512013-10-14 11:00:02 +0200174 .eval = nft_log_eval,
175 .init = nft_log_init,
176 .destroy = nft_log_destroy,
177 .dump = nft_log_dump,
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200178};
179
180static struct nft_expr_type nft_log_type __read_mostly = {
181 .name = "log",
182 .ops = &nft_log_ops,
Patrick McHardy96518512013-10-14 11:00:02 +0200183 .policy = nft_log_policy,
184 .maxattr = NFTA_LOG_MAX,
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200185 .owner = THIS_MODULE,
Patrick McHardy96518512013-10-14 11:00:02 +0200186};
187
188static int __init nft_log_module_init(void)
189{
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200190 return nft_register_expr(&nft_log_type);
Patrick McHardy96518512013-10-14 11:00:02 +0200191}
192
193static void __exit nft_log_module_exit(void)
194{
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200195 nft_unregister_expr(&nft_log_type);
Patrick McHardy96518512013-10-14 11:00:02 +0200196}
197
198module_init(nft_log_module_init);
199module_exit(nft_log_module_exit);
200
201MODULE_LICENSE("GPL");
202MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
203MODULE_ALIAS_NFT_EXPR("log");