1 // SPDX-License-Identifier: GPL-2.0-only
3 * Creates audit record for dropped/accepted packets
5 * (C) 2010-2011 Thomas Graf <tgraf@redhat.com>
6 * (C) 2010-2011 Red Hat, Inc.
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #include <linux/audit.h>
12 #include <linux/module.h>
13 #include <linux/skbuff.h>
14 #include <linux/tcp.h>
15 #include <linux/udp.h>
16 #include <linux/if_arp.h>
17 #include <linux/netfilter/x_tables.h>
18 #include <linux/netfilter/xt_AUDIT.h>
19 #include <linux/netfilter_bridge/ebtables.h>
23 MODULE_LICENSE("GPL");
24 MODULE_AUTHOR("Thomas Graf <tgraf@redhat.com>");
25 MODULE_DESCRIPTION("Xtables: creates audit records for dropped/accepted packets");
26 MODULE_ALIAS("ipt_AUDIT");
27 MODULE_ALIAS("ip6t_AUDIT");
28 MODULE_ALIAS("ebt_AUDIT");
29 MODULE_ALIAS("arpt_AUDIT");
31 static bool audit_ip4(struct audit_buffer
*ab
, struct sk_buff
*skb
)
34 const struct iphdr
*ih
;
36 ih
= skb_header_pointer(skb
, skb_network_offset(skb
), sizeof(_iph
), &_iph
);
40 audit_log_format(ab
, " saddr=%pI4 daddr=%pI4 proto=%hhu",
41 &ih
->saddr
, &ih
->daddr
, ih
->protocol
);
46 static bool audit_ip6(struct audit_buffer
*ab
, struct sk_buff
*skb
)
49 const struct ipv6hdr
*ih
;
53 ih
= skb_header_pointer(skb
, skb_network_offset(skb
), sizeof(_ip6h
), &_ip6h
);
57 nexthdr
= ih
->nexthdr
;
58 ipv6_skip_exthdr(skb
, skb_network_offset(skb
) + sizeof(_ip6h
), &nexthdr
, &frag_off
);
60 audit_log_format(ab
, " saddr=%pI6c daddr=%pI6c proto=%hhu",
61 &ih
->saddr
, &ih
->daddr
, nexthdr
);
67 audit_tg(struct sk_buff
*skb
, const struct xt_action_param
*par
)
69 struct audit_buffer
*ab
;
72 if (audit_enabled
== AUDIT_OFF
)
74 ab
= audit_log_start(NULL
, GFP_ATOMIC
, AUDIT_NETFILTER_PKT
);
78 audit_log_format(ab
, "mark=%#x", skb
->mark
);
80 switch (xt_family(par
)) {
82 switch (eth_hdr(skb
)->h_proto
) {
84 fam
= audit_ip4(ab
, skb
) ? NFPROTO_IPV4
: -1;
86 case htons(ETH_P_IPV6
):
87 fam
= audit_ip6(ab
, skb
) ? NFPROTO_IPV6
: -1;
92 fam
= audit_ip4(ab
, skb
) ? NFPROTO_IPV4
: -1;
95 fam
= audit_ip6(ab
, skb
) ? NFPROTO_IPV6
: -1;
100 audit_log_format(ab
, " saddr=? daddr=? proto=-1");
109 audit_tg_ebt(struct sk_buff
*skb
, const struct xt_action_param
*par
)
115 static int audit_tg_check(const struct xt_tgchk_param
*par
)
117 const struct xt_audit_info
*info
= par
->targinfo
;
119 if (info
->type
> XT_AUDIT_TYPE_MAX
) {
120 pr_info_ratelimited("Audit type out of range (valid range: 0..%hhu)\n",
128 static struct xt_target audit_tg_reg
[] __read_mostly
= {
131 .family
= NFPROTO_UNSPEC
,
133 .targetsize
= sizeof(struct xt_audit_info
),
134 .checkentry
= audit_tg_check
,
139 .family
= NFPROTO_BRIDGE
,
140 .target
= audit_tg_ebt
,
141 .targetsize
= sizeof(struct xt_audit_info
),
142 .checkentry
= audit_tg_check
,
147 static int __init
audit_tg_init(void)
149 return xt_register_targets(audit_tg_reg
, ARRAY_SIZE(audit_tg_reg
));
152 static void __exit
audit_tg_exit(void)
154 xt_unregister_targets(audit_tg_reg
, ARRAY_SIZE(audit_tg_reg
));
157 module_init(audit_tg_init
);
158 module_exit(audit_tg_exit
);