Merge branch 'master' of ssh+git://oss.sgi.com/oss/git/xfs/xfs
[linux-block.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 */
1da177e4
LT
11#include <linux/module.h>
12#include <linux/ip.h>
2e4e6a17 13#include <linux/in.h>
1da177e4
LT
14#include <linux/if_arp.h>
15#include <linux/spinlock.h>
f01ffbd6 16#include <net/netfilter/nf_log.h>
93f65158
KT
17#include <linux/ipv6.h>
18#include <net/ipv6.h>
19#include <linux/in6.h>
18219d3f
JE
20#include <linux/netfilter/x_tables.h>
21#include <linux/netfilter_bridge/ebtables.h>
22#include <linux/netfilter_bridge/ebt_log.h>
23#include <linux/netfilter.h>
1da177e4
LT
24
25static DEFINE_SPINLOCK(ebt_log_lock);
26
af5d6dc2 27static bool ebt_log_tg_check(const struct xt_tgchk_param *par)
1da177e4 28{
af5d6dc2 29 struct ebt_log_info *info = par->targinfo;
1da177e4 30
1da177e4 31 if (info->bitmask & ~EBT_LOG_MASK)
19eda879 32 return false;
1da177e4 33 if (info->loglevel >= 8)
19eda879 34 return false;
1da177e4 35 info->prefix[EBT_LOG_PREFIX_SIZE - 1] = '\0';
19eda879 36 return true;
1da177e4
LT
37}
38
39struct tcpudphdr
40{
47c183fa
AV
41 __be16 src;
42 __be16 dst;
1da177e4
LT
43};
44
45struct arppayload
46{
47 unsigned char mac_src[ETH_ALEN];
48 unsigned char ip_src[4];
49 unsigned char mac_dst[ETH_ALEN];
50 unsigned char ip_dst[4];
51};
52
abfdf1c4 53static void print_MAC(const unsigned char *p)
1da177e4
LT
54{
55 int i;
56
57 for (i = 0; i < ETH_ALEN; i++, p++)
58 printk("%02x%c", *p, i == ETH_ALEN - 1 ? ' ':':');
59}
60
93f65158
KT
61static void
62print_ports(const struct sk_buff *skb, uint8_t protocol, int offset)
63{
64 if (protocol == IPPROTO_TCP ||
65 protocol == IPPROTO_UDP ||
66 protocol == IPPROTO_UDPLITE ||
67 protocol == IPPROTO_SCTP ||
68 protocol == IPPROTO_DCCP) {
69 const struct tcpudphdr *pptr;
70 struct tcpudphdr _ports;
71
72 pptr = skb_header_pointer(skb, offset,
73 sizeof(_ports), &_ports);
74 if (pptr == NULL) {
75 printk(" INCOMPLETE TCP/UDP header");
76 return;
77 }
78 printk(" SPT=%u DPT=%u", ntohs(pptr->src), ntohs(pptr->dst));
79 }
80}
81
d5228a4f 82static void
76108cea 83ebt_log_packet(u_int8_t pf, unsigned int hooknum,
d5228a4f
BDS
84 const struct sk_buff *skb, const struct net_device *in,
85 const struct net_device *out, const struct nf_loginfo *loginfo,
86 const char *prefix)
1da177e4 87{
d5228a4f 88 unsigned int bitmask;
1da177e4 89
1da177e4 90 spin_lock_bh(&ebt_log_lock);
d5228a4f
BDS
91 printk("<%c>%s IN=%s OUT=%s MAC source = ", '0' + loginfo->u.log.level,
92 prefix, in ? in->name : "", out ? out->name : "");
1da177e4 93
1da177e4
LT
94 print_MAC(eth_hdr(skb)->h_source);
95 printk("MAC dest = ");
96 print_MAC(eth_hdr(skb)->h_dest);
97
98 printk("proto = 0x%04x", ntohs(eth_hdr(skb)->h_proto));
99
d5228a4f
BDS
100 if (loginfo->type == NF_LOG_TYPE_LOG)
101 bitmask = loginfo->u.log.logflags;
102 else
103 bitmask = NF_LOG_MASK;
104
105 if ((bitmask & EBT_LOG_IP) && eth_hdr(skb)->h_proto ==
1da177e4 106 htons(ETH_P_IP)){
abfdf1c4
JE
107 const struct iphdr *ih;
108 struct iphdr _iph;
1da177e4
LT
109
110 ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph);
111 if (ih == NULL) {
112 printk(" INCOMPLETE IP header");
113 goto out;
114 }
21454aaa
HH
115 printk(" IP SRC=%pI4 IP DST=%pI4, IP tos=0x%02X, IP proto=%d",
116 &ih->saddr, &ih->daddr, ih->tos, ih->protocol);
93f65158
KT
117 print_ports(skb, ih->protocol, ih->ihl*4);
118 goto out;
119 }
120
f586287e 121#if defined(CONFIG_BRIDGE_EBT_IP6) || defined(CONFIG_BRIDGE_EBT_IP6_MODULE)
93f65158
KT
122 if ((bitmask & EBT_LOG_IP6) && eth_hdr(skb)->h_proto ==
123 htons(ETH_P_IPV6)) {
124 const struct ipv6hdr *ih;
125 struct ipv6hdr _iph;
126 uint8_t nexthdr;
127 int offset_ph;
128
129 ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph);
130 if (ih == NULL) {
131 printk(" INCOMPLETE IPv6 header");
132 goto out;
1da177e4 133 }
5b095d98 134 printk(" IPv6 SRC=%pI6 IPv6 DST=%pI6, IPv6 priority=0x%01X, Next Header=%d",
b189db5d 135 &ih->saddr, &ih->daddr, ih->priority, ih->nexthdr);
93f65158
KT
136 nexthdr = ih->nexthdr;
137 offset_ph = ipv6_skip_exthdr(skb, sizeof(_iph), &nexthdr);
138 if (offset_ph == -1)
139 goto out;
140 print_ports(skb, nexthdr, offset_ph);
1da177e4
LT
141 goto out;
142 }
f586287e 143#endif
1da177e4 144
d5228a4f 145 if ((bitmask & EBT_LOG_ARP) &&
1da177e4
LT
146 ((eth_hdr(skb)->h_proto == htons(ETH_P_ARP)) ||
147 (eth_hdr(skb)->h_proto == htons(ETH_P_RARP)))) {
abfdf1c4
JE
148 const struct arphdr *ah;
149 struct arphdr _arph;
1da177e4
LT
150
151 ah = skb_header_pointer(skb, 0, sizeof(_arph), &_arph);
152 if (ah == NULL) {
153 printk(" INCOMPLETE ARP header");
154 goto out;
155 }
156 printk(" ARP HTYPE=%d, PTYPE=0x%04x, OPCODE=%d",
157 ntohs(ah->ar_hrd), ntohs(ah->ar_pro),
158 ntohs(ah->ar_op));
159
160 /* If it's for Ethernet and the lengths are OK,
161 * then log the ARP payload */
162 if (ah->ar_hrd == htons(1) &&
163 ah->ar_hln == ETH_ALEN &&
47c183fa 164 ah->ar_pln == sizeof(__be32)) {
abfdf1c4
JE
165 const struct arppayload *ap;
166 struct arppayload _arpp;
1da177e4 167
85c1937b 168 ap = skb_header_pointer(skb, sizeof(_arph),
1da177e4
LT
169 sizeof(_arpp), &_arpp);
170 if (ap == NULL) {
171 printk(" INCOMPLETE ARP payload");
172 goto out;
173 }
174 printk(" ARP MAC SRC=");
175 print_MAC(ap->mac_src);
21454aaa 176 printk(" ARP IP SRC=%pI4", ap->ip_src);
1da177e4
LT
177 printk(" ARP MAC DST=");
178 print_MAC(ap->mac_dst);
21454aaa 179 printk(" ARP IP DST=%pI4", ap->ip_dst);
1da177e4
LT
180 }
181 }
182out:
183 printk("\n");
184 spin_unlock_bh(&ebt_log_lock);
d5228a4f
BDS
185
186}
187
2d06d4a5 188static unsigned int
7eb35586 189ebt_log_tg(struct sk_buff *skb, const struct xt_target_param *par)
d5228a4f 190{
7eb35586 191 const struct ebt_log_info *info = par->targinfo;
d5228a4f
BDS
192 struct nf_loginfo li;
193
194 li.type = NF_LOG_TYPE_LOG;
195 li.u.log.level = info->loglevel;
196 li.u.log.logflags = info->bitmask;
197
bafac2a5 198 if (info->bitmask & EBT_LOG_NFLOG)
7eb35586
JE
199 nf_log_packet(NFPROTO_BRIDGE, par->hooknum, skb, par->in,
200 par->out, &li, "%s", info->prefix);
bafac2a5 201 else
7eb35586
JE
202 ebt_log_packet(NFPROTO_BRIDGE, par->hooknum, skb, par->in,
203 par->out, &li, info->prefix);
0ac6ab1f 204 return EBT_CONTINUE;
1da177e4
LT
205}
206
043ef46c
JE
207static struct xt_target ebt_log_tg_reg __read_mostly = {
208 .name = "log",
001a18d3
JE
209 .revision = 0,
210 .family = NFPROTO_BRIDGE,
2d06d4a5
JE
211 .target = ebt_log_tg,
212 .checkentry = ebt_log_tg_check,
18219d3f 213 .targetsize = XT_ALIGN(sizeof(struct ebt_log_info)),
1da177e4
LT
214 .me = THIS_MODULE,
215};
216
704b3ea3 217static struct nf_logger ebt_log_logger __read_mostly = {
d5228a4f
BDS
218 .name = "ebt_log",
219 .logfn = &ebt_log_packet,
220 .me = THIS_MODULE,
221};
222
65b4b4e8 223static int __init ebt_log_init(void)
1da177e4 224{
d5228a4f
BDS
225 int ret;
226
043ef46c 227 ret = xt_register_target(&ebt_log_tg_reg);
d5228a4f
BDS
228 if (ret < 0)
229 return ret;
ee999d8b 230 nf_log_register(NFPROTO_BRIDGE, &ebt_log_logger);
7e2acc7e 231 return 0;
1da177e4
LT
232}
233
65b4b4e8 234static void __exit ebt_log_fini(void)
1da177e4 235{
e92ad99c 236 nf_log_unregister(&ebt_log_logger);
043ef46c 237 xt_unregister_target(&ebt_log_tg_reg);
1da177e4
LT
238}
239
65b4b4e8
AM
240module_init(ebt_log_init);
241module_exit(ebt_log_fini);
f776c4cd 242MODULE_DESCRIPTION("Ebtables: Packet logging to syslog");
1da177e4 243MODULE_LICENSE("GPL");