blob: 6e5a8bb9b940ce3eb03e1dc6840f4e84ef4d1f98 [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 Engelhardt135367b2010-03-19 17:16:42 +010027static int ebt_log_tg_check(const struct xt_tgchk_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -070028{
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +020029 struct ebt_log_info *info = par->targinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 if (info->bitmask & ~EBT_LOG_MASK)
Jan Engelhardtd6b00a52010-03-25 16:34:45 +010032 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 if (info->loglevel >= 8)
Jan Engelhardtd6b00a52010-03-25 16:34:45 +010034 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 info->prefix[EBT_LOG_PREFIX_SIZE - 1] = '\0';
Jan Engelhardtd6b00a52010-03-25 16:34:45 +010036 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037}
38
39struct tcpudphdr
40{
Al Viro47c183fa2006-11-14 21:11:51 -080041 __be16 src;
42 __be16 dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043};
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
Kuo-lang Tseng93f65152008-06-09 15:55:45 -070053static void
54print_ports(const struct sk_buff *skb, uint8_t protocol, int offset)
55{
56 if (protocol == IPPROTO_TCP ||
57 protocol == IPPROTO_UDP ||
58 protocol == IPPROTO_UDPLITE ||
59 protocol == IPPROTO_SCTP ||
60 protocol == IPPROTO_DCCP) {
61 const struct tcpudphdr *pptr;
62 struct tcpudphdr _ports;
63
64 pptr = skb_header_pointer(skb, offset,
65 sizeof(_ports), &_ports);
66 if (pptr == NULL) {
67 printk(" INCOMPLETE TCP/UDP header");
68 return;
69 }
70 printk(" SPT=%u DPT=%u", ntohs(pptr->src), ntohs(pptr->dst));
71 }
72}
73
Bart De Schuymerd5228a42005-12-13 23:14:08 -080074static void
Jan Engelhardt76108ce2008-10-08 11:35:00 +020075ebt_log_packet(u_int8_t pf, unsigned int hooknum,
Bart De Schuymerd5228a42005-12-13 23:14:08 -080076 const struct sk_buff *skb, const struct net_device *in,
77 const struct net_device *out, const struct nf_loginfo *loginfo,
78 const char *prefix)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
Bart De Schuymerd5228a42005-12-13 23:14:08 -080080 unsigned int bitmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 spin_lock_bh(&ebt_log_lock);
Tobias Klauserbe39ee12009-08-10 10:10:55 +020083 printk("<%c>%s IN=%s OUT=%s MAC source = %pM MAC dest = %pM proto = 0x%04x",
84 '0' + loginfo->u.log.level, prefix,
85 in ? in->name : "", out ? out->name : "",
86 eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
87 ntohs(eth_hdr(skb)->h_proto));
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Bart De Schuymerd5228a42005-12-13 23:14:08 -080089 if (loginfo->type == NF_LOG_TYPE_LOG)
90 bitmask = loginfo->u.log.logflags;
91 else
92 bitmask = NF_LOG_MASK;
93
94 if ((bitmask & EBT_LOG_IP) && eth_hdr(skb)->h_proto ==
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 htons(ETH_P_IP)){
Jan Engelhardtabfdf1c2008-01-31 03:59:24 -080096 const struct iphdr *ih;
97 struct iphdr _iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99 ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph);
100 if (ih == NULL) {
101 printk(" INCOMPLETE IP header");
102 goto out;
103 }
Harvey Harrison21454aa2008-10-31 00:54:56 -0700104 printk(" IP SRC=%pI4 IP DST=%pI4, IP tos=0x%02X, IP proto=%d",
105 &ih->saddr, &ih->daddr, ih->tos, ih->protocol);
Kuo-lang Tseng93f65152008-06-09 15:55:45 -0700106 print_ports(skb, ih->protocol, ih->ihl*4);
107 goto out;
108 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Randy Dunlapf5862872008-06-17 16:16:13 -0700110#if defined(CONFIG_BRIDGE_EBT_IP6) || defined(CONFIG_BRIDGE_EBT_IP6_MODULE)
Kuo-lang Tseng93f65152008-06-09 15:55:45 -0700111 if ((bitmask & EBT_LOG_IP6) && eth_hdr(skb)->h_proto ==
112 htons(ETH_P_IPV6)) {
113 const struct ipv6hdr *ih;
114 struct ipv6hdr _iph;
115 uint8_t nexthdr;
116 int offset_ph;
117
118 ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph);
119 if (ih == NULL) {
120 printk(" INCOMPLETE IPv6 header");
121 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 }
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700123 printk(" IPv6 SRC=%pI6 IPv6 DST=%pI6, IPv6 priority=0x%01X, Next Header=%d",
Harvey Harrisonb189db52008-10-28 22:38:52 -0700124 &ih->saddr, &ih->daddr, ih->priority, ih->nexthdr);
Kuo-lang Tseng93f65152008-06-09 15:55:45 -0700125 nexthdr = ih->nexthdr;
126 offset_ph = ipv6_skip_exthdr(skb, sizeof(_iph), &nexthdr);
127 if (offset_ph == -1)
128 goto out;
129 print_ports(skb, nexthdr, offset_ph);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 goto out;
131 }
Randy Dunlapf5862872008-06-17 16:16:13 -0700132#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Bart De Schuymerd5228a42005-12-13 23:14:08 -0800134 if ((bitmask & EBT_LOG_ARP) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 ((eth_hdr(skb)->h_proto == htons(ETH_P_ARP)) ||
136 (eth_hdr(skb)->h_proto == htons(ETH_P_RARP)))) {
Jan Engelhardtabfdf1c2008-01-31 03:59:24 -0800137 const struct arphdr *ah;
138 struct arphdr _arph;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140 ah = skb_header_pointer(skb, 0, sizeof(_arph), &_arph);
141 if (ah == NULL) {
142 printk(" INCOMPLETE ARP header");
143 goto out;
144 }
145 printk(" ARP HTYPE=%d, PTYPE=0x%04x, OPCODE=%d",
146 ntohs(ah->ar_hrd), ntohs(ah->ar_pro),
147 ntohs(ah->ar_op));
148
149 /* If it's for Ethernet and the lengths are OK,
150 * then log the ARP payload */
151 if (ah->ar_hrd == htons(1) &&
152 ah->ar_hln == ETH_ALEN &&
Al Viro47c183fa2006-11-14 21:11:51 -0800153 ah->ar_pln == sizeof(__be32)) {
Jan Engelhardtabfdf1c2008-01-31 03:59:24 -0800154 const struct arppayload *ap;
155 struct arppayload _arpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
David S. Miller85c19372005-06-28 12:39:40 -0700157 ap = skb_header_pointer(skb, sizeof(_arph),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 sizeof(_arpp), &_arpp);
159 if (ap == NULL) {
160 printk(" INCOMPLETE ARP payload");
161 goto out;
162 }
Tobias Klauserbe39ee12009-08-10 10:10:55 +0200163 printk(" ARP MAC SRC=%pM ARP IP SRC=%pI4 ARP MAC DST=%pM ARP IP DST=%pI4",
164 ap->mac_src, ap->ip_src, ap->mac_dst, ap->ip_dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 }
166 }
167out:
168 printk("\n");
169 spin_unlock_bh(&ebt_log_lock);
Bart De Schuymerd5228a42005-12-13 23:14:08 -0800170
171}
172
Jan Engelhardt2d06d4a2008-10-08 11:35:15 +0200173static unsigned int
Jan Engelhardt4b560b42009-07-05 19:43:26 +0200174ebt_log_tg(struct sk_buff *skb, const struct xt_action_param *par)
Bart De Schuymerd5228a42005-12-13 23:14:08 -0800175{
Jan Engelhardt7eb35582008-10-08 11:35:19 +0200176 const struct ebt_log_info *info = par->targinfo;
Bart De Schuymerd5228a42005-12-13 23:14:08 -0800177 struct nf_loginfo li;
178
179 li.type = NF_LOG_TYPE_LOG;
180 li.u.log.level = info->loglevel;
181 li.u.log.logflags = info->bitmask;
182
Patrick McHardybafac2a2006-02-27 13:04:17 -0800183 if (info->bitmask & EBT_LOG_NFLOG)
Jan Engelhardt7eb35582008-10-08 11:35:19 +0200184 nf_log_packet(NFPROTO_BRIDGE, par->hooknum, skb, par->in,
185 par->out, &li, "%s", info->prefix);
Patrick McHardybafac2a2006-02-27 13:04:17 -0800186 else
Jan Engelhardt7eb35582008-10-08 11:35:19 +0200187 ebt_log_packet(NFPROTO_BRIDGE, par->hooknum, skb, par->in,
188 par->out, &li, info->prefix);
Jan Engelhardt0ac6ab12008-10-08 11:35:13 +0200189 return EBT_CONTINUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
191
Jan Engelhardt043ef462008-10-08 11:35:15 +0200192static struct xt_target ebt_log_tg_reg __read_mostly = {
193 .name = "log",
Jan Engelhardt001a18d2008-10-08 11:35:14 +0200194 .revision = 0,
195 .family = NFPROTO_BRIDGE,
Jan Engelhardt2d06d4a2008-10-08 11:35:15 +0200196 .target = ebt_log_tg,
197 .checkentry = ebt_log_tg_check,
Florian Westphalfc0e3df2010-02-15 18:16:26 +0100198 .targetsize = sizeof(struct ebt_log_info),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 .me = THIS_MODULE,
200};
201
Eric Leblond704b3ea2009-03-26 01:03:23 -0700202static struct nf_logger ebt_log_logger __read_mostly = {
Bart De Schuymerd5228a42005-12-13 23:14:08 -0800203 .name = "ebt_log",
204 .logfn = &ebt_log_packet,
205 .me = THIS_MODULE,
206};
207
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800208static int __init ebt_log_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
Bart De Schuymerd5228a42005-12-13 23:14:08 -0800210 int ret;
211
Jan Engelhardt043ef462008-10-08 11:35:15 +0200212 ret = xt_register_target(&ebt_log_tg_reg);
Bart De Schuymerd5228a42005-12-13 23:14:08 -0800213 if (ret < 0)
214 return ret;
Jan Engelhardtee999d82008-10-08 11:35:01 +0200215 nf_log_register(NFPROTO_BRIDGE, &ebt_log_logger);
Patrick McHardy7e2acc72007-07-24 15:29:55 -0700216 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
218
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800219static void __exit ebt_log_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
Patrick McHardye92ad992007-02-12 11:11:55 -0800221 nf_log_unregister(&ebt_log_logger);
Jan Engelhardt043ef462008-10-08 11:35:15 +0200222 xt_unregister_target(&ebt_log_tg_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800225module_init(ebt_log_init);
226module_exit(ebt_log_fini);
Jan Engelhardtf776c4c2008-01-31 04:00:30 -0800227MODULE_DESCRIPTION("Ebtables: Packet logging to syslog");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228MODULE_LICENSE("GPL");