2 * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
3 * Copyright (c) 2012-2014 Pablo Neira Ayuso <pablo@netfilter.org>
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.
9 * Development of this code funded by Astaro AG (http://www.astaro.com/)
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>
22 static const char *nft_log_null_prefix
= "";
25 struct nf_loginfo loginfo
;
29 static void nft_log_eval(const struct nft_expr
*expr
,
30 struct nft_regs
*regs
,
31 const struct nft_pktinfo
*pkt
)
33 const struct nft_log
*priv
= nft_expr_priv(expr
);
35 nf_log_packet(nft_net(pkt
), nft_pf(pkt
), nft_hook(pkt
), pkt
->skb
,
36 nft_in(pkt
), nft_out(pkt
), &priv
->loginfo
, "%s",
40 static const struct nla_policy nft_log_policy
[NFTA_LOG_MAX
+ 1] = {
41 [NFTA_LOG_GROUP
] = { .type
= NLA_U16
},
42 [NFTA_LOG_PREFIX
] = { .type
= NLA_STRING
},
43 [NFTA_LOG_SNAPLEN
] = { .type
= NLA_U32
},
44 [NFTA_LOG_QTHRESHOLD
] = { .type
= NLA_U16
},
45 [NFTA_LOG_LEVEL
] = { .type
= NLA_U32
},
46 [NFTA_LOG_FLAGS
] = { .type
= NLA_U32
},
49 static int nft_log_init(const struct nft_ctx
*ctx
,
50 const struct nft_expr
*expr
,
51 const struct nlattr
* const tb
[])
53 struct nft_log
*priv
= nft_expr_priv(expr
);
54 struct nf_loginfo
*li
= &priv
->loginfo
;
55 const struct nlattr
*nla
;
58 li
->type
= NF_LOG_TYPE_LOG
;
59 if (tb
[NFTA_LOG_LEVEL
] != NULL
&&
60 tb
[NFTA_LOG_GROUP
] != NULL
)
62 if (tb
[NFTA_LOG_GROUP
] != NULL
) {
63 li
->type
= NF_LOG_TYPE_ULOG
;
64 if (tb
[NFTA_LOG_FLAGS
] != NULL
)
68 nla
= tb
[NFTA_LOG_PREFIX
];
70 priv
->prefix
= kmalloc(nla_len(nla
) + 1, GFP_KERNEL
);
71 if (priv
->prefix
== NULL
)
73 nla_strlcpy(priv
->prefix
, nla
, nla_len(nla
) + 1);
75 priv
->prefix
= (char *)nft_log_null_prefix
;
80 if (tb
[NFTA_LOG_LEVEL
] != NULL
) {
82 ntohl(nla_get_be32(tb
[NFTA_LOG_LEVEL
]));
84 li
->u
.log
.level
= LOGLEVEL_WARNING
;
86 if (li
->u
.log
.level
> LOGLEVEL_DEBUG
) {
91 if (tb
[NFTA_LOG_FLAGS
] != NULL
) {
93 ntohl(nla_get_be32(tb
[NFTA_LOG_FLAGS
]));
94 if (li
->u
.log
.logflags
& ~NF_LOG_MASK
) {
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
) {
103 li
->u
.ulog
.flags
|= NF_LOG_F_COPY_LEN
;
104 li
->u
.ulog
.copy_len
=
105 ntohl(nla_get_be32(tb
[NFTA_LOG_SNAPLEN
]));
107 if (tb
[NFTA_LOG_QTHRESHOLD
] != NULL
) {
108 li
->u
.ulog
.qthreshold
=
109 ntohs(nla_get_be16(tb
[NFTA_LOG_QTHRESHOLD
]));
114 err
= nf_logger_find_get(ctx
->afi
->family
, li
->type
);
121 if (priv
->prefix
!= nft_log_null_prefix
)
126 static void nft_log_destroy(const struct nft_ctx
*ctx
,
127 const struct nft_expr
*expr
)
129 struct nft_log
*priv
= nft_expr_priv(expr
);
130 struct nf_loginfo
*li
= &priv
->loginfo
;
132 if (priv
->prefix
!= nft_log_null_prefix
)
135 nf_logger_put(ctx
->afi
->family
, li
->type
);
138 static int nft_log_dump(struct sk_buff
*skb
, const struct nft_expr
*expr
)
140 const struct nft_log
*priv
= nft_expr_priv(expr
);
141 const struct nf_loginfo
*li
= &priv
->loginfo
;
143 if (priv
->prefix
!= nft_log_null_prefix
)
144 if (nla_put_string(skb
, NFTA_LOG_PREFIX
, priv
->prefix
))
145 goto nla_put_failure
;
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
;
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
;
157 case NF_LOG_TYPE_ULOG
:
158 if (nla_put_be16(skb
, NFTA_LOG_GROUP
, htons(li
->u
.ulog
.group
)))
159 goto nla_put_failure
;
161 if (li
->u
.ulog
.flags
& NF_LOG_F_COPY_LEN
) {
162 if (nla_put_be32(skb
, NFTA_LOG_SNAPLEN
,
163 htonl(li
->u
.ulog
.copy_len
)))
164 goto nla_put_failure
;
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
;
179 static struct nft_expr_type nft_log_type
;
180 static const struct nft_expr_ops nft_log_ops
= {
181 .type
= &nft_log_type
,
182 .size
= NFT_EXPR_SIZE(sizeof(struct nft_log
)),
183 .eval
= nft_log_eval
,
184 .init
= nft_log_init
,
185 .destroy
= nft_log_destroy
,
186 .dump
= nft_log_dump
,
189 static struct nft_expr_type nft_log_type __read_mostly
= {
192 .policy
= nft_log_policy
,
193 .maxattr
= NFTA_LOG_MAX
,
194 .owner
= THIS_MODULE
,
197 static int __init
nft_log_module_init(void)
199 return nft_register_expr(&nft_log_type
);
202 static void __exit
nft_log_module_exit(void)
204 nft_unregister_expr(&nft_log_type
);
207 module_init(nft_log_module_init
);
208 module_exit(nft_log_module_exit
);
210 MODULE_LICENSE("GPL");
211 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
212 MODULE_ALIAS_NFT_EXPR("log");