Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/drzeus/mmc
[deliverable/linux.git] / net / bridge / netfilter / ebt_log.c
CommitLineData
1da177e4
LT
1/*
2 * ebt_log
3 *
4 * Authors:
5 * Bart De Schuymer <bdschuym@pandora.be>
d5228a4f 6 * Harald Welte <laforge@netfilter.org>
1da177e4
LT
7 *
8 * April, 2002
9 *
10 */
11
12#include <linux/netfilter_bridge/ebtables.h>
13#include <linux/netfilter_bridge/ebt_log.h>
d5228a4f 14#include <linux/netfilter.h>
1da177e4
LT
15#include <linux/module.h>
16#include <linux/ip.h>
2e4e6a17 17#include <linux/in.h>
1da177e4
LT
18#include <linux/if_arp.h>
19#include <linux/spinlock.h>
20
21static DEFINE_SPINLOCK(ebt_log_lock);
22
23static int ebt_log_check(const char *tablename, unsigned int hookmask,
24 const struct ebt_entry *e, void *data, unsigned int datalen)
25{
26 struct ebt_log_info *info = (struct ebt_log_info *)data;
27
28 if (datalen != EBT_ALIGN(sizeof(struct ebt_log_info)))
29 return -EINVAL;
30 if (info->bitmask & ~EBT_LOG_MASK)
31 return -EINVAL;
32 if (info->loglevel >= 8)
33 return -EINVAL;
34 info->prefix[EBT_LOG_PREFIX_SIZE - 1] = '\0';
35 return 0;
36}
37
38struct tcpudphdr
39{
47c183fa
AV
40 __be16 src;
41 __be16 dst;
1da177e4
LT
42};
43
44struct arppayload
45{
46 unsigned char mac_src[ETH_ALEN];
47 unsigned char ip_src[4];
48 unsigned char mac_dst[ETH_ALEN];
49 unsigned char ip_dst[4];
50};
51
52static void print_MAC(unsigned char *p)
53{
54 int i;
55
56 for (i = 0; i < ETH_ALEN; i++, p++)
57 printk("%02x%c", *p, i == ETH_ALEN - 1 ? ' ':':');
58}
59
60#define myNIPQUAD(a) a[0], a[1], a[2], a[3]
d5228a4f
BDS
61static void
62ebt_log_packet(unsigned int pf, unsigned int hooknum,
63 const struct sk_buff *skb, const struct net_device *in,
64 const struct net_device *out, const struct nf_loginfo *loginfo,
65 const char *prefix)
1da177e4 66{
d5228a4f 67 unsigned int bitmask;
1da177e4 68
1da177e4 69 spin_lock_bh(&ebt_log_lock);
d5228a4f
BDS
70 printk("<%c>%s IN=%s OUT=%s MAC source = ", '0' + loginfo->u.log.level,
71 prefix, in ? in->name : "", out ? out->name : "");
1da177e4 72
1da177e4
LT
73 print_MAC(eth_hdr(skb)->h_source);
74 printk("MAC dest = ");
75 print_MAC(eth_hdr(skb)->h_dest);
76
77 printk("proto = 0x%04x", ntohs(eth_hdr(skb)->h_proto));
78
d5228a4f
BDS
79 if (loginfo->type == NF_LOG_TYPE_LOG)
80 bitmask = loginfo->u.log.logflags;
81 else
82 bitmask = NF_LOG_MASK;
83
84 if ((bitmask & EBT_LOG_IP) && eth_hdr(skb)->h_proto ==
1da177e4
LT
85 htons(ETH_P_IP)){
86 struct iphdr _iph, *ih;
87
88 ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph);
89 if (ih == NULL) {
90 printk(" INCOMPLETE IP header");
91 goto out;
92 }
d5228a4f
BDS
93 printk(" IP SRC=%u.%u.%u.%u IP DST=%u.%u.%u.%u, IP "
94 "tos=0x%02X, IP proto=%d", NIPQUAD(ih->saddr),
95 NIPQUAD(ih->daddr), ih->tos, ih->protocol);
1da177e4 96 if (ih->protocol == IPPROTO_TCP ||
ab67a4d5 97 ih->protocol == IPPROTO_UDP ||
a8d0f952 98 ih->protocol == IPPROTO_UDPLITE ||
ab67a4d5
PM
99 ih->protocol == IPPROTO_SCTP ||
100 ih->protocol == IPPROTO_DCCP) {
1da177e4
LT
101 struct tcpudphdr _ports, *pptr;
102
103 pptr = skb_header_pointer(skb, ih->ihl*4,
104 sizeof(_ports), &_ports);
105 if (pptr == NULL) {
106 printk(" INCOMPLETE TCP/UDP header");
107 goto out;
108 }
109 printk(" SPT=%u DPT=%u", ntohs(pptr->src),
110 ntohs(pptr->dst));
111 }
112 goto out;
113 }
114
d5228a4f 115 if ((bitmask & EBT_LOG_ARP) &&
1da177e4
LT
116 ((eth_hdr(skb)->h_proto == htons(ETH_P_ARP)) ||
117 (eth_hdr(skb)->h_proto == htons(ETH_P_RARP)))) {
118 struct arphdr _arph, *ah;
119
120 ah = skb_header_pointer(skb, 0, sizeof(_arph), &_arph);
121 if (ah == NULL) {
122 printk(" INCOMPLETE ARP header");
123 goto out;
124 }
125 printk(" ARP HTYPE=%d, PTYPE=0x%04x, OPCODE=%d",
126 ntohs(ah->ar_hrd), ntohs(ah->ar_pro),
127 ntohs(ah->ar_op));
128
129 /* If it's for Ethernet and the lengths are OK,
130 * then log the ARP payload */
131 if (ah->ar_hrd == htons(1) &&
132 ah->ar_hln == ETH_ALEN &&
47c183fa 133 ah->ar_pln == sizeof(__be32)) {
1da177e4
LT
134 struct arppayload _arpp, *ap;
135
85c1937b 136 ap = skb_header_pointer(skb, sizeof(_arph),
1da177e4
LT
137 sizeof(_arpp), &_arpp);
138 if (ap == NULL) {
139 printk(" INCOMPLETE ARP payload");
140 goto out;
141 }
142 printk(" ARP MAC SRC=");
143 print_MAC(ap->mac_src);
144 printk(" ARP IP SRC=%u.%u.%u.%u",
145 myNIPQUAD(ap->ip_src));
146 printk(" ARP MAC DST=");
147 print_MAC(ap->mac_dst);
148 printk(" ARP IP DST=%u.%u.%u.%u",
149 myNIPQUAD(ap->ip_dst));
150 }
151 }
152out:
153 printk("\n");
154 spin_unlock_bh(&ebt_log_lock);
d5228a4f
BDS
155
156}
157
158static void ebt_log(const struct sk_buff *skb, unsigned int hooknr,
159 const struct net_device *in, const struct net_device *out,
160 const void *data, unsigned int datalen)
161{
162 struct ebt_log_info *info = (struct ebt_log_info *)data;
163 struct nf_loginfo li;
164
165 li.type = NF_LOG_TYPE_LOG;
166 li.u.log.level = info->loglevel;
167 li.u.log.logflags = info->bitmask;
168
bafac2a5
PM
169 if (info->bitmask & EBT_LOG_NFLOG)
170 nf_log_packet(PF_BRIDGE, hooknr, skb, in, out, &li,
9d6f229f 171 "%s", info->prefix);
bafac2a5
PM
172 else
173 ebt_log_packet(PF_BRIDGE, hooknr, skb, in, out, &li,
9d6f229f 174 info->prefix);
1da177e4
LT
175}
176
177static struct ebt_watcher log =
178{
179 .name = EBT_LOG_WATCHER,
180 .watcher = ebt_log,
181 .check = ebt_log_check,
182 .me = THIS_MODULE,
183};
184
d5228a4f
BDS
185static struct nf_logger ebt_log_logger = {
186 .name = "ebt_log",
187 .logfn = &ebt_log_packet,
188 .me = THIS_MODULE,
189};
190
65b4b4e8 191static int __init ebt_log_init(void)
1da177e4 192{
d5228a4f
BDS
193 int ret;
194
195 ret = ebt_register_watcher(&log);
196 if (ret < 0)
197 return ret;
7e2acc7e
PM
198 nf_log_register(PF_BRIDGE, &ebt_log_logger);
199 return 0;
1da177e4
LT
200}
201
65b4b4e8 202static void __exit ebt_log_fini(void)
1da177e4 203{
e92ad99c 204 nf_log_unregister(&ebt_log_logger);
1da177e4
LT
205 ebt_unregister_watcher(&log);
206}
207
65b4b4e8
AM
208module_init(ebt_log_init);
209module_exit(ebt_log_fini);
1da177e4 210MODULE_LICENSE("GPL");
This page took 0.484925 seconds and 5 git commands to generate.