blob: 17f2e4bc2a29fcbb6d40dc408d5472fb67c5721c [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
Hans Schillstrom8cdb46d2013-05-15 01:23:45 +000075ebt_log_packet(struct net *net, u_int8_t pf, unsigned int hooknum,
76 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;
Gao feng7d278922013-03-24 23:50:41 +000081
82 /* FIXME: Disabled from containers until syslog ns is supported */
83 if (!net_eq(net, &init_net))
84 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 spin_lock_bh(&ebt_log_lock);
Joe Perches16af5112012-09-12 02:04:53 +000087 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 +020088 '0' + loginfo->u.log.level, prefix,
89 in ? in->name : "", out ? out->name : "",
90 eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
91 ntohs(eth_hdr(skb)->h_proto));
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Bart De Schuymerd5228a42005-12-13 23:14:08 -080093 if (loginfo->type == NF_LOG_TYPE_LOG)
94 bitmask = loginfo->u.log.logflags;
95 else
96 bitmask = NF_LOG_MASK;
97
98 if ((bitmask & EBT_LOG_IP) && eth_hdr(skb)->h_proto ==
tanxiaojun31a5b832013-12-19 13:28:12 +080099 htons(ETH_P_IP)) {
Jan Engelhardtabfdf1c2008-01-31 03:59:24 -0800100 const struct iphdr *ih;
101 struct iphdr _iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103 ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph);
104 if (ih == NULL) {
105 printk(" INCOMPLETE IP header");
106 goto out;
107 }
Harvey Harrison21454aa2008-10-31 00:54:56 -0700108 printk(" IP SRC=%pI4 IP DST=%pI4, IP tos=0x%02X, IP proto=%d",
109 &ih->saddr, &ih->daddr, ih->tos, ih->protocol);
Kuo-lang Tseng93f65152008-06-09 15:55:45 -0700110 print_ports(skb, ih->protocol, ih->ihl*4);
111 goto out;
112 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Igor Maraviće6373c42011-12-12 02:58:25 +0000114#if IS_ENABLED(CONFIG_BRIDGE_EBT_IP6)
Kuo-lang Tseng93f65152008-06-09 15:55:45 -0700115 if ((bitmask & EBT_LOG_IP6) && eth_hdr(skb)->h_proto ==
116 htons(ETH_P_IPV6)) {
117 const struct ipv6hdr *ih;
118 struct ipv6hdr _iph;
119 uint8_t nexthdr;
Jesse Gross75f28112011-11-30 17:05:51 -0800120 __be16 frag_off;
Kuo-lang Tseng93f65152008-06-09 15:55:45 -0700121 int offset_ph;
122
123 ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph);
124 if (ih == NULL) {
125 printk(" INCOMPLETE IPv6 header");
126 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 }
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700128 printk(" IPv6 SRC=%pI6 IPv6 DST=%pI6, IPv6 priority=0x%01X, Next Header=%d",
Harvey Harrisonb189db52008-10-28 22:38:52 -0700129 &ih->saddr, &ih->daddr, ih->priority, ih->nexthdr);
Kuo-lang Tseng93f65152008-06-09 15:55:45 -0700130 nexthdr = ih->nexthdr;
Jesse Gross75f28112011-11-30 17:05:51 -0800131 offset_ph = ipv6_skip_exthdr(skb, sizeof(_iph), &nexthdr, &frag_off);
Kuo-lang Tseng93f65152008-06-09 15:55:45 -0700132 if (offset_ph == -1)
133 goto out;
134 print_ports(skb, nexthdr, offset_ph);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 goto out;
136 }
Randy Dunlapf5862872008-06-17 16:16:13 -0700137#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Bart De Schuymerd5228a42005-12-13 23:14:08 -0800139 if ((bitmask & EBT_LOG_ARP) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 ((eth_hdr(skb)->h_proto == htons(ETH_P_ARP)) ||
141 (eth_hdr(skb)->h_proto == htons(ETH_P_RARP)))) {
Jan Engelhardtabfdf1c2008-01-31 03:59:24 -0800142 const struct arphdr *ah;
143 struct arphdr _arph;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145 ah = skb_header_pointer(skb, 0, sizeof(_arph), &_arph);
146 if (ah == NULL) {
147 printk(" INCOMPLETE ARP header");
148 goto out;
149 }
150 printk(" ARP HTYPE=%d, PTYPE=0x%04x, OPCODE=%d",
151 ntohs(ah->ar_hrd), ntohs(ah->ar_pro),
152 ntohs(ah->ar_op));
153
154 /* If it's for Ethernet and the lengths are OK,
155 * then log the ARP payload */
156 if (ah->ar_hrd == htons(1) &&
157 ah->ar_hln == ETH_ALEN &&
Al Viro47c183fa2006-11-14 21:11:51 -0800158 ah->ar_pln == sizeof(__be32)) {
Jan Engelhardtabfdf1c2008-01-31 03:59:24 -0800159 const struct arppayload *ap;
160 struct arppayload _arpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
David S. Miller85c19372005-06-28 12:39:40 -0700162 ap = skb_header_pointer(skb, sizeof(_arph),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 sizeof(_arpp), &_arpp);
164 if (ap == NULL) {
165 printk(" INCOMPLETE ARP payload");
166 goto out;
167 }
Tobias Klauserbe39ee12009-08-10 10:10:55 +0200168 printk(" ARP MAC SRC=%pM ARP IP SRC=%pI4 ARP MAC DST=%pM ARP IP DST=%pI4",
169 ap->mac_src, ap->ip_src, ap->mac_dst, ap->ip_dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 }
171 }
172out:
173 printk("\n");
174 spin_unlock_bh(&ebt_log_lock);
Bart De Schuymerd5228a42005-12-13 23:14:08 -0800175
176}
177
Jan Engelhardt2d06d4a2008-10-08 11:35:15 +0200178static unsigned int
Jan Engelhardt4b560b42009-07-05 19:43:26 +0200179ebt_log_tg(struct sk_buff *skb, const struct xt_action_param *par)
Bart De Schuymerd5228a42005-12-13 23:14:08 -0800180{
Jan Engelhardt7eb35582008-10-08 11:35:19 +0200181 const struct ebt_log_info *info = par->targinfo;
Bart De Schuymerd5228a42005-12-13 23:14:08 -0800182 struct nf_loginfo li;
Gao feng30e0c6a2013-03-24 23:50:40 +0000183 struct net *net = dev_net(par->in ? par->in : par->out);
Bart De Schuymerd5228a42005-12-13 23:14:08 -0800184
185 li.type = NF_LOG_TYPE_LOG;
186 li.u.log.level = info->loglevel;
187 li.u.log.logflags = info->bitmask;
188
Pablo Neira Ayuso960649d2014-06-23 00:28:18 +0200189 /* Remember that we have to use ebt_log_packet() not to break backward
190 * compatibility. We cannot use the default bridge packet logger via
191 * nf_log_packet() with NFT_LOG_TYPE_LOG here. --Pablo
192 */
Patrick McHardybafac2a2006-02-27 13:04:17 -0800193 if (info->bitmask & EBT_LOG_NFLOG)
Gao feng30e0c6a2013-03-24 23:50:40 +0000194 nf_log_packet(net, NFPROTO_BRIDGE, par->hooknum, skb,
195 par->in, par->out, &li, "%s", info->prefix);
Patrick McHardybafac2a2006-02-27 13:04:17 -0800196 else
Hans Schillstrom8cdb46d2013-05-15 01:23:45 +0000197 ebt_log_packet(net, NFPROTO_BRIDGE, par->hooknum, skb, par->in,
Gao feng30e0c6a2013-03-24 23:50:40 +0000198 par->out, &li, info->prefix);
Jan Engelhardt0ac6ab12008-10-08 11:35:13 +0200199 return EBT_CONTINUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
Jan Engelhardt043ef462008-10-08 11:35:15 +0200202static struct xt_target ebt_log_tg_reg __read_mostly = {
203 .name = "log",
Jan Engelhardt001a18d2008-10-08 11:35:14 +0200204 .revision = 0,
205 .family = NFPROTO_BRIDGE,
Jan Engelhardt2d06d4a2008-10-08 11:35:15 +0200206 .target = ebt_log_tg,
207 .checkentry = ebt_log_tg_check,
Florian Westphalfc0e3df2010-02-15 18:16:26 +0100208 .targetsize = sizeof(struct ebt_log_info),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 .me = THIS_MODULE,
210};
211
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800212static int __init ebt_log_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
Pablo Neira Ayuso960649d2014-06-23 00:28:18 +0200214 return xt_register_target(&ebt_log_tg_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
216
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800217static void __exit ebt_log_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
Jan Engelhardt043ef462008-10-08 11:35:15 +0200219 xt_unregister_target(&ebt_log_tg_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
221
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800222module_init(ebt_log_init);
223module_exit(ebt_log_fini);
Jan Engelhardtf776c4c2008-01-31 04:00:30 -0800224MODULE_DESCRIPTION("Ebtables: Packet logging to syslog");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225MODULE_LICENSE("GPL");