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/audit.h>
13 #include <linux/kernel.h>
14 #include <linux/init.h>
15 #include <linux/module.h>
16 #include <linux/netlink.h>
17 #include <linux/netfilter.h>
18 #include <linux/netfilter/nf_tables.h>
21 #include <net/netfilter/nf_tables.h>
22 #include <net/netfilter/nf_log.h>
23 #include <linux/netdevice.h>
25 static const char *nft_log_null_prefix
= "";
28 struct nf_loginfo loginfo
;
32 static bool audit_ip4(struct audit_buffer
*ab
, struct sk_buff
*skb
)
35 const struct iphdr
*ih
;
37 ih
= skb_header_pointer(skb
, skb_network_offset(skb
), sizeof(_iph
), &_iph
);
41 audit_log_format(ab
, " saddr=%pI4 daddr=%pI4 proto=%hhu",
42 &ih
->saddr
, &ih
->daddr
, ih
->protocol
);
47 static bool audit_ip6(struct audit_buffer
*ab
, struct sk_buff
*skb
)
50 const struct ipv6hdr
*ih
;
54 ih
= skb_header_pointer(skb
, skb_network_offset(skb
), sizeof(_ip6h
), &_ip6h
);
58 nexthdr
= ih
->nexthdr
;
59 ipv6_skip_exthdr(skb
, skb_network_offset(skb
) + sizeof(_ip6h
), &nexthdr
, &frag_off
);
61 audit_log_format(ab
, " saddr=%pI6c daddr=%pI6c proto=%hhu",
62 &ih
->saddr
, &ih
->daddr
, nexthdr
);
67 static void nft_log_eval_audit(const struct nft_pktinfo
*pkt
)
69 struct sk_buff
*skb
= pkt
->skb
;
70 struct audit_buffer
*ab
;
76 ab
= audit_log_start(NULL
, GFP_ATOMIC
, AUDIT_NETFILTER_PKT
);
80 audit_log_format(ab
, "mark=%#x", skb
->mark
);
82 switch (nft_pf(pkt
)) {
84 switch (eth_hdr(skb
)->h_proto
) {
86 fam
= audit_ip4(ab
, skb
) ? NFPROTO_IPV4
: -1;
88 case htons(ETH_P_IPV6
):
89 fam
= audit_ip6(ab
, skb
) ? NFPROTO_IPV6
: -1;
94 fam
= audit_ip4(ab
, skb
) ? NFPROTO_IPV4
: -1;
97 fam
= audit_ip6(ab
, skb
) ? NFPROTO_IPV6
: -1;
102 audit_log_format(ab
, " saddr=? daddr=? proto=-1");
107 static void nft_log_eval(const struct nft_expr
*expr
,
108 struct nft_regs
*regs
,
109 const struct nft_pktinfo
*pkt
)
111 const struct nft_log
*priv
= nft_expr_priv(expr
);
113 if (priv
->loginfo
.type
== NF_LOG_TYPE_LOG
&&
114 priv
->loginfo
.u
.log
.level
== NFT_LOGLEVEL_AUDIT
) {
115 nft_log_eval_audit(pkt
);
119 nf_log_packet(nft_net(pkt
), nft_pf(pkt
), nft_hook(pkt
), pkt
->skb
,
120 nft_in(pkt
), nft_out(pkt
), &priv
->loginfo
, "%s",
124 static const struct nla_policy nft_log_policy
[NFTA_LOG_MAX
+ 1] = {
125 [NFTA_LOG_GROUP
] = { .type
= NLA_U16
},
126 [NFTA_LOG_PREFIX
] = { .type
= NLA_STRING
,
127 .len
= NF_LOG_PREFIXLEN
- 1 },
128 [NFTA_LOG_SNAPLEN
] = { .type
= NLA_U32
},
129 [NFTA_LOG_QTHRESHOLD
] = { .type
= NLA_U16
},
130 [NFTA_LOG_LEVEL
] = { .type
= NLA_U32
},
131 [NFTA_LOG_FLAGS
] = { .type
= NLA_U32
},
134 static int nft_log_init(const struct nft_ctx
*ctx
,
135 const struct nft_expr
*expr
,
136 const struct nlattr
* const tb
[])
138 struct nft_log
*priv
= nft_expr_priv(expr
);
139 struct nf_loginfo
*li
= &priv
->loginfo
;
140 const struct nlattr
*nla
;
143 li
->type
= NF_LOG_TYPE_LOG
;
144 if (tb
[NFTA_LOG_LEVEL
] != NULL
&&
145 tb
[NFTA_LOG_GROUP
] != NULL
)
147 if (tb
[NFTA_LOG_GROUP
] != NULL
) {
148 li
->type
= NF_LOG_TYPE_ULOG
;
149 if (tb
[NFTA_LOG_FLAGS
] != NULL
)
153 nla
= tb
[NFTA_LOG_PREFIX
];
155 priv
->prefix
= kmalloc(nla_len(nla
) + 1, GFP_KERNEL
);
156 if (priv
->prefix
== NULL
)
158 nla_strlcpy(priv
->prefix
, nla
, nla_len(nla
) + 1);
160 priv
->prefix
= (char *)nft_log_null_prefix
;
164 case NF_LOG_TYPE_LOG
:
165 if (tb
[NFTA_LOG_LEVEL
] != NULL
) {
167 ntohl(nla_get_be32(tb
[NFTA_LOG_LEVEL
]));
169 li
->u
.log
.level
= NFT_LOGLEVEL_WARNING
;
171 if (li
->u
.log
.level
> NFT_LOGLEVEL_AUDIT
) {
176 if (tb
[NFTA_LOG_FLAGS
] != NULL
) {
178 ntohl(nla_get_be32(tb
[NFTA_LOG_FLAGS
]));
179 if (li
->u
.log
.logflags
& ~NF_LOG_MASK
) {
185 case NF_LOG_TYPE_ULOG
:
186 li
->u
.ulog
.group
= ntohs(nla_get_be16(tb
[NFTA_LOG_GROUP
]));
187 if (tb
[NFTA_LOG_SNAPLEN
] != NULL
) {
188 li
->u
.ulog
.flags
|= NF_LOG_F_COPY_LEN
;
189 li
->u
.ulog
.copy_len
=
190 ntohl(nla_get_be32(tb
[NFTA_LOG_SNAPLEN
]));
192 if (tb
[NFTA_LOG_QTHRESHOLD
] != NULL
) {
193 li
->u
.ulog
.qthreshold
=
194 ntohs(nla_get_be16(tb
[NFTA_LOG_QTHRESHOLD
]));
199 if (li
->u
.log
.level
== NFT_LOGLEVEL_AUDIT
)
202 err
= nf_logger_find_get(ctx
->family
, li
->type
);
209 if (priv
->prefix
!= nft_log_null_prefix
)
214 static void nft_log_destroy(const struct nft_ctx
*ctx
,
215 const struct nft_expr
*expr
)
217 struct nft_log
*priv
= nft_expr_priv(expr
);
218 struct nf_loginfo
*li
= &priv
->loginfo
;
220 if (priv
->prefix
!= nft_log_null_prefix
)
223 if (li
->u
.log
.level
== NFT_LOGLEVEL_AUDIT
)
226 nf_logger_put(ctx
->family
, li
->type
);
229 static int nft_log_dump(struct sk_buff
*skb
, const struct nft_expr
*expr
)
231 const struct nft_log
*priv
= nft_expr_priv(expr
);
232 const struct nf_loginfo
*li
= &priv
->loginfo
;
234 if (priv
->prefix
!= nft_log_null_prefix
)
235 if (nla_put_string(skb
, NFTA_LOG_PREFIX
, priv
->prefix
))
236 goto nla_put_failure
;
238 case NF_LOG_TYPE_LOG
:
239 if (nla_put_be32(skb
, NFTA_LOG_LEVEL
, htonl(li
->u
.log
.level
)))
240 goto nla_put_failure
;
242 if (li
->u
.log
.logflags
) {
243 if (nla_put_be32(skb
, NFTA_LOG_FLAGS
,
244 htonl(li
->u
.log
.logflags
)))
245 goto nla_put_failure
;
248 case NF_LOG_TYPE_ULOG
:
249 if (nla_put_be16(skb
, NFTA_LOG_GROUP
, htons(li
->u
.ulog
.group
)))
250 goto nla_put_failure
;
252 if (li
->u
.ulog
.flags
& NF_LOG_F_COPY_LEN
) {
253 if (nla_put_be32(skb
, NFTA_LOG_SNAPLEN
,
254 htonl(li
->u
.ulog
.copy_len
)))
255 goto nla_put_failure
;
257 if (li
->u
.ulog
.qthreshold
) {
258 if (nla_put_be16(skb
, NFTA_LOG_QTHRESHOLD
,
259 htons(li
->u
.ulog
.qthreshold
)))
260 goto nla_put_failure
;
270 static struct nft_expr_type nft_log_type
;
271 static const struct nft_expr_ops nft_log_ops
= {
272 .type
= &nft_log_type
,
273 .size
= NFT_EXPR_SIZE(sizeof(struct nft_log
)),
274 .eval
= nft_log_eval
,
275 .init
= nft_log_init
,
276 .destroy
= nft_log_destroy
,
277 .dump
= nft_log_dump
,
280 static struct nft_expr_type nft_log_type __read_mostly
= {
283 .policy
= nft_log_policy
,
284 .maxattr
= NFTA_LOG_MAX
,
285 .owner
= THIS_MODULE
,
288 static int __init
nft_log_module_init(void)
290 return nft_register_expr(&nft_log_type
);
293 static void __exit
nft_log_module_exit(void)
295 nft_unregister_expr(&nft_log_type
);
298 module_init(nft_log_module_init
);
299 module_exit(nft_log_module_exit
);
301 MODULE_LICENSE("GPL");
302 MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
303 MODULE_ALIAS_NFT_EXPR("log");