blob: 152300d164acd1bb9702aa60db5f7a3ccc4752fe [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
Ian Morrisabcdd9a622015-10-26 09:10:42 +000039struct tcpudphdr {
Al Viro47c183fa2006-11-14 21:11:51 -080040 __be16 src;
41 __be16 dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042};
43
Ian Morrisabcdd9a622015-10-26 09:10:42 +000044struct arppayload {
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 unsigned char mac_src[ETH_ALEN];
46 unsigned char ip_src[4];
47 unsigned char mac_dst[ETH_ALEN];
48 unsigned char ip_dst[4];
49};
50
Kuo-lang Tseng93f65152008-06-09 15:55:45 -070051static void
52print_ports(const struct sk_buff *skb, uint8_t protocol, int offset)
53{
54 if (protocol == IPPROTO_TCP ||
55 protocol == IPPROTO_UDP ||
56 protocol == IPPROTO_UDPLITE ||
57 protocol == IPPROTO_SCTP ||
58 protocol == IPPROTO_DCCP) {
59 const struct tcpudphdr *pptr;
60 struct tcpudphdr _ports;
61
62 pptr = skb_header_pointer(skb, offset,
63 sizeof(_ports), &_ports);
64 if (pptr == NULL) {
65 printk(" INCOMPLETE TCP/UDP header");
66 return;
67 }
68 printk(" SPT=%u DPT=%u", ntohs(pptr->src), ntohs(pptr->dst));
69 }
70}
71
Bart De Schuymerd5228a42005-12-13 23:14:08 -080072static void
Hans Schillstrom8cdb46d2013-05-15 01:23:45 +000073ebt_log_packet(struct net *net, u_int8_t pf, unsigned int hooknum,
74 const struct sk_buff *skb, const struct net_device *in,
75 const struct net_device *out, const struct nf_loginfo *loginfo,
76 const char *prefix)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
Bart De Schuymerd5228a42005-12-13 23:14:08 -080078 unsigned int bitmask;
Gao feng7d278922013-03-24 23:50:41 +000079
80 /* FIXME: Disabled from containers until syslog ns is supported */
81 if (!net_eq(net, &init_net))
82 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 spin_lock_bh(&ebt_log_lock);
Joe Perches16af5112012-09-12 02:04:53 +000085 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 +020086 '0' + loginfo->u.log.level, prefix,
87 in ? in->name : "", out ? out->name : "",
88 eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
89 ntohs(eth_hdr(skb)->h_proto));
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Bart De Schuymerd5228a42005-12-13 23:14:08 -080091 if (loginfo->type == NF_LOG_TYPE_LOG)
92 bitmask = loginfo->u.log.logflags;
93 else
94 bitmask = NF_LOG_MASK;
95
96 if ((bitmask & EBT_LOG_IP) && eth_hdr(skb)->h_proto ==
tanxiaojun31a5b832013-12-19 13:28:12 +080097 htons(ETH_P_IP)) {
Jan Engelhardtabfdf1c2008-01-31 03:59:24 -080098 const struct iphdr *ih;
99 struct iphdr _iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101 ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph);
102 if (ih == NULL) {
103 printk(" INCOMPLETE IP header");
104 goto out;
105 }
Harvey Harrison21454aa2008-10-31 00:54:56 -0700106 printk(" IP SRC=%pI4 IP DST=%pI4, IP tos=0x%02X, IP proto=%d",
107 &ih->saddr, &ih->daddr, ih->tos, ih->protocol);
Kuo-lang Tseng93f65152008-06-09 15:55:45 -0700108 print_ports(skb, ih->protocol, ih->ihl*4);
109 goto out;
110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Igor Maraviće6373c42011-12-12 02:58:25 +0000112#if IS_ENABLED(CONFIG_BRIDGE_EBT_IP6)
Kuo-lang Tseng93f65152008-06-09 15:55:45 -0700113 if ((bitmask & EBT_LOG_IP6) && eth_hdr(skb)->h_proto ==
114 htons(ETH_P_IPV6)) {
115 const struct ipv6hdr *ih;
116 struct ipv6hdr _iph;
117 uint8_t nexthdr;
Jesse Gross75f28112011-11-30 17:05:51 -0800118 __be16 frag_off;
Kuo-lang Tseng93f65152008-06-09 15:55:45 -0700119 int offset_ph;
120
121 ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph);
122 if (ih == NULL) {
123 printk(" INCOMPLETE IPv6 header");
124 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 }
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700126 printk(" IPv6 SRC=%pI6 IPv6 DST=%pI6, IPv6 priority=0x%01X, Next Header=%d",
Harvey Harrisonb189db52008-10-28 22:38:52 -0700127 &ih->saddr, &ih->daddr, ih->priority, ih->nexthdr);
Kuo-lang Tseng93f65152008-06-09 15:55:45 -0700128 nexthdr = ih->nexthdr;
Jesse Gross75f28112011-11-30 17:05:51 -0800129 offset_ph = ipv6_skip_exthdr(skb, sizeof(_iph), &nexthdr, &frag_off);
Kuo-lang Tseng93f65152008-06-09 15:55:45 -0700130 if (offset_ph == -1)
131 goto out;
132 print_ports(skb, nexthdr, offset_ph);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 goto out;
134 }
Randy Dunlapf5862872008-06-17 16:16:13 -0700135#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Bart De Schuymerd5228a42005-12-13 23:14:08 -0800137 if ((bitmask & EBT_LOG_ARP) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 ((eth_hdr(skb)->h_proto == htons(ETH_P_ARP)) ||
139 (eth_hdr(skb)->h_proto == htons(ETH_P_RARP)))) {
Jan Engelhardtabfdf1c2008-01-31 03:59:24 -0800140 const struct arphdr *ah;
141 struct arphdr _arph;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143 ah = skb_header_pointer(skb, 0, sizeof(_arph), &_arph);
144 if (ah == NULL) {
145 printk(" INCOMPLETE ARP header");
146 goto out;
147 }
148 printk(" ARP HTYPE=%d, PTYPE=0x%04x, OPCODE=%d",
149 ntohs(ah->ar_hrd), ntohs(ah->ar_pro),
150 ntohs(ah->ar_op));
151
152 /* If it's for Ethernet and the lengths are OK,
Ian Morris7f495ad2015-10-26 09:10:41 +0000153 * then log the ARP payload
154 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 if (ah->ar_hrd == htons(1) &&
156 ah->ar_hln == ETH_ALEN &&
Al Viro47c183fa2006-11-14 21:11:51 -0800157 ah->ar_pln == sizeof(__be32)) {
Jan Engelhardtabfdf1c2008-01-31 03:59:24 -0800158 const struct arppayload *ap;
159 struct arppayload _arpp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
David S. Miller85c19372005-06-28 12:39:40 -0700161 ap = skb_header_pointer(skb, sizeof(_arph),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 sizeof(_arpp), &_arpp);
163 if (ap == NULL) {
164 printk(" INCOMPLETE ARP payload");
165 goto out;
166 }
Tobias Klauserbe39ee12009-08-10 10:10:55 +0200167 printk(" ARP MAC SRC=%pM ARP IP SRC=%pI4 ARP MAC DST=%pM ARP IP DST=%pI4",
168 ap->mac_src, ap->ip_src, ap->mac_dst, ap->ip_dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 }
170 }
171out:
172 printk("\n");
173 spin_unlock_bh(&ebt_log_lock);
Bart De Schuymerd5228a42005-12-13 23:14:08 -0800174
175}
176
Jan Engelhardt2d06d4a2008-10-08 11:35:15 +0200177static unsigned int
Jan Engelhardt4b560b42009-07-05 19:43:26 +0200178ebt_log_tg(struct sk_buff *skb, const struct xt_action_param *par)
Bart De Schuymerd5228a42005-12-13 23:14:08 -0800179{
Jan Engelhardt7eb35582008-10-08 11:35:19 +0200180 const struct ebt_log_info *info = par->targinfo;
Bart De Schuymerd5228a42005-12-13 23:14:08 -0800181 struct nf_loginfo li;
Eric W. Biederman686c9b52015-09-18 14:32:59 -0500182 struct net *net = par->net;
Bart De Schuymerd5228a42005-12-13 23:14:08 -0800183
184 li.type = NF_LOG_TYPE_LOG;
185 li.u.log.level = info->loglevel;
186 li.u.log.logflags = info->bitmask;
187
Pablo Neira Ayuso960649d2014-06-23 00:28:18 +0200188 /* Remember that we have to use ebt_log_packet() not to break backward
189 * compatibility. We cannot use the default bridge packet logger via
190 * nf_log_packet() with NFT_LOG_TYPE_LOG here. --Pablo
191 */
Patrick McHardybafac2a2006-02-27 13:04:17 -0800192 if (info->bitmask & EBT_LOG_NFLOG)
Gao feng30e0c6a2013-03-24 23:50:40 +0000193 nf_log_packet(net, NFPROTO_BRIDGE, par->hooknum, skb,
194 par->in, par->out, &li, "%s", info->prefix);
Patrick McHardybafac2a2006-02-27 13:04:17 -0800195 else
Hans Schillstrom8cdb46d2013-05-15 01:23:45 +0000196 ebt_log_packet(net, NFPROTO_BRIDGE, par->hooknum, skb, par->in,
Gao feng30e0c6a2013-03-24 23:50:40 +0000197 par->out, &li, info->prefix);
Jan Engelhardt0ac6ab12008-10-08 11:35:13 +0200198 return EBT_CONTINUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
200
Jan Engelhardt043ef462008-10-08 11:35:15 +0200201static struct xt_target ebt_log_tg_reg __read_mostly = {
202 .name = "log",
Jan Engelhardt001a18d2008-10-08 11:35:14 +0200203 .revision = 0,
204 .family = NFPROTO_BRIDGE,
Jan Engelhardt2d06d4a2008-10-08 11:35:15 +0200205 .target = ebt_log_tg,
206 .checkentry = ebt_log_tg_check,
Florian Westphalfc0e3df2010-02-15 18:16:26 +0100207 .targetsize = sizeof(struct ebt_log_info),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 .me = THIS_MODULE,
209};
210
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800211static int __init ebt_log_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
Pablo Neira Ayuso960649d2014-06-23 00:28:18 +0200213 return xt_register_target(&ebt_log_tg_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
215
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800216static void __exit ebt_log_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
Jan Engelhardt043ef462008-10-08 11:35:15 +0200218 xt_unregister_target(&ebt_log_tg_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
220
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800221module_init(ebt_log_init);
222module_exit(ebt_log_fini);
Jan Engelhardtf776c4c2008-01-31 04:00:30 -0800223MODULE_DESCRIPTION("Ebtables: Packet logging to syslog");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224MODULE_LICENSE("GPL");