blob: b40f9ed4c343290591512fc41e08cba4c699847b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * ebt_log
3 *
4 * Authors:
5 * Bart De Schuymer <bdschuym@pandora.be>
Bart De Schuymerd5228a42005-12-13 23:14:08 -08006 * Harald Welte <laforge@netfilter.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * April, 2002
9 *
10 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/module.h>
12#include <linux/ip.h>
Harald Welte2e4e6a12006-01-12 13:30:04 -080013#include <linux/in.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/if_arp.h>
15#include <linux/spinlock.h>
Patrick McHardyf01ffbd2007-12-17 22:38:49 -080016#include <net/netfilter/nf_log.h>
Kuo-lang Tseng93f65152008-06-09 15:55:45 -070017#include <linux/ipv6.h>
18#include <net/ipv6.h>
19#include <linux/in6.h>
Jan Engelhardt18219d32008-10-08 11:35:13 +020020#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25static DEFINE_SPINLOCK(ebt_log_lock);
26
Jan Engelhardt2d06d4a2008-10-08 11:35:15 +020027static bool
28ebt_log_tg_check(const char *table, const void *entry,
29 const struct xt_target *target, void *data,
30 unsigned int hook_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -070031{
Jan Engelhardtabfdf1c2008-01-31 03:59:24 -080032 struct ebt_log_info *info = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 if (info->bitmask & ~EBT_LOG_MASK)
Jan Engelhardt19eda872008-10-08 11:35:13 +020035 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 if (info->loglevel >= 8)
Jan Engelhardt19eda872008-10-08 11:35:13 +020037 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 info->prefix[EBT_LOG_PREFIX_SIZE - 1] = '\0';
Jan Engelhardt19eda872008-10-08 11:35:13 +020039 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040}
41
42struct tcpudphdr
43{
Al Viro47c183fa2006-11-14 21:11:51 -080044 __be16 src;
45 __be16 dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046};
47
48struct arppayload
49{
50 unsigned char mac_src[ETH_ALEN];
51 unsigned char ip_src[4];
52 unsigned char mac_dst[ETH_ALEN];
53 unsigned char ip_dst[4];
54};
55
Jan Engelhardtabfdf1c2008-01-31 03:59:24 -080056static void print_MAC(const unsigned char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
58 int i;
59
60 for (i = 0; i < ETH_ALEN; i++, p++)
61 printk("%02x%c", *p, i == ETH_ALEN - 1 ? ' ':':');
62}
63
Kuo-lang Tseng93f65152008-06-09 15:55:45 -070064static void
65print_ports(const struct sk_buff *skb, uint8_t protocol, int offset)
66{
67 if (protocol == IPPROTO_TCP ||
68 protocol == IPPROTO_UDP ||
69 protocol == IPPROTO_UDPLITE ||
70 protocol == IPPROTO_SCTP ||
71 protocol == IPPROTO_DCCP) {
72 const struct tcpudphdr *pptr;
73 struct tcpudphdr _ports;
74
75 pptr = skb_header_pointer(skb, offset,
76 sizeof(_ports), &_ports);
77 if (pptr == NULL) {
78 printk(" INCOMPLETE TCP/UDP header");
79 return;
80 }
81 printk(" SPT=%u DPT=%u", ntohs(pptr->src), ntohs(pptr->dst));
82 }
83}
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#define myNIPQUAD(a) a[0], a[1], a[2], a[3]
Bart De Schuymerd5228a42005-12-13 23:14:08 -080086static void
Jan Engelhardt76108ce2008-10-08 11:35:00 +020087ebt_log_packet(u_int8_t pf, unsigned int hooknum,
Bart De Schuymerd5228a42005-12-13 23:14:08 -080088 const struct sk_buff *skb, const struct net_device *in,
89 const struct net_device *out, const struct nf_loginfo *loginfo,
90 const char *prefix)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Bart De Schuymerd5228a42005-12-13 23:14:08 -080092 unsigned int bitmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 spin_lock_bh(&ebt_log_lock);
Bart De Schuymerd5228a42005-12-13 23:14:08 -080095 printk("<%c>%s IN=%s OUT=%s MAC source = ", '0' + loginfo->u.log.level,
96 prefix, in ? in->name : "", out ? out->name : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 print_MAC(eth_hdr(skb)->h_source);
99 printk("MAC dest = ");
100 print_MAC(eth_hdr(skb)->h_dest);
101
102 printk("proto = 0x%04x", ntohs(eth_hdr(skb)->h_proto));
103
Bart De Schuymerd5228a42005-12-13 23:14:08 -0800104 if (loginfo->type == NF_LOG_TYPE_LOG)
105 bitmask = loginfo->u.log.logflags;
106 else
107 bitmask = NF_LOG_MASK;
108
109 if ((bitmask & EBT_LOG_IP) && eth_hdr(skb)->h_proto ==
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 htons(ETH_P_IP)){
Jan Engelhardtabfdf1c2008-01-31 03:59:24 -0800111 const struct iphdr *ih;
112 struct iphdr _iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114 ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph);
115 if (ih == NULL) {
116 printk(" INCOMPLETE IP header");
117 goto out;
118 }
Bart De Schuymerd5228a42005-12-13 23:14:08 -0800119 printk(" IP SRC=%u.%u.%u.%u IP DST=%u.%u.%u.%u, IP "
120 "tos=0x%02X, IP proto=%d", NIPQUAD(ih->saddr),
121 NIPQUAD(ih->daddr), ih->tos, ih->protocol);
Kuo-lang Tseng93f65152008-06-09 15:55:45 -0700122 print_ports(skb, ih->protocol, ih->ihl*4);
123 goto out;
124 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Randy Dunlapf5862872008-06-17 16:16:13 -0700126#if defined(CONFIG_BRIDGE_EBT_IP6) || defined(CONFIG_BRIDGE_EBT_IP6_MODULE)
Kuo-lang Tseng93f65152008-06-09 15:55:45 -0700127 if ((bitmask & EBT_LOG_IP6) && eth_hdr(skb)->h_proto ==
128 htons(ETH_P_IPV6)) {
129 const struct ipv6hdr *ih;
130 struct ipv6hdr _iph;
131 uint8_t nexthdr;
132 int offset_ph;
133
134 ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph);
135 if (ih == NULL) {
136 printk(" INCOMPLETE IPv6 header");
137 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 }
Kuo-lang Tseng93f65152008-06-09 15:55:45 -0700139 printk(" IPv6 SRC=%x:%x:%x:%x:%x:%x:%x:%x "
140 "IPv6 DST=%x:%x:%x:%x:%x:%x:%x:%x, IPv6 "
141 "priority=0x%01X, Next Header=%d", NIP6(ih->saddr),
142 NIP6(ih->daddr), ih->priority, ih->nexthdr);
143 nexthdr = ih->nexthdr;
144 offset_ph = ipv6_skip_exthdr(skb, sizeof(_iph), &nexthdr);
145 if (offset_ph == -1)
146 goto out;
147 print_ports(skb, nexthdr, offset_ph);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 goto out;
149 }
Randy Dunlapf5862872008-06-17 16:16:13 -0700150#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Bart De Schuymerd5228a42005-12-13 23:14:08 -0800152 if ((bitmask & EBT_LOG_ARP) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 ((eth_hdr(skb)->h_proto == htons(ETH_P_ARP)) ||
154 (eth_hdr(skb)->h_proto == htons(ETH_P_RARP)))) {
Jan Engelhardtabfdf1c2008-01-31 03:59:24 -0800155 const struct arphdr *ah;
156 struct arphdr _arph;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158 ah = skb_header_pointer(skb, 0, sizeof(_arph), &_arph);
159 if (ah == NULL) {
160 printk(" INCOMPLETE ARP header");
161 goto out;
162 }
163 printk(" ARP HTYPE=%d, PTYPE=0x%04x, OPCODE=%d",
164 ntohs(ah->ar_hrd), ntohs(ah->ar_pro),
165 ntohs(ah->ar_op));
166
167 /* If it's for Ethernet and the lengths are OK,
168 * then log the ARP payload */
169 if (ah->ar_hrd == htons(1) &&
170 ah->ar_hln == ETH_ALEN &&
Al Viro47c183fa2006-11-14 21:11:51 -0800171 ah->ar_pln == sizeof(__be32)) {
Jan Engelhardtabfdf1c2008-01-31 03:59:24 -0800172 const struct arppayload *ap;
173 struct arppayload _arpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
David S. Miller85c19372005-06-28 12:39:40 -0700175 ap = skb_header_pointer(skb, sizeof(_arph),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 sizeof(_arpp), &_arpp);
177 if (ap == NULL) {
178 printk(" INCOMPLETE ARP payload");
179 goto out;
180 }
181 printk(" ARP MAC SRC=");
182 print_MAC(ap->mac_src);
183 printk(" ARP IP SRC=%u.%u.%u.%u",
184 myNIPQUAD(ap->ip_src));
185 printk(" ARP MAC DST=");
186 print_MAC(ap->mac_dst);
187 printk(" ARP IP DST=%u.%u.%u.%u",
188 myNIPQUAD(ap->ip_dst));
189 }
190 }
191out:
192 printk("\n");
193 spin_unlock_bh(&ebt_log_lock);
Bart De Schuymerd5228a42005-12-13 23:14:08 -0800194
195}
196
Jan Engelhardt2d06d4a2008-10-08 11:35:15 +0200197static unsigned int
198ebt_log_tg(struct sk_buff *skb, const struct net_device *in,
199 const struct net_device *out, unsigned int hooknr,
200 const struct xt_target *target, const void *data)
Bart De Schuymerd5228a42005-12-13 23:14:08 -0800201{
Jan Engelhardtabfdf1c2008-01-31 03:59:24 -0800202 const struct ebt_log_info *info = data;
Bart De Schuymerd5228a42005-12-13 23:14:08 -0800203 struct nf_loginfo li;
204
205 li.type = NF_LOG_TYPE_LOG;
206 li.u.log.level = info->loglevel;
207 li.u.log.logflags = info->bitmask;
208
Patrick McHardybafac2a2006-02-27 13:04:17 -0800209 if (info->bitmask & EBT_LOG_NFLOG)
Jan Engelhardtee999d82008-10-08 11:35:01 +0200210 nf_log_packet(NFPROTO_BRIDGE, hooknr, skb, in, out, &li,
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900211 "%s", info->prefix);
Patrick McHardybafac2a2006-02-27 13:04:17 -0800212 else
Jan Engelhardtee999d82008-10-08 11:35:01 +0200213 ebt_log_packet(NFPROTO_BRIDGE, hooknr, skb, in, out, &li,
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900214 info->prefix);
Jan Engelhardt0ac6ab12008-10-08 11:35:13 +0200215 return EBT_CONTINUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216}
217
Jan Engelhardt043ef462008-10-08 11:35:15 +0200218static struct xt_target ebt_log_tg_reg __read_mostly = {
219 .name = "log",
Jan Engelhardt001a18d2008-10-08 11:35:14 +0200220 .revision = 0,
221 .family = NFPROTO_BRIDGE,
Jan Engelhardt2d06d4a2008-10-08 11:35:15 +0200222 .target = ebt_log_tg,
223 .checkentry = ebt_log_tg_check,
Jan Engelhardt18219d32008-10-08 11:35:13 +0200224 .targetsize = XT_ALIGN(sizeof(struct ebt_log_info)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 .me = THIS_MODULE,
226};
227
Patrick McHardy7b2f9632007-12-17 22:39:08 -0800228static const struct nf_logger ebt_log_logger = {
Bart De Schuymerd5228a42005-12-13 23:14:08 -0800229 .name = "ebt_log",
230 .logfn = &ebt_log_packet,
231 .me = THIS_MODULE,
232};
233
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800234static int __init ebt_log_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
Bart De Schuymerd5228a42005-12-13 23:14:08 -0800236 int ret;
237
Jan Engelhardt043ef462008-10-08 11:35:15 +0200238 ret = xt_register_target(&ebt_log_tg_reg);
Bart De Schuymerd5228a42005-12-13 23:14:08 -0800239 if (ret < 0)
240 return ret;
Jan Engelhardtee999d82008-10-08 11:35:01 +0200241 nf_log_register(NFPROTO_BRIDGE, &ebt_log_logger);
Patrick McHardy7e2acc72007-07-24 15:29:55 -0700242 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
244
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800245static void __exit ebt_log_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
Patrick McHardye92ad992007-02-12 11:11:55 -0800247 nf_log_unregister(&ebt_log_logger);
Jan Engelhardt043ef462008-10-08 11:35:15 +0200248 xt_unregister_target(&ebt_log_tg_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800251module_init(ebt_log_init);
252module_exit(ebt_log_fini);
Jan Engelhardtf776c4c2008-01-31 04:00:30 -0800253MODULE_DESCRIPTION("Ebtables: Packet logging to syslog");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254MODULE_LICENSE("GPL");