]>
Commit | Line | Data |
---|---|---|
96518518 | 1 | /* |
ef1f7df9 | 2 | * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net> |
09d27b88 | 3 | * Copyright (c) 2012-2014 Pablo Neira Ayuso <pablo@netfilter.org> |
96518518 PM |
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 | ||
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 | ||
22 | static const char *nft_log_null_prefix = ""; | |
23 | ||
24 | struct nft_log { | |
25 | struct nf_loginfo loginfo; | |
26 | char *prefix; | |
96518518 PM |
27 | }; |
28 | ||
29 | static void nft_log_eval(const struct nft_expr *expr, | |
a55e22e9 | 30 | struct nft_regs *regs, |
96518518 PM |
31 | const struct nft_pktinfo *pkt) |
32 | { | |
33 | const struct nft_log *priv = nft_expr_priv(expr); | |
96518518 | 34 | |
88182a0e | 35 | nf_log_packet(pkt->net, pkt->pf, pkt->hook, pkt->skb, pkt->in, |
96518518 PM |
36 | pkt->out, &priv->loginfo, "%s", priv->prefix); |
37 | } | |
38 | ||
39 | static 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 }, | |
09d27b88 PNA |
44 | [NFTA_LOG_LEVEL] = { .type = NLA_U32 }, |
45 | [NFTA_LOG_FLAGS] = { .type = NLA_U32 }, | |
96518518 PM |
46 | }; |
47 | ||
48 | static 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; | |
c2d9a429 LZ |
55 | 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; | |
ff107d27 | 61 | if (tb[NFTA_LOG_GROUP] != NULL) { |
c2d9a429 | 62 | li->type = NF_LOG_TYPE_ULOG; |
ff107d27 LZ |
63 | if (tb[NFTA_LOG_FLAGS] != NULL) |
64 | return -EINVAL; | |
65 | } | |
96518518 | 66 | |
96518518 PM |
67 | nla = tb[NFTA_LOG_PREFIX]; |
68 | if (nla != NULL) { | |
69 | priv->prefix = kmalloc(nla_len(nla) + 1, GFP_KERNEL); | |
70 | if (priv->prefix == NULL) | |
71 | return -ENOMEM; | |
72 | nla_strlcpy(priv->prefix, nla, nla_len(nla) + 1); | |
09d27b88 | 73 | } else { |
96518518 | 74 | priv->prefix = (char *)nft_log_null_prefix; |
09d27b88 | 75 | } |
96518518 | 76 | |
09d27b88 PNA |
77 | switch (li->type) { |
78 | case NF_LOG_TYPE_LOG: | |
79 | if (tb[NFTA_LOG_LEVEL] != NULL) { | |
80 | li->u.log.level = | |
5cbfda20 | 81 | ntohl(nla_get_be32(tb[NFTA_LOG_LEVEL])); |
09d27b88 | 82 | } else { |
a81b2ce8 | 83 | li->u.log.level = LOGLEVEL_WARNING; |
09d27b88 | 84 | } |
1bc4e013 LZ |
85 | if (li->u.log.level > LOGLEVEL_DEBUG) { |
86 | err = -EINVAL; | |
87 | goto err1; | |
88 | } | |
89 | ||
09d27b88 PNA |
90 | if (tb[NFTA_LOG_FLAGS] != NULL) { |
91 | li->u.log.logflags = | |
92 | ntohl(nla_get_be32(tb[NFTA_LOG_FLAGS])); | |
ff107d27 LZ |
93 | if (li->u.log.logflags & ~NF_LOG_MASK) { |
94 | err = -EINVAL; | |
95 | goto err1; | |
96 | } | |
09d27b88 PNA |
97 | } |
98 | break; | |
99 | case NF_LOG_TYPE_ULOG: | |
96518518 | 100 | li->u.ulog.group = ntohs(nla_get_be16(tb[NFTA_LOG_GROUP])); |
09d27b88 | 101 | if (tb[NFTA_LOG_SNAPLEN] != NULL) { |
cc37c1ad | 102 | li->u.ulog.flags |= NF_LOG_F_COPY_LEN; |
09d27b88 PNA |
103 | li->u.ulog.copy_len = |
104 | ntohl(nla_get_be32(tb[NFTA_LOG_SNAPLEN])); | |
105 | } | |
106 | if (tb[NFTA_LOG_QTHRESHOLD] != NULL) { | |
107 | li->u.ulog.qthreshold = | |
108 | ntohs(nla_get_be16(tb[NFTA_LOG_QTHRESHOLD])); | |
109 | } | |
110 | break; | |
96518518 PM |
111 | } |
112 | ||
c2d9a429 LZ |
113 | err = nf_logger_find_get(ctx->afi->family, li->type); |
114 | if (err < 0) | |
115 | goto err1; | |
116 | ||
117 | return 0; | |
118 | ||
119 | err1: | |
120 | if (priv->prefix != nft_log_null_prefix) | |
121 | kfree(priv->prefix); | |
122 | return err; | |
96518518 PM |
123 | } |
124 | ||
62472bce PM |
125 | static void nft_log_destroy(const struct nft_ctx *ctx, |
126 | const struct nft_expr *expr) | |
96518518 PM |
127 | { |
128 | struct nft_log *priv = nft_expr_priv(expr); | |
85d30e24 | 129 | struct nf_loginfo *li = &priv->loginfo; |
96518518 PM |
130 | |
131 | if (priv->prefix != nft_log_null_prefix) | |
132 | kfree(priv->prefix); | |
85d30e24 | 133 | |
f3bb5333 | 134 | nf_logger_put(ctx->afi->family, li->type); |
96518518 PM |
135 | } |
136 | ||
137 | static int nft_log_dump(struct sk_buff *skb, const struct nft_expr *expr) | |
138 | { | |
139 | const struct nft_log *priv = nft_expr_priv(expr); | |
140 | const struct nf_loginfo *li = &priv->loginfo; | |
141 | ||
142 | if (priv->prefix != nft_log_null_prefix) | |
143 | if (nla_put_string(skb, NFTA_LOG_PREFIX, priv->prefix)) | |
144 | goto nla_put_failure; | |
09d27b88 PNA |
145 | switch (li->type) { |
146 | case NF_LOG_TYPE_LOG: | |
147 | if (nla_put_be32(skb, NFTA_LOG_LEVEL, htonl(li->u.log.level))) | |
96518518 | 148 | goto nla_put_failure; |
09d27b88 PNA |
149 | |
150 | if (li->u.log.logflags) { | |
151 | if (nla_put_be32(skb, NFTA_LOG_FLAGS, | |
152 | htonl(li->u.log.logflags))) | |
153 | goto nla_put_failure; | |
154 | } | |
155 | break; | |
156 | case NF_LOG_TYPE_ULOG: | |
157 | if (nla_put_be16(skb, NFTA_LOG_GROUP, htons(li->u.ulog.group))) | |
96518518 | 158 | goto nla_put_failure; |
09d27b88 | 159 | |
cc37c1ad | 160 | if (li->u.ulog.flags & NF_LOG_F_COPY_LEN) { |
09d27b88 PNA |
161 | if (nla_put_be32(skb, NFTA_LOG_SNAPLEN, |
162 | htonl(li->u.ulog.copy_len))) | |
163 | goto nla_put_failure; | |
164 | } | |
165 | if (li->u.ulog.qthreshold) { | |
166 | if (nla_put_be16(skb, NFTA_LOG_QTHRESHOLD, | |
167 | htons(li->u.ulog.qthreshold))) | |
168 | goto nla_put_failure; | |
169 | } | |
170 | break; | |
171 | } | |
96518518 PM |
172 | return 0; |
173 | ||
174 | nla_put_failure: | |
175 | return -1; | |
176 | } | |
177 | ||
ef1f7df9 PM |
178 | static struct nft_expr_type nft_log_type; |
179 | static const struct nft_expr_ops nft_log_ops = { | |
180 | .type = &nft_log_type, | |
96518518 | 181 | .size = NFT_EXPR_SIZE(sizeof(struct nft_log)), |
96518518 PM |
182 | .eval = nft_log_eval, |
183 | .init = nft_log_init, | |
184 | .destroy = nft_log_destroy, | |
185 | .dump = nft_log_dump, | |
ef1f7df9 PM |
186 | }; |
187 | ||
188 | static struct nft_expr_type nft_log_type __read_mostly = { | |
189 | .name = "log", | |
190 | .ops = &nft_log_ops, | |
96518518 PM |
191 | .policy = nft_log_policy, |
192 | .maxattr = NFTA_LOG_MAX, | |
ef1f7df9 | 193 | .owner = THIS_MODULE, |
96518518 PM |
194 | }; |
195 | ||
196 | static int __init nft_log_module_init(void) | |
197 | { | |
ef1f7df9 | 198 | return nft_register_expr(&nft_log_type); |
96518518 PM |
199 | } |
200 | ||
201 | static void __exit nft_log_module_exit(void) | |
202 | { | |
ef1f7df9 | 203 | nft_unregister_expr(&nft_log_type); |
96518518 PM |
204 | } |
205 | ||
206 | module_init(nft_log_module_init); | |
207 | module_exit(nft_log_module_exit); | |
208 | ||
209 | MODULE_LICENSE("GPL"); | |
210 | MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); | |
211 | MODULE_ALIAS_NFT_EXPR("log"); |