blob: 3654f3ebdb8fec01cbd95e74cb275d0dfde0b216 [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 Engelhardtaf5d6dc2008-10-08 11:35:19 +020027static bool 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 Engelhardt19eda872008-10-08 11:35:13 +020032 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 if (info->loglevel >= 8)
Jan Engelhardt19eda872008-10-08 11:35:13 +020034 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 info->prefix[EBT_LOG_PREFIX_SIZE - 1] = '\0';
Jan Engelhardt19eda872008-10-08 11:35:13 +020036 return true;
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
Jan Engelhardtabfdf1c2008-01-31 03:59:24 -080053static void print_MAC(const unsigned char *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
55 int i;
56
57 for (i = 0; i < ETH_ALEN; i++, p++)
58 printk("%02x%c", *p, i == ETH_ALEN - 1 ? ' ':':');
59}
60
Kuo-lang Tseng93f65152008-06-09 15:55:45 -070061static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#define myNIPQUAD(a) a[0], a[1], a[2], a[3]
Bart De Schuymerd5228a42005-12-13 23:14:08 -080083static void
Jan Engelhardt76108ce2008-10-08 11:35:00 +020084ebt_log_packet(u_int8_t pf, unsigned int hooknum,
Bart De Schuymerd5228a42005-12-13 23:14:08 -080085 const struct sk_buff *skb, const struct net_device *in,
86 const struct net_device *out, const struct nf_loginfo *loginfo,
87 const char *prefix)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
Bart De Schuymerd5228a42005-12-13 23:14:08 -080089 unsigned int bitmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 spin_lock_bh(&ebt_log_lock);
Bart De Schuymerd5228a42005-12-13 23:14:08 -080092 printk("<%c>%s IN=%s OUT=%s MAC source = ", '0' + loginfo->u.log.level,
93 prefix, in ? in->name : "", out ? out->name : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 print_MAC(eth_hdr(skb)->h_source);
96 printk("MAC dest = ");
97 print_MAC(eth_hdr(skb)->h_dest);
98
99 printk("proto = 0x%04x", ntohs(eth_hdr(skb)->h_proto));
100
Bart De Schuymerd5228a42005-12-13 23:14:08 -0800101 if (loginfo->type == NF_LOG_TYPE_LOG)
102 bitmask = loginfo->u.log.logflags;
103 else
104 bitmask = NF_LOG_MASK;
105
106 if ((bitmask & EBT_LOG_IP) && eth_hdr(skb)->h_proto ==
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 htons(ETH_P_IP)){
Jan Engelhardtabfdf1c2008-01-31 03:59:24 -0800108 const struct iphdr *ih;
109 struct iphdr _iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111 ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph);
112 if (ih == NULL) {
113 printk(" INCOMPLETE IP header");
114 goto out;
115 }
Bart De Schuymerd5228a42005-12-13 23:14:08 -0800116 printk(" IP SRC=%u.%u.%u.%u IP DST=%u.%u.%u.%u, IP "
117 "tos=0x%02X, IP proto=%d", NIPQUAD(ih->saddr),
118 NIPQUAD(ih->daddr), ih->tos, ih->protocol);
Kuo-lang Tseng93f65152008-06-09 15:55:45 -0700119 print_ports(skb, ih->protocol, ih->ihl*4);
120 goto out;
121 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Randy Dunlapf5862872008-06-17 16:16:13 -0700123#if defined(CONFIG_BRIDGE_EBT_IP6) || defined(CONFIG_BRIDGE_EBT_IP6_MODULE)
Kuo-lang Tseng93f65152008-06-09 15:55:45 -0700124 if ((bitmask & EBT_LOG_IP6) && eth_hdr(skb)->h_proto ==
125 htons(ETH_P_IPV6)) {
126 const struct ipv6hdr *ih;
127 struct ipv6hdr _iph;
128 uint8_t nexthdr;
129 int offset_ph;
130
131 ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph);
132 if (ih == NULL) {
133 printk(" INCOMPLETE IPv6 header");
134 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 }
Harvey Harrisonb189db52008-10-28 22:38:52 -0700136 printk(" IPv6 SRC=%p6 IPv6 DST=%p6, IPv6 priority=0x%01X, Next Header=%d",
137 &ih->saddr, &ih->daddr, ih->priority, ih->nexthdr);
Kuo-lang Tseng93f65152008-06-09 15:55:45 -0700138 nexthdr = ih->nexthdr;
139 offset_ph = ipv6_skip_exthdr(skb, sizeof(_iph), &nexthdr);
140 if (offset_ph == -1)
141 goto out;
142 print_ports(skb, nexthdr, offset_ph);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 goto out;
144 }
Randy Dunlapf5862872008-06-17 16:16:13 -0700145#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Bart De Schuymerd5228a42005-12-13 23:14:08 -0800147 if ((bitmask & EBT_LOG_ARP) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 ((eth_hdr(skb)->h_proto == htons(ETH_P_ARP)) ||
149 (eth_hdr(skb)->h_proto == htons(ETH_P_RARP)))) {
Jan Engelhardtabfdf1c2008-01-31 03:59:24 -0800150 const struct arphdr *ah;
151 struct arphdr _arph;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
153 ah = skb_header_pointer(skb, 0, sizeof(_arph), &_arph);
154 if (ah == NULL) {
155 printk(" INCOMPLETE ARP header");
156 goto out;
157 }
158 printk(" ARP HTYPE=%d, PTYPE=0x%04x, OPCODE=%d",
159 ntohs(ah->ar_hrd), ntohs(ah->ar_pro),
160 ntohs(ah->ar_op));
161
162 /* If it's for Ethernet and the lengths are OK,
163 * then log the ARP payload */
164 if (ah->ar_hrd == htons(1) &&
165 ah->ar_hln == ETH_ALEN &&
Al Viro47c183fa2006-11-14 21:11:51 -0800166 ah->ar_pln == sizeof(__be32)) {
Jan Engelhardtabfdf1c2008-01-31 03:59:24 -0800167 const struct arppayload *ap;
168 struct arppayload _arpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
David S. Miller85c19372005-06-28 12:39:40 -0700170 ap = skb_header_pointer(skb, sizeof(_arph),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 sizeof(_arpp), &_arpp);
172 if (ap == NULL) {
173 printk(" INCOMPLETE ARP payload");
174 goto out;
175 }
176 printk(" ARP MAC SRC=");
177 print_MAC(ap->mac_src);
178 printk(" ARP IP SRC=%u.%u.%u.%u",
179 myNIPQUAD(ap->ip_src));
180 printk(" ARP MAC DST=");
181 print_MAC(ap->mac_dst);
182 printk(" ARP IP DST=%u.%u.%u.%u",
183 myNIPQUAD(ap->ip_dst));
184 }
185 }
186out:
187 printk("\n");
188 spin_unlock_bh(&ebt_log_lock);
Bart De Schuymerd5228a42005-12-13 23:14:08 -0800189
190}
191
Jan Engelhardt2d06d4a2008-10-08 11:35:15 +0200192static unsigned int
Jan Engelhardt7eb35582008-10-08 11:35:19 +0200193ebt_log_tg(struct sk_buff *skb, const struct xt_target_param *par)
Bart De Schuymerd5228a42005-12-13 23:14:08 -0800194{
Jan Engelhardt7eb35582008-10-08 11:35:19 +0200195 const struct ebt_log_info *info = par->targinfo;
Bart De Schuymerd5228a42005-12-13 23:14:08 -0800196 struct nf_loginfo li;
197
198 li.type = NF_LOG_TYPE_LOG;
199 li.u.log.level = info->loglevel;
200 li.u.log.logflags = info->bitmask;
201
Patrick McHardybafac2a2006-02-27 13:04:17 -0800202 if (info->bitmask & EBT_LOG_NFLOG)
Jan Engelhardt7eb35582008-10-08 11:35:19 +0200203 nf_log_packet(NFPROTO_BRIDGE, par->hooknum, skb, par->in,
204 par->out, &li, "%s", info->prefix);
Patrick McHardybafac2a2006-02-27 13:04:17 -0800205 else
Jan Engelhardt7eb35582008-10-08 11:35:19 +0200206 ebt_log_packet(NFPROTO_BRIDGE, par->hooknum, skb, par->in,
207 par->out, &li, info->prefix);
Jan Engelhardt0ac6ab12008-10-08 11:35:13 +0200208 return EBT_CONTINUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
210
Jan Engelhardt043ef462008-10-08 11:35:15 +0200211static struct xt_target ebt_log_tg_reg __read_mostly = {
212 .name = "log",
Jan Engelhardt001a18d2008-10-08 11:35:14 +0200213 .revision = 0,
214 .family = NFPROTO_BRIDGE,
Jan Engelhardt2d06d4a2008-10-08 11:35:15 +0200215 .target = ebt_log_tg,
216 .checkentry = ebt_log_tg_check,
Jan Engelhardt18219d32008-10-08 11:35:13 +0200217 .targetsize = XT_ALIGN(sizeof(struct ebt_log_info)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 .me = THIS_MODULE,
219};
220
Patrick McHardy7b2f9632007-12-17 22:39:08 -0800221static const struct nf_logger ebt_log_logger = {
Bart De Schuymerd5228a42005-12-13 23:14:08 -0800222 .name = "ebt_log",
223 .logfn = &ebt_log_packet,
224 .me = THIS_MODULE,
225};
226
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800227static int __init ebt_log_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
Bart De Schuymerd5228a42005-12-13 23:14:08 -0800229 int ret;
230
Jan Engelhardt043ef462008-10-08 11:35:15 +0200231 ret = xt_register_target(&ebt_log_tg_reg);
Bart De Schuymerd5228a42005-12-13 23:14:08 -0800232 if (ret < 0)
233 return ret;
Jan Engelhardtee999d82008-10-08 11:35:01 +0200234 nf_log_register(NFPROTO_BRIDGE, &ebt_log_logger);
Patrick McHardy7e2acc72007-07-24 15:29:55 -0700235 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236}
237
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800238static void __exit ebt_log_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
Patrick McHardye92ad992007-02-12 11:11:55 -0800240 nf_log_unregister(&ebt_log_logger);
Jan Engelhardt043ef462008-10-08 11:35:15 +0200241 xt_unregister_target(&ebt_log_tg_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242}
243
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800244module_init(ebt_log_init);
245module_exit(ebt_log_fini);
Jan Engelhardtf776c4c2008-01-31 04:00:30 -0800246MODULE_DESCRIPTION("Ebtables: Packet logging to syslog");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247MODULE_LICENSE("GPL");