Linux Audio

Check our new training course

Loading...
v5.14.15
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Creates audit record for dropped/accepted packets
  4 *
  5 * (C) 2010-2011 Thomas Graf <tgraf@redhat.com>
  6 * (C) 2010-2011 Red Hat, Inc.
 
 
 
 
  7*/
  8
  9#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 10
 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>
 20#include <net/ipv6.h>
 21#include <net/ip.h>
 22
 23MODULE_LICENSE("GPL");
 24MODULE_AUTHOR("Thomas Graf <tgraf@redhat.com>");
 25MODULE_DESCRIPTION("Xtables: creates audit records for dropped/accepted packets");
 26MODULE_ALIAS("ipt_AUDIT");
 27MODULE_ALIAS("ip6t_AUDIT");
 28MODULE_ALIAS("ebt_AUDIT");
 29MODULE_ALIAS("arpt_AUDIT");
 30
 31static bool audit_ip4(struct audit_buffer *ab, struct sk_buff *skb)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 32{
 33	struct iphdr _iph;
 34	const struct iphdr *ih;
 35
 36	ih = skb_header_pointer(skb, skb_network_offset(skb), sizeof(_iph), &_iph);
 37	if (!ih)
 38		return false;
 
 
 39
 40	audit_log_format(ab, " saddr=%pI4 daddr=%pI4 proto=%hhu",
 41			 &ih->saddr, &ih->daddr, ih->protocol);
 42
 43	return true;
 
 
 
 
 
 44}
 45
 46static bool audit_ip6(struct audit_buffer *ab, struct sk_buff *skb)
 47{
 48	struct ipv6hdr _ip6h;
 49	const struct ipv6hdr *ih;
 50	u8 nexthdr;
 51	__be16 frag_off;
 52
 53	ih = skb_header_pointer(skb, skb_network_offset(skb), sizeof(_ip6h), &_ip6h);
 54	if (!ih)
 55		return false;
 
 
 56
 57	nexthdr = ih->nexthdr;
 58	ipv6_skip_exthdr(skb, skb_network_offset(skb) + sizeof(_ip6h), &nexthdr, &frag_off);
 
 59
 60	audit_log_format(ab, " saddr=%pI6c daddr=%pI6c proto=%hhu",
 61			 &ih->saddr, &ih->daddr, nexthdr);
 62
 63	return true;
 
 64}
 65
 66static unsigned int
 67audit_tg(struct sk_buff *skb, const struct xt_action_param *par)
 68{
 
 69	struct audit_buffer *ab;
 70	int fam = -1;
 71
 72	if (audit_enabled == AUDIT_OFF)
 73		goto errout;
 74	ab = audit_log_start(NULL, GFP_ATOMIC, AUDIT_NETFILTER_PKT);
 75	if (ab == NULL)
 76		goto errout;
 77
 78	audit_log_format(ab, "mark=%#x", skb->mark);
 79
 80	switch (xt_family(par)) {
 81	case NFPROTO_BRIDGE:
 82		switch (eth_hdr(skb)->h_proto) {
 83		case htons(ETH_P_IP):
 84			fam = audit_ip4(ab, skb) ? NFPROTO_IPV4 : -1;
 85			break;
 86		case htons(ETH_P_IPV6):
 87			fam = audit_ip6(ab, skb) ? NFPROTO_IPV6 : -1;
 88			break;
 
 
 
 
 
 
 
 
 
 
 
 
 89		}
 90		break;
 
 
 91	case NFPROTO_IPV4:
 92		fam = audit_ip4(ab, skb) ? NFPROTO_IPV4 : -1;
 93		break;
 
 94	case NFPROTO_IPV6:
 95		fam = audit_ip6(ab, skb) ? NFPROTO_IPV6 : -1;
 96		break;
 97	}
 98
 99	if (fam == -1)
100		audit_log_format(ab, " saddr=? daddr=? proto=-1");
 
 
101
102	audit_log_end(ab);
103
104errout:
105	return XT_CONTINUE;
106}
107
108static unsigned int
109audit_tg_ebt(struct sk_buff *skb, const struct xt_action_param *par)
110{
111	audit_tg(skb, par);
112	return EBT_CONTINUE;
113}
114
115static int audit_tg_check(const struct xt_tgchk_param *par)
116{
117	const struct xt_audit_info *info = par->targinfo;
118
119	if (info->type > XT_AUDIT_TYPE_MAX) {
120		pr_info_ratelimited("Audit type out of range (valid range: 0..%u)\n",
121				    XT_AUDIT_TYPE_MAX);
122		return -ERANGE;
123	}
124
125	return 0;
126}
127
128static struct xt_target audit_tg_reg[] __read_mostly = {
129	{
130		.name		= "AUDIT",
131		.family		= NFPROTO_UNSPEC,
132		.target		= audit_tg,
133		.targetsize	= sizeof(struct xt_audit_info),
134		.checkentry	= audit_tg_check,
135		.me		= THIS_MODULE,
136	},
137	{
138		.name		= "AUDIT",
139		.family		= NFPROTO_BRIDGE,
140		.target		= audit_tg_ebt,
141		.targetsize	= sizeof(struct xt_audit_info),
142		.checkentry	= audit_tg_check,
143		.me		= THIS_MODULE,
144	},
145};
146
147static int __init audit_tg_init(void)
148{
149	return xt_register_targets(audit_tg_reg, ARRAY_SIZE(audit_tg_reg));
150}
151
152static void __exit audit_tg_exit(void)
153{
154	xt_unregister_targets(audit_tg_reg, ARRAY_SIZE(audit_tg_reg));
155}
156
157module_init(audit_tg_init);
158module_exit(audit_tg_exit);
v3.1
 
  1/*
  2 * Creates audit record for dropped/accepted packets
  3 *
  4 * (C) 2010-2011 Thomas Graf <tgraf@redhat.com>
  5 * (C) 2010-2011 Red Hat, Inc.
  6 *
  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.
 10*/
 11
 12#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
 13
 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>
 23#include <net/ipv6.h>
 24#include <net/ip.h>
 25
 26MODULE_LICENSE("GPL");
 27MODULE_AUTHOR("Thomas Graf <tgraf@redhat.com>");
 28MODULE_DESCRIPTION("Xtables: creates audit records for dropped/accepted packets");
 29MODULE_ALIAS("ipt_AUDIT");
 30MODULE_ALIAS("ip6t_AUDIT");
 31MODULE_ALIAS("ebt_AUDIT");
 32MODULE_ALIAS("arpt_AUDIT");
 33
 34static void audit_proto(struct audit_buffer *ab, struct sk_buff *skb,
 35			unsigned int proto, unsigned int offset)
 36{
 37	switch (proto) {
 38	case IPPROTO_TCP:
 39	case IPPROTO_UDP:
 40	case IPPROTO_UDPLITE: {
 41		const __be16 *pptr;
 42		__be16 _ports[2];
 43
 44		pptr = skb_header_pointer(skb, offset, sizeof(_ports), _ports);
 45		if (pptr == NULL) {
 46			audit_log_format(ab, " truncated=1");
 47			return;
 48		}
 49
 50		audit_log_format(ab, " sport=%hu dport=%hu",
 51				 ntohs(pptr[0]), ntohs(pptr[1]));
 52		}
 53		break;
 54
 55	case IPPROTO_ICMP:
 56	case IPPROTO_ICMPV6: {
 57		const u8 *iptr;
 58		u8 _ih[2];
 59
 60		iptr = skb_header_pointer(skb, offset, sizeof(_ih), &_ih);
 61		if (iptr == NULL) {
 62			audit_log_format(ab, " truncated=1");
 63			return;
 64		}
 65
 66		audit_log_format(ab, " icmptype=%hhu icmpcode=%hhu",
 67				 iptr[0], iptr[1]);
 68
 69		}
 70		break;
 71	}
 72}
 73
 74static void audit_ip4(struct audit_buffer *ab, struct sk_buff *skb)
 75{
 76	struct iphdr _iph;
 77	const struct iphdr *ih;
 78
 79	ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph);
 80	if (!ih) {
 81		audit_log_format(ab, " truncated=1");
 82		return;
 83	}
 84
 85	audit_log_format(ab, " saddr=%pI4 daddr=%pI4 ipid=%hu proto=%hhu",
 86		&ih->saddr, &ih->daddr, ntohs(ih->id), ih->protocol);
 87
 88	if (ntohs(ih->frag_off) & IP_OFFSET) {
 89		audit_log_format(ab, " frag=1");
 90		return;
 91	}
 92
 93	audit_proto(ab, skb, ih->protocol, ih->ihl * 4);
 94}
 95
 96static void audit_ip6(struct audit_buffer *ab, struct sk_buff *skb)
 97{
 98	struct ipv6hdr _ip6h;
 99	const struct ipv6hdr *ih;
100	u8 nexthdr;
101	int offset;
102
103	ih = skb_header_pointer(skb, skb_network_offset(skb), sizeof(_ip6h), &_ip6h);
104	if (!ih) {
105		audit_log_format(ab, " truncated=1");
106		return;
107	}
108
109	nexthdr = ih->nexthdr;
110	offset = ipv6_skip_exthdr(skb, skb_network_offset(skb) + sizeof(_ip6h),
111				  &nexthdr);
112
113	audit_log_format(ab, " saddr=%pI6c daddr=%pI6c proto=%hhu",
114			 &ih->saddr, &ih->daddr, nexthdr);
115
116	if (offset)
117		audit_proto(ab, skb, nexthdr, offset);
118}
119
120static unsigned int
121audit_tg(struct sk_buff *skb, const struct xt_action_param *par)
122{
123	const struct xt_audit_info *info = par->targinfo;
124	struct audit_buffer *ab;
 
125
 
 
126	ab = audit_log_start(NULL, GFP_ATOMIC, AUDIT_NETFILTER_PKT);
127	if (ab == NULL)
128		goto errout;
129
130	audit_log_format(ab, "action=%hhu hook=%u len=%u inif=%s outif=%s",
131			 info->type, par->hooknum, skb->len,
132			 par->in ? par->in->name : "?",
133			 par->out ? par->out->name : "?");
134
135	if (skb->mark)
136		audit_log_format(ab, " mark=%#x", skb->mark);
137
138	if (skb->dev && skb->dev->type == ARPHRD_ETHER) {
139		audit_log_format(ab, " smac=%pM dmac=%pM macproto=0x%04x",
140				 eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
141				 ntohs(eth_hdr(skb)->h_proto));
142
143		if (par->family == NFPROTO_BRIDGE) {
144			switch (eth_hdr(skb)->h_proto) {
145			case __constant_htons(ETH_P_IP):
146				audit_ip4(ab, skb);
147				break;
148
149			case __constant_htons(ETH_P_IPV6):
150				audit_ip6(ab, skb);
151				break;
152			}
153		}
154	}
155
156	switch (par->family) {
157	case NFPROTO_IPV4:
158		audit_ip4(ab, skb);
159		break;
160
161	case NFPROTO_IPV6:
162		audit_ip6(ab, skb);
163		break;
164	}
165
166#ifdef CONFIG_NETWORK_SECMARK
167	if (skb->secmark)
168		audit_log_secctx(ab, skb->secmark);
169#endif
170
171	audit_log_end(ab);
172
173errout:
174	return XT_CONTINUE;
175}
176
177static unsigned int
178audit_tg_ebt(struct sk_buff *skb, const struct xt_action_param *par)
179{
180	audit_tg(skb, par);
181	return EBT_CONTINUE;
182}
183
184static int audit_tg_check(const struct xt_tgchk_param *par)
185{
186	const struct xt_audit_info *info = par->targinfo;
187
188	if (info->type > XT_AUDIT_TYPE_MAX) {
189		pr_info("Audit type out of range (valid range: 0..%hhu)\n",
190			XT_AUDIT_TYPE_MAX);
191		return -ERANGE;
192	}
193
194	return 0;
195}
196
197static struct xt_target audit_tg_reg[] __read_mostly = {
198	{
199		.name		= "AUDIT",
200		.family		= NFPROTO_UNSPEC,
201		.target		= audit_tg,
202		.targetsize	= sizeof(struct xt_audit_info),
203		.checkentry	= audit_tg_check,
204		.me		= THIS_MODULE,
205	},
206	{
207		.name		= "AUDIT",
208		.family		= NFPROTO_BRIDGE,
209		.target		= audit_tg_ebt,
210		.targetsize	= sizeof(struct xt_audit_info),
211		.checkentry	= audit_tg_check,
212		.me		= THIS_MODULE,
213	},
214};
215
216static int __init audit_tg_init(void)
217{
218	return xt_register_targets(audit_tg_reg, ARRAY_SIZE(audit_tg_reg));
219}
220
221static void __exit audit_tg_exit(void)
222{
223	xt_unregister_targets(audit_tg_reg, ARRAY_SIZE(audit_tg_reg));
224}
225
226module_init(audit_tg_init);
227module_exit(audit_tg_exit);