blob: 08e5ea5ec4edffc79794132c8174359f41059e14 [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);
Joe Perches16af5112012-09-12 02:04:53 +000083 printk(KERN_SOH "%c%s IN=%s OUT=%s MAC source = %pM MAC dest = %pM proto = 0x%04x",
Tobias Klauserbe39ee12009-08-10 10:10:55 +020084 '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
Igor Maraviće6373c42011-12-12 02:58:25 +0000110#if IS_ENABLED(CONFIG_BRIDGE_EBT_IP6)
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;
Jesse Gross75f28112011-11-30 17:05:51 -0800116 __be16 frag_off;
Kuo-lang Tseng93f65152008-06-09 15:55:45 -0700117 int offset_ph;
118
119 ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph);
120 if (ih == NULL) {
121 printk(" INCOMPLETE IPv6 header");
122 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 }
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700124 printk(" IPv6 SRC=%pI6 IPv6 DST=%pI6, IPv6 priority=0x%01X, Next Header=%d",
Harvey Harrisonb189db52008-10-28 22:38:52 -0700125 &ih->saddr, &ih->daddr, ih->priority, ih->nexthdr);
Kuo-lang Tseng93f65152008-06-09 15:55:45 -0700126 nexthdr = ih->nexthdr;
Jesse Gross75f28112011-11-30 17:05:51 -0800127 offset_ph = ipv6_skip_exthdr(skb, sizeof(_iph), &nexthdr, &frag_off);
Kuo-lang Tseng93f65152008-06-09 15:55:45 -0700128 if (offset_ph == -1)
129 goto out;
130 print_ports(skb, nexthdr, offset_ph);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 goto out;
132 }
Randy Dunlapf5862872008-06-17 16:16:13 -0700133#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Bart De Schuymerd5228a42005-12-13 23:14:08 -0800135 if ((bitmask & EBT_LOG_ARP) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 ((eth_hdr(skb)->h_proto == htons(ETH_P_ARP)) ||
137 (eth_hdr(skb)->h_proto == htons(ETH_P_RARP)))) {
Jan Engelhardtabfdf1c2008-01-31 03:59:24 -0800138 const struct arphdr *ah;
139 struct arphdr _arph;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 ah = skb_header_pointer(skb, 0, sizeof(_arph), &_arph);
142 if (ah == NULL) {
143 printk(" INCOMPLETE ARP header");
144 goto out;
145 }
146 printk(" ARP HTYPE=%d, PTYPE=0x%04x, OPCODE=%d",
147 ntohs(ah->ar_hrd), ntohs(ah->ar_pro),
148 ntohs(ah->ar_op));
149
150 /* If it's for Ethernet and the lengths are OK,
151 * then log the ARP payload */
152 if (ah->ar_hrd == htons(1) &&
153 ah->ar_hln == ETH_ALEN &&
Al Viro47c183fa2006-11-14 21:11:51 -0800154 ah->ar_pln == sizeof(__be32)) {
Jan Engelhardtabfdf1c2008-01-31 03:59:24 -0800155 const struct arppayload *ap;
156 struct arppayload _arpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
David S. Miller85c19372005-06-28 12:39:40 -0700158 ap = skb_header_pointer(skb, sizeof(_arph),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 sizeof(_arpp), &_arpp);
160 if (ap == NULL) {
161 printk(" INCOMPLETE ARP payload");
162 goto out;
163 }
Tobias Klauserbe39ee12009-08-10 10:10:55 +0200164 printk(" ARP MAC SRC=%pM ARP IP SRC=%pI4 ARP MAC DST=%pM ARP IP DST=%pI4",
165 ap->mac_src, ap->ip_src, ap->mac_dst, ap->ip_dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 }
167 }
168out:
169 printk("\n");
170 spin_unlock_bh(&ebt_log_lock);
Bart De Schuymerd5228a42005-12-13 23:14:08 -0800171
172}
173
Jan Engelhardt2d06d4a2008-10-08 11:35:15 +0200174static unsigned int
Jan Engelhardt4b560b42009-07-05 19:43:26 +0200175ebt_log_tg(struct sk_buff *skb, const struct xt_action_param *par)
Bart De Schuymerd5228a42005-12-13 23:14:08 -0800176{
Jan Engelhardt7eb35582008-10-08 11:35:19 +0200177 const struct ebt_log_info *info = par->targinfo;
Bart De Schuymerd5228a42005-12-13 23:14:08 -0800178 struct nf_loginfo li;
Gao feng30e0c6a2013-03-24 23:50:40 +0000179 struct net *net = dev_net(par->in ? par->in : par->out);
Bart De Schuymerd5228a42005-12-13 23:14:08 -0800180
181 li.type = NF_LOG_TYPE_LOG;
182 li.u.log.level = info->loglevel;
183 li.u.log.logflags = info->bitmask;
184
Patrick McHardybafac2a2006-02-27 13:04:17 -0800185 if (info->bitmask & EBT_LOG_NFLOG)
Gao feng30e0c6a2013-03-24 23:50:40 +0000186 nf_log_packet(net, NFPROTO_BRIDGE, par->hooknum, skb,
187 par->in, par->out, &li, "%s", info->prefix);
Patrick McHardybafac2a2006-02-27 13:04:17 -0800188 else
Jan Engelhardt7eb35582008-10-08 11:35:19 +0200189 ebt_log_packet(NFPROTO_BRIDGE, par->hooknum, skb, par->in,
Gao feng30e0c6a2013-03-24 23:50:40 +0000190 par->out, &li, info->prefix);
Jan Engelhardt0ac6ab12008-10-08 11:35:13 +0200191 return EBT_CONTINUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
193
Jan Engelhardt043ef462008-10-08 11:35:15 +0200194static struct xt_target ebt_log_tg_reg __read_mostly = {
195 .name = "log",
Jan Engelhardt001a18d2008-10-08 11:35:14 +0200196 .revision = 0,
197 .family = NFPROTO_BRIDGE,
Jan Engelhardt2d06d4a2008-10-08 11:35:15 +0200198 .target = ebt_log_tg,
199 .checkentry = ebt_log_tg_check,
Florian Westphalfc0e3df2010-02-15 18:16:26 +0100200 .targetsize = sizeof(struct ebt_log_info),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 .me = THIS_MODULE,
202};
203
Eric Leblond704b3ea2009-03-26 01:03:23 -0700204static struct nf_logger ebt_log_logger __read_mostly = {
Bart De Schuymerd5228a42005-12-13 23:14:08 -0800205 .name = "ebt_log",
206 .logfn = &ebt_log_packet,
207 .me = THIS_MODULE,
208};
209
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800210static int __init ebt_log_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
Bart De Schuymerd5228a42005-12-13 23:14:08 -0800212 int ret;
213
Jan Engelhardt043ef462008-10-08 11:35:15 +0200214 ret = xt_register_target(&ebt_log_tg_reg);
Bart De Schuymerd5228a42005-12-13 23:14:08 -0800215 if (ret < 0)
216 return ret;
Jan Engelhardtee999d82008-10-08 11:35:01 +0200217 nf_log_register(NFPROTO_BRIDGE, &ebt_log_logger);
Patrick McHardy7e2acc72007-07-24 15:29:55 -0700218 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
220
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800221static void __exit ebt_log_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
Patrick McHardye92ad992007-02-12 11:11:55 -0800223 nf_log_unregister(&ebt_log_logger);
Jan Engelhardt043ef462008-10-08 11:35:15 +0200224 xt_unregister_target(&ebt_log_tg_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225}
226
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800227module_init(ebt_log_init);
228module_exit(ebt_log_fini);
Jan Engelhardtf776c4c2008-01-31 04:00:30 -0800229MODULE_DESCRIPTION("Ebtables: Packet logging to syslog");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230MODULE_LICENSE("GPL");