blob: 6f6e64423643a8c2991f2dedab65aff9e8c92720 [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
Pablo Neira Ayuso0e5a1c72016-11-03 10:56:26 +010035 nf_log_packet(nft_net(pkt), nft_pf(pkt), nft_hook(pkt), pkt->skb,
36 nft_in(pkt), nft_out(pkt), &priv->loginfo, "%s",
37 priv->prefix);
Patrick McHardy96518512013-10-14 11:00:02 +020038}
39
40static const struct nla_policy nft_log_policy[NFTA_LOG_MAX + 1] = {
41 [NFTA_LOG_GROUP] = { .type = NLA_U16 },
Liping Zhang5ce6b042017-01-22 22:10:32 +080042 [NFTA_LOG_PREFIX] = { .type = NLA_STRING,
43 .len = NF_LOG_PREFIXLEN - 1 },
Patrick McHardy96518512013-10-14 11:00:02 +020044 [NFTA_LOG_SNAPLEN] = { .type = NLA_U32 },
45 [NFTA_LOG_QTHRESHOLD] = { .type = NLA_U16 },
Pablo Neira Ayuso09d27b82014-06-25 13:37:13 +020046 [NFTA_LOG_LEVEL] = { .type = NLA_U32 },
47 [NFTA_LOG_FLAGS] = { .type = NLA_U32 },
Patrick McHardy96518512013-10-14 11:00:02 +020048};
49
50static int nft_log_init(const struct nft_ctx *ctx,
51 const struct nft_expr *expr,
52 const struct nlattr * const tb[])
53{
54 struct nft_log *priv = nft_expr_priv(expr);
55 struct nf_loginfo *li = &priv->loginfo;
56 const struct nlattr *nla;
Liping Zhangc2d9a422016-07-18 20:44:15 +080057 int err;
58
59 li->type = NF_LOG_TYPE_LOG;
60 if (tb[NFTA_LOG_LEVEL] != NULL &&
61 tb[NFTA_LOG_GROUP] != NULL)
62 return -EINVAL;
Liping Zhangff107d22016-09-25 16:35:56 +080063 if (tb[NFTA_LOG_GROUP] != NULL) {
Liping Zhangc2d9a422016-07-18 20:44:15 +080064 li->type = NF_LOG_TYPE_ULOG;
Liping Zhangff107d22016-09-25 16:35:56 +080065 if (tb[NFTA_LOG_FLAGS] != NULL)
66 return -EINVAL;
67 }
Patrick McHardy96518512013-10-14 11:00:02 +020068
Patrick McHardy96518512013-10-14 11:00:02 +020069 nla = tb[NFTA_LOG_PREFIX];
70 if (nla != NULL) {
71 priv->prefix = kmalloc(nla_len(nla) + 1, GFP_KERNEL);
72 if (priv->prefix == NULL)
73 return -ENOMEM;
74 nla_strlcpy(priv->prefix, nla, nla_len(nla) + 1);
Pablo Neira Ayuso09d27b82014-06-25 13:37:13 +020075 } else {
Patrick McHardy96518512013-10-14 11:00:02 +020076 priv->prefix = (char *)nft_log_null_prefix;
Pablo Neira Ayuso09d27b82014-06-25 13:37:13 +020077 }
Patrick McHardy96518512013-10-14 11:00:02 +020078
Pablo Neira Ayuso09d27b82014-06-25 13:37:13 +020079 switch (li->type) {
80 case NF_LOG_TYPE_LOG:
81 if (tb[NFTA_LOG_LEVEL] != NULL) {
82 li->u.log.level =
Fengguang Wu5cbfda22014-06-29 13:55:08 +020083 ntohl(nla_get_be32(tb[NFTA_LOG_LEVEL]));
Pablo Neira Ayuso09d27b82014-06-25 13:37:13 +020084 } else {
Joe Perchesa81b2ce2015-03-23 11:50:10 -070085 li->u.log.level = LOGLEVEL_WARNING;
Pablo Neira Ayuso09d27b82014-06-25 13:37:13 +020086 }
Liping Zhang1bc4e012016-07-18 20:44:16 +080087 if (li->u.log.level > LOGLEVEL_DEBUG) {
88 err = -EINVAL;
89 goto err1;
90 }
91
Pablo Neira Ayuso09d27b82014-06-25 13:37:13 +020092 if (tb[NFTA_LOG_FLAGS] != NULL) {
93 li->u.log.logflags =
94 ntohl(nla_get_be32(tb[NFTA_LOG_FLAGS]));
Liping Zhangff107d22016-09-25 16:35:56 +080095 if (li->u.log.logflags & ~NF_LOG_MASK) {
96 err = -EINVAL;
97 goto err1;
98 }
Pablo Neira Ayuso09d27b82014-06-25 13:37:13 +020099 }
100 break;
101 case NF_LOG_TYPE_ULOG:
102 li->u.ulog.group = ntohs(nla_get_be16(tb[NFTA_LOG_GROUP]));
103 if (tb[NFTA_LOG_SNAPLEN] != NULL) {
Liping Zhangcc37c1a2016-07-18 20:44:17 +0800104 li->u.ulog.flags |= NF_LOG_F_COPY_LEN;
Pablo Neira Ayuso09d27b82014-06-25 13:37:13 +0200105 li->u.ulog.copy_len =
106 ntohl(nla_get_be32(tb[NFTA_LOG_SNAPLEN]));
107 }
108 if (tb[NFTA_LOG_QTHRESHOLD] != NULL) {
109 li->u.ulog.qthreshold =
110 ntohs(nla_get_be16(tb[NFTA_LOG_QTHRESHOLD]));
111 }
112 break;
Patrick McHardy96518512013-10-14 11:00:02 +0200113 }
114
Liping Zhangc2d9a422016-07-18 20:44:15 +0800115 err = nf_logger_find_get(ctx->afi->family, li->type);
116 if (err < 0)
117 goto err1;
118
119 return 0;
120
121err1:
122 if (priv->prefix != nft_log_null_prefix)
123 kfree(priv->prefix);
124 return err;
Patrick McHardy96518512013-10-14 11:00:02 +0200125}
126
Patrick McHardy62472bc2014-03-07 19:08:30 +0100127static void nft_log_destroy(const struct nft_ctx *ctx,
128 const struct nft_expr *expr)
Patrick McHardy96518512013-10-14 11:00:02 +0200129{
130 struct nft_log *priv = nft_expr_priv(expr);
Pablo Neira Ayuso85d30e22014-06-25 13:29:15 +0200131 struct nf_loginfo *li = &priv->loginfo;
Patrick McHardy96518512013-10-14 11:00:02 +0200132
133 if (priv->prefix != nft_log_null_prefix)
134 kfree(priv->prefix);
Pablo Neira Ayuso85d30e22014-06-25 13:29:15 +0200135
Liping Zhangf3bb5332016-06-08 20:43:17 +0800136 nf_logger_put(ctx->afi->family, li->type);
Patrick McHardy96518512013-10-14 11:00:02 +0200137}
138
139static int nft_log_dump(struct sk_buff *skb, const struct nft_expr *expr)
140{
141 const struct nft_log *priv = nft_expr_priv(expr);
142 const struct nf_loginfo *li = &priv->loginfo;
143
144 if (priv->prefix != nft_log_null_prefix)
145 if (nla_put_string(skb, NFTA_LOG_PREFIX, priv->prefix))
146 goto nla_put_failure;
Pablo Neira Ayuso09d27b82014-06-25 13:37:13 +0200147 switch (li->type) {
148 case NF_LOG_TYPE_LOG:
149 if (nla_put_be32(skb, NFTA_LOG_LEVEL, htonl(li->u.log.level)))
150 goto nla_put_failure;
151
152 if (li->u.log.logflags) {
153 if (nla_put_be32(skb, NFTA_LOG_FLAGS,
154 htonl(li->u.log.logflags)))
155 goto nla_put_failure;
156 }
157 break;
158 case NF_LOG_TYPE_ULOG:
Patrick McHardy96518512013-10-14 11:00:02 +0200159 if (nla_put_be16(skb, NFTA_LOG_GROUP, htons(li->u.ulog.group)))
160 goto nla_put_failure;
Pablo Neira Ayuso09d27b82014-06-25 13:37:13 +0200161
Liping Zhangcc37c1a2016-07-18 20:44:17 +0800162 if (li->u.ulog.flags & NF_LOG_F_COPY_LEN) {
Pablo Neira Ayuso09d27b82014-06-25 13:37:13 +0200163 if (nla_put_be32(skb, NFTA_LOG_SNAPLEN,
164 htonl(li->u.ulog.copy_len)))
165 goto nla_put_failure;
166 }
167 if (li->u.ulog.qthreshold) {
168 if (nla_put_be16(skb, NFTA_LOG_QTHRESHOLD,
169 htons(li->u.ulog.qthreshold)))
170 goto nla_put_failure;
171 }
172 break;
173 }
Patrick McHardy96518512013-10-14 11:00:02 +0200174 return 0;
175
176nla_put_failure:
177 return -1;
178}
179
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200180static struct nft_expr_type nft_log_type;
181static const struct nft_expr_ops nft_log_ops = {
182 .type = &nft_log_type,
Patrick McHardy96518512013-10-14 11:00:02 +0200183 .size = NFT_EXPR_SIZE(sizeof(struct nft_log)),
Patrick McHardy96518512013-10-14 11:00:02 +0200184 .eval = nft_log_eval,
185 .init = nft_log_init,
186 .destroy = nft_log_destroy,
187 .dump = nft_log_dump,
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200188};
189
190static struct nft_expr_type nft_log_type __read_mostly = {
191 .name = "log",
192 .ops = &nft_log_ops,
Patrick McHardy96518512013-10-14 11:00:02 +0200193 .policy = nft_log_policy,
194 .maxattr = NFTA_LOG_MAX,
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200195 .owner = THIS_MODULE,
Patrick McHardy96518512013-10-14 11:00:02 +0200196};
197
198static int __init nft_log_module_init(void)
199{
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200200 return nft_register_expr(&nft_log_type);
Patrick McHardy96518512013-10-14 11:00:02 +0200201}
202
203static void __exit nft_log_module_exit(void)
204{
Patrick McHardyef1f7df2013-10-10 11:41:20 +0200205 nft_unregister_expr(&nft_log_type);
Patrick McHardy96518512013-10-14 11:00:02 +0200206}
207
208module_init(nft_log_module_init);
209module_exit(nft_log_module_exit);
210
211MODULE_LICENSE("GPL");
212MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
213MODULE_ALIAS_NFT_EXPR("log");