summaryrefslogtreecommitdiffstats
path: root/kernel/linux2.5/net/bridge/netfilter/ebt_log.c
blob: 72dec1ed04e249ecd6a53f35c11598f2851cebe8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
 *  ebt_log
 *
 *	Authors:
 *	Bart De Schuymer <bdschuym@pandora.be>
 *
 *  April, 2002
 *
 */

#include <linux/netfilter_bridge/ebtables.h>
#include <linux/netfilter_bridge/ebt_log.h>
#include <linux/module.h>
#include <linux/ip.h>
#include <linux/if_arp.h>
#include <linux/spinlock.h>

static spinlock_t ebt_log_lock = SPIN_LOCK_UNLOCKED;

struct tcpudphdr
{
	uint16_t src;
	uint16_t dst;
};

struct arppayload
{
	unsigned char mac_src[ETH_ALEN];
	unsigned char ip_src[4];
	unsigned char mac_dst[ETH_ALEN];
	unsigned char ip_dst[4];
};

static void print_MAC(unsigned char *p)
{
	int i;

	for (i = 0; i < ETH_ALEN; i++, p++)
		printk("%02x%c", *p, i == ETH_ALEN - 1 ? ' ':':');
}

#define myNIPQUAD(a) a[0], a[1], a[2], a[3]
static void ebt_log(const struct sk_buff *skb, const struct net_device *in,
   const struct net_device *out, const void *data, unsigned int datalen)
{
	struct ebt_log_info *info = (struct ebt_log_info *)data;
	char level_string[4] = "< >";
	union {struct iphdr iph; struct tcpudphdr ports;
	       struct arphdr arph; struct arppayload arpp;} u;

	level_string[1] = '0' + info->loglevel;
	spin_lock_bh(&ebt_log_lock);
	printk(level_string);
	printk("%s IN=%s OUT=%s ", info->prefix, in ? in->name : "",
	   out ? out->name : "");

	printk("MAC source = ");
	print_MAC((skb->mac.ethernet)->h_source);
	printk("MAC dest = ");
	print_MAC((skb->mac.ethernet)->h_dest);

	printk("proto = 0x%04x", ntohs(((*skb).mac.ethernet)->h_proto));

	if ((info->bitmask & EBT_LOG_IP) && skb->mac.ethernet->h_proto ==
	   htons(ETH_P_IP)){
		if (skb_copy_bits(skb, 0, &u.iph, sizeof(u.iph))) {
			printk(" INCOMPLETE IP header");
			goto out;
		}
		printk(" IP SRC=%u.%u.%u.%u IP DST=%u.%u.%u.%u,",
		   NIPQUAD(u.iph.saddr), NIPQUAD(u.iph.daddr));
		printk(" IP tos=0x%02X, IP proto=%d", u.iph.tos,
		       u.iph.protocol);
		if (u.iph.protocol == IPPROTO_TCP ||
		    u.iph.protocol == IPPROTO_UDP) {
			if (skb_copy_bits(skb, u.iph.ihl*4, &u.ports,
			    sizeof(u.ports))) {
				printk(" INCOMPLETE TCP/UDP header");
				goto out;
			}
			printk(" SPT=%u DPT=%u", ntohs(u.ports.src),
			   ntohs(u.ports.dst));
		}
		goto out;
	}

	if ((info->bitmask & EBT_LOG_ARP) &&
	    ((skb->mac.ethernet->h_proto == __constant_htons(ETH_P_ARP)) ||
	    (skb->mac.ethernet->h_proto == __constant_htons(ETH_P_RARP)))) {
		if (skb_copy_bits(skb, 0, &u.arph, sizeof(u.arph))) {
			printk(" INCOMPLETE ARP header");
			goto out;
		}
		printk(" ARP HTYPE=%d, PTYPE=0x%04x, OPCODE=%d",
		       ntohs(u.arph.ar_hrd), ntohs(u.arph.ar_pro),
		       ntohs(u.arph.ar_op));

		/* If it's for Ethernet and the lengths are OK,
		 * then log the ARP payload */
		if (u.arph.ar_hrd == __constant_htons(1) &&
		    u.arph.ar_hln == ETH_ALEN &&
		    u.arph.ar_pln == sizeof(uint32_t)) {
			if (skb_copy_bits(skb, sizeof(u.arph), &u.arpp,
			    sizeof(u.arpp))) {
				printk(" INCOMPLETE ARP payload");
				goto out;
			}
			printk(" ARP MAC SRC=");
			print_MAC(u.arpp.mac_src);
			printk(" ARP IP SRC=%u.%u.%u.%u",
			       myNIPQUAD(u.arpp.ip_src));
			printk(" ARP MAC DST=");
			print_MAC(u.arpp.mac_dst);
			printk(" ARP IP DST=%u.%u.%u.%u",
			       myNIPQUAD(u.arpp.ip_dst));
		}
	}
out:
	printk("\n");
	spin_unlock_bh(&ebt_log_lock);
}

static struct ebt_watcher log;
static int ebt_log_check(const char *tablename, unsigned int hookmask,
   const struct ebt_entry *e, void *data, unsigned int datalen,
   unsigned int version)
{
	struct ebt_log_info *info = (struct ebt_log_info *)data;

	if (datalen != sizeof(struct ebt_log_info))
		return -EINVAL;
	if (ebt_check_version(version, log.version, log.name))
		return -EINVAL;
	if (info->bitmask & ~EBT_LOG_MASK)
		return -EINVAL;
	if (info->loglevel >= 8)
		return -EINVAL;
	info->prefix[EBT_LOG_PREFIX_SIZE - 1] = '\0';
	return 0;
}

static struct ebt_watcher log =
{
	.name		= EBT_LOG_WATCHER,
	.watcher	= ebt_log,
	.check		= ebt_log_check,
	.me		= THIS_MODULE,
	.version	= VERSIONIZE(1,0),
};

static int __init init(void)
{
	return ebt_register_watcher(&log);
}

static void __exit fini(void)
{
	ebt_unregister_watcher(&log);
}

module_init(init);
module_exit(fini);
MODULE_LICENSE("GPL");