blob: c7704e9123ef899b3faac5d8d6f08b9cd86c3ca7 [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 },
Liping Zhangc47538f2017-01-22 22:10:32 +080041 [NFTA_LOG_PREFIX] = { .type = NLA_STRING,
42 .len = NF_LOG_PREFIXLEN - 1 },
Patrick McHardy96518512013-10-14 11:00:02 +020043 [NFTA_LOG_SNAPLEN] = { .type = NLA_U32 },
44 [NFTA_LOG_QTHRESHOLD] = { .type = NLA_U16 },
Pablo Neira Ayuso09d27b82014-06-25 13:37:13 +020045 [NFTA_LOG_LEVEL] = { .type = NLA_U32 },
46 [NFTA_LOG_FLAGS] = { .type = NLA_U32 },
Patrick McHardy96518512013-10-14 11:00:02 +020047};
48
49static int nft_log_init(const struct nft_ctx *ctx,
50 const struct nft_expr *expr,
51 const struct nlattr * const tb[])
52{
53 struct nft_log *priv = nft_expr_priv(expr);
54 struct nf_loginfo *li = &priv->loginfo;
55 const struct nlattr *nla;
Liping Zhangc2d9a422016-07-18 20:44:15 +080056 int err;
57
58 li->type = NF_LOG_TYPE_LOG;
59 if (tb[NFTA_LOG_LEVEL] != NULL &&
60 tb[NFTA_LOG_GROUP] != NULL)
61 return -EINVAL;
Liping Zhangff107d22016-09-25 16:35:56 +080062 if (tb[NFTA_LOG_GROUP] != NULL) {
Liping Zhangc2d9a422016-07-18 20:44:15 +080063 li->type = NF_LOG_TYPE_ULOG;
Liping Zhangff107d22016-09-25 16:35:56 +080064 if (tb[NFTA_LOG_FLAGS] != NULL)
65 return -EINVAL;
66 }
Patrick McHardy96518512013-10-14 11:00:02 +020067
Patrick McHardy96518512013-10-14 11:00:02 +020068 nla = tb[NFTA_LOG_PREFIX];
69 if (nla != NULL) {
70 priv->prefix = kmalloc(nla_len(nla) + 1, GFP_KERNEL);
71 if (priv->prefix == NULL)
72 return -ENOMEM;
73 nla_strlcpy(priv->prefix, nla, nla_len(nla) + 1);
Pablo Neira Ayuso09d27b82014-06-25 13:37:13 +020074 } else {
Patrick McHardy96518512013-10-14 11:00:02 +020075 priv->prefix = (char *)nft_log_null_prefix;
Pablo Neira Ayuso09d27b82014-06-25 13:37:13 +020076 }
Patrick McHardy96518512013-10-14 11:00:02 +020077
Pablo Neira Ayuso09d27b82014-06-25 13:37:13 +020078 switch (li->type) {
79 case NF_LOG_TYPE_LOG:
80 if (tb[NFTA_LOG_LEVEL] != NULL) {
81 li->u.log.level =
Fengguang Wu5cbfda22014-06-29 13:55:08 +020082 ntohl(nla_get_be32(tb[NFTA_LOG_LEVEL]));
Pablo Neira Ayuso09d27b82014-06-25 13:37:13 +020083 } else {
Joe Perchesa81b2ce2015-03-23 11:50:10 -070084 li->u.log.level = LOGLEVEL_WARNING;
Pablo Neira Ayuso09d27b82014-06-25 13:37:13 +020085 }
Liping Zhang1bc4e012016-07-18 20:44:16 +080086 if (li->u.log.level > LOGLEVEL_DEBUG) {
87 err = -EINVAL;
88 goto err1;
89 }
90
Pablo Neira Ayuso09d27b82014-06-25 13:37:13 +020091 if (tb[NFTA_LOG_FLAGS] != NULL) {
92 li->u.log.logflags =
93 ntohl(nla_get_be32(tb[NFTA_LOG_FLAGS]));
Liping Zhangff107d22016-09-25 16:35:56 +080094 if (li->u.log.logflags & ~NF_LOG_MASK) {
95 err = -EINVAL;
96 goto err1;
97 }
Pablo Neira Ayuso09d27b82014-06-25 13:37:13 +020098 }
99 break;
100 case NF_LOG_TYPE_ULOG:
101 li->u.ulog.group = ntohs(nla_get_be16(tb[NFTA_LOG_GROUP]));
102 if (tb[NFTA_LOG_SNAPLEN] != NULL) {
Liping Zhangcc37c1a2016-07-18 20:44:17 +0800103 li->u.ulog.flags |= NF_LOG_F_COPY_LEN;
Pablo Neira Ayuso09d27b82014-06-25 13:37:13 +0200104 li->u.ulog.copy_len =
105 ntohl(nla_get_be32(tb[NFTA_LOG_SNAPLEN]));
106 }
107 if (tb[NFTA_LOG_QTHRESHOLD] != NULL) {
108 li->u.ulog.qthreshold =
109 ntohs(nla_get_be16(tb[NFTA_LOG_QTHRESHOLD]));
110 }
111 break;
Patrick McHardy96518512013-10-14 11:00:02 +0200112 }
113
Liping Zhangc2d9a422016-07-18 20:44:15 +0800114 err = nf_logger_find_get(ctx->afi->family, li->type);
115 if (err < 0)
116 goto err1;
117
118 return 0;
119
120err1:
121 if (priv->prefix != nft_log_null_prefix)
122 kfree(priv->prefix);
123 return err;
Patrick McHardy96518512013-10-14 11:00:02 +0200124}
125
Patrick McHardy62472bc2014-03-07 19:08:30 +0100126static void nft_log_destroy(const struct nft_ctx *ctx,
127 const struct nft_expr *expr)
Patrick McHardy96518512013-10-14 11:00:02 +0200128{
129 struct nft_log *priv = nft_expr_priv(expr);
Pablo Neira Ayuso85d30e22014-06-25 13:29:15 +0200130 struct nf_loginfo *li = &priv->loginfo;
Patrick McHardy96518512013-10-14 11:00:02 +0200131
132 if (priv->prefix != nft_log_null_prefix)
133 kfree(priv->prefix);
Pablo Neira Ayuso85d30e22014-06-25 13:29:15 +0200134
Liping Zhangf3bb5332016-06-08 20:43:17 +0800135 nf_logger_put(ctx->afi->family, li->type);
Patrick McHardy96518512013-10-14 11:00:02 +0200136}
137
138static int nft_log_dump(struct sk_buff *skb, const struct nft_expr *expr)
139{
140 const struct nft_log *priv = nft_expr_priv(expr);
141 const struct nf_loginfo *li = &priv->loginfo;
142
143 if (priv->prefix != nft_log_null_prefix)
144 if (nla_put_string(skb, NFTA_LOG_PREFIX, priv->prefix))
145 goto nla_put_failure;
Pablo Neira Ayuso09d27b82014-06-25 13:37:13 +0200146 switch (li->type) {
147 case NF_LOG_TYPE_LOG:
148 if (nla_put_be32(skb, NFTA_LOG_LEVEL, htonl(li->u.log.level)))
149 goto nla_put_failure;
150
151 if (li->u.log.logflags) {
152 if (nla_put_be32(skb, NFTA_LOG_FLAGS,
153 htonl(li->u.log.logflags)))
154 goto nla_put_failure;
155 }
156 break;
157 case NF_LOG_TYPE_ULOG:
Patrick McHardy96518512013-10-14 11:00:02 +0200158 if (nla_put_be16(skb, NFTA_LOG_GROUP, htons(li->u.ulog.group)))
159 goto nla_put_failure;
Pablo Neira Ayuso09d27b82014-06-25 13:37:13 +0200160
Liping Zhangcc37c1a2016-07-18 20:44:17 +0800161 if (li->u.ulog.flags & NF_LOG_F_COPY_LEN) {
Pablo Neira Ayuso09d27b82014-06-25 13:37:13 +0200162 if (nla_put_be32(skb, NFTA_LOG_SNAPLEN,
163 htonl(li->u.ulog.copy_len)))
164 goto nla_put_failure;
165 }
166 if (li->u.ulog.qthreshold) {
167 if (nla_put_be16(skb, NFTA_LOG_QTHRESHOLD,
168 htons(li->u.ulog.qthreshold)))
169 goto nla_put_failure;
170 }
171 break;
172 }
Patrick McHardy96518512013-10-14 11:00:02 +0200173 return 0;
174
175nla_put_failure:
176 return -1;
177}
178
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200179static struct nft_expr_type nft_log_type;
180static const struct nft_expr_ops nft_log_ops = {
181 .type = &nft_log_type,
Patrick McHardy96518512013-10-14 11:00:02 +0200182 .size = NFT_EXPR_SIZE(sizeof(struct nft_log)),
Patrick McHardy96518512013-10-14 11:00:02 +0200183 .eval = nft_log_eval,
184 .init = nft_log_init,
185 .destroy = nft_log_destroy,
186 .dump = nft_log_dump,
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200187};
188
189static struct nft_expr_type nft_log_type __read_mostly = {
190 .name = "log",
191 .ops = &nft_log_ops,
Patrick McHardy96518512013-10-14 11:00:02 +0200192 .policy = nft_log_policy,
193 .maxattr = NFTA_LOG_MAX,
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200194 .owner = THIS_MODULE,
Patrick McHardy96518512013-10-14 11:00:02 +0200195};
196
197static int __init nft_log_module_init(void)
198{
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200199 return nft_register_expr(&nft_log_type);
Patrick McHardy96518512013-10-14 11:00:02 +0200200}
201
202static void __exit nft_log_module_exit(void)
203{
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200204 nft_unregister_expr(&nft_log_type);
Patrick McHardy96518512013-10-14 11:00:02 +0200205}
206
207module_init(nft_log_module_init);
208module_exit(nft_log_module_exit);
209
210MODULE_LICENSE("GPL");
211MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
212MODULE_ALIAS_NFT_EXPR("log");