2 * Creates audit record for dropped/accepted packets
4 * (C) 2010-2011 Thomas Graf <tgraf@redhat.com>
5 * (C) 2010-2011 Red Hat, Inc.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/audit.h>
15 #include <linux/module.h>
16 #include <linux/skbuff.h>
17 #include <linux/tcp.h>
18 #include <linux/udp.h>
19 #include <linux/if_arp.h>
20 #include <linux/netfilter/x_tables.h>
21 #include <linux/netfilter/xt_AUDIT.h>
22 #include <linux/netfilter_bridge/ebtables.h>
26 MODULE_LICENSE("GPL");
27 MODULE_AUTHOR("Thomas Graf <tgraf@redhat.com>");
28 MODULE_DESCRIPTION("Xtables: creates audit records for dropped/accepted packets");
29 MODULE_ALIAS("ipt_AUDIT");
30 MODULE_ALIAS("ip6t_AUDIT");
31 MODULE_ALIAS("ebt_AUDIT");
32 MODULE_ALIAS("arpt_AUDIT");
34 static bool audit_ip4(struct audit_buffer
*ab
, struct sk_buff
*skb
)
37 const struct iphdr
*ih
;
39 ih
= skb_header_pointer(skb
, skb_network_offset(skb
), sizeof(_iph
), &_iph
);
43 audit_log_format(ab
, " saddr=%pI4 daddr=%pI4 proto=%hhu",
44 &ih
->saddr
, &ih
->daddr
, ih
->protocol
);
49 static bool audit_ip6(struct audit_buffer
*ab
, struct sk_buff
*skb
)
52 const struct ipv6hdr
*ih
;
56 ih
= skb_header_pointer(skb
, skb_network_offset(skb
), sizeof(_ip6h
), &_ip6h
);
60 nexthdr
= ih
->nexthdr
;
61 ipv6_skip_exthdr(skb
, skb_network_offset(skb
) + sizeof(_ip6h
), &nexthdr
, &frag_off
);
63 audit_log_format(ab
, " saddr=%pI6c daddr=%pI6c proto=%hhu",
64 &ih
->saddr
, &ih
->daddr
, nexthdr
);
70 audit_tg(struct sk_buff
*skb
, const struct xt_action_param
*par
)
72 struct audit_buffer
*ab
;
75 if (audit_enabled
== 0)
77 ab
= audit_log_start(NULL
, GFP_ATOMIC
, AUDIT_NETFILTER_PKT
);
81 audit_log_format(ab
, "mark=%#x", skb
->mark
);
83 switch (xt_family(par
)) {
85 switch (eth_hdr(skb
)->h_proto
) {
87 fam
= audit_ip4(ab
, skb
) ? NFPROTO_IPV4
: -1;
89 case htons(ETH_P_IPV6
):
90 fam
= audit_ip6(ab
, skb
) ? NFPROTO_IPV6
: -1;
95 fam
= audit_ip4(ab
, skb
) ? NFPROTO_IPV4
: -1;
98 fam
= audit_ip6(ab
, skb
) ? NFPROTO_IPV6
: -1;
103 audit_log_format(ab
, " saddr=? daddr=? proto=-1");
112 audit_tg_ebt(struct sk_buff
*skb
, const struct xt_action_param
*par
)
118 static int audit_tg_check(const struct xt_tgchk_param
*par
)
120 const struct xt_audit_info
*info
= par
->targinfo
;
122 if (info
->type
> XT_AUDIT_TYPE_MAX
) {
123 pr_info_ratelimited("Audit type out of range (valid range: 0..%hhu)\n",
131 static struct xt_target audit_tg_reg
[] __read_mostly
= {
134 .family
= NFPROTO_UNSPEC
,
136 .targetsize
= sizeof(struct xt_audit_info
),
137 .checkentry
= audit_tg_check
,
142 .family
= NFPROTO_BRIDGE
,
143 .target
= audit_tg_ebt
,
144 .targetsize
= sizeof(struct xt_audit_info
),
145 .checkentry
= audit_tg_check
,
150 static int __init
audit_tg_init(void)
152 return xt_register_targets(audit_tg_reg
, ARRAY_SIZE(audit_tg_reg
));
155 static void __exit
audit_tg_exit(void)
157 xt_unregister_targets(audit_tg_reg
, ARRAY_SIZE(audit_tg_reg
));
160 module_init(audit_tg_init
);
161 module_exit(audit_tg_exit
);