netfilter: bridge: add generic packet logger
[deliverable/linux.git] / net / netfilter / nft_log.c
CommitLineData
96518518 1/*
ef1f7df9 2 * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
96518518
PM
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * Development of this code funded by Astaro AG (http://www.astaro.com/)
9 */
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
21static const char *nft_log_null_prefix = "";
22
23struct nft_log {
24 struct nf_loginfo loginfo;
25 char *prefix;
96518518
PM
26};
27
28static void nft_log_eval(const struct nft_expr *expr,
29 struct nft_data data[NFT_REG_MAX + 1],
30 const struct nft_pktinfo *pkt)
31{
32 const struct nft_log *priv = nft_expr_priv(expr);
33 struct net *net = dev_net(pkt->in ? pkt->in : pkt->out);
34
b8ecbee6 35 nf_log_packet(net, pkt->ops->pf, pkt->ops->hooknum, pkt->skb, pkt->in,
96518518
PM
36 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 },
44};
45
46static int nft_log_init(const struct nft_ctx *ctx,
47 const struct nft_expr *expr,
48 const struct nlattr * const tb[])
49{
50 struct nft_log *priv = nft_expr_priv(expr);
51 struct nf_loginfo *li = &priv->loginfo;
52 const struct nlattr *nla;
53
96518518
PM
54 nla = tb[NFTA_LOG_PREFIX];
55 if (nla != NULL) {
56 priv->prefix = kmalloc(nla_len(nla) + 1, GFP_KERNEL);
57 if (priv->prefix == NULL)
58 return -ENOMEM;
59 nla_strlcpy(priv->prefix, nla, nla_len(nla) + 1);
60 } else
61 priv->prefix = (char *)nft_log_null_prefix;
62
63 li->type = NF_LOG_TYPE_ULOG;
64 if (tb[NFTA_LOG_GROUP] != NULL)
65 li->u.ulog.group = ntohs(nla_get_be16(tb[NFTA_LOG_GROUP]));
66
67 if (tb[NFTA_LOG_SNAPLEN] != NULL)
68 li->u.ulog.copy_len = ntohl(nla_get_be32(tb[NFTA_LOG_SNAPLEN]));
69 if (tb[NFTA_LOG_QTHRESHOLD] != NULL) {
70 li->u.ulog.qthreshold =
71 ntohs(nla_get_be16(tb[NFTA_LOG_QTHRESHOLD]));
72 }
73
74 return 0;
75}
76
62472bce
PM
77static void nft_log_destroy(const struct nft_ctx *ctx,
78 const struct nft_expr *expr)
96518518
PM
79{
80 struct nft_log *priv = nft_expr_priv(expr);
81
82 if (priv->prefix != nft_log_null_prefix)
83 kfree(priv->prefix);
84}
85
86static int nft_log_dump(struct sk_buff *skb, const struct nft_expr *expr)
87{
88 const struct nft_log *priv = nft_expr_priv(expr);
89 const struct nf_loginfo *li = &priv->loginfo;
90
91 if (priv->prefix != nft_log_null_prefix)
92 if (nla_put_string(skb, NFTA_LOG_PREFIX, priv->prefix))
93 goto nla_put_failure;
94 if (li->u.ulog.group)
95 if (nla_put_be16(skb, NFTA_LOG_GROUP, htons(li->u.ulog.group)))
96 goto nla_put_failure;
97 if (li->u.ulog.copy_len)
98 if (nla_put_be32(skb, NFTA_LOG_SNAPLEN,
99 htonl(li->u.ulog.copy_len)))
100 goto nla_put_failure;
101 if (li->u.ulog.qthreshold)
102 if (nla_put_be16(skb, NFTA_LOG_QTHRESHOLD,
103 htons(li->u.ulog.qthreshold)))
104 goto nla_put_failure;
105 return 0;
106
107nla_put_failure:
108 return -1;
109}
110
ef1f7df9
PM
111static struct nft_expr_type nft_log_type;
112static const struct nft_expr_ops nft_log_ops = {
113 .type = &nft_log_type,
96518518 114 .size = NFT_EXPR_SIZE(sizeof(struct nft_log)),
96518518
PM
115 .eval = nft_log_eval,
116 .init = nft_log_init,
117 .destroy = nft_log_destroy,
118 .dump = nft_log_dump,
ef1f7df9
PM
119};
120
121static struct nft_expr_type nft_log_type __read_mostly = {
122 .name = "log",
123 .ops = &nft_log_ops,
96518518
PM
124 .policy = nft_log_policy,
125 .maxattr = NFTA_LOG_MAX,
ef1f7df9 126 .owner = THIS_MODULE,
96518518
PM
127};
128
129static int __init nft_log_module_init(void)
130{
ef1f7df9 131 return nft_register_expr(&nft_log_type);
96518518
PM
132}
133
134static void __exit nft_log_module_exit(void)
135{
ef1f7df9 136 nft_unregister_expr(&nft_log_type);
96518518
PM
137}
138
139module_init(nft_log_module_init);
140module_exit(nft_log_module_exit);
141
142MODULE_LICENSE("GPL");
143MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
144MODULE_ALIAS_NFT_EXPR("log");
This page took 0.066072 seconds and 5 git commands to generate.