Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * ebt_log |
| 3 | * |
| 4 | * Authors: |
| 5 | * Bart De Schuymer <bdschuym@pandora.be> |
Bart De Schuymer | d5228a4 | 2005-12-13 23:14:08 -0800 | [diff] [blame] | 6 | * Harald Welte <laforge@netfilter.org> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * |
| 8 | * April, 2002 |
| 9 | * |
| 10 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/module.h> |
| 12 | #include <linux/ip.h> |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 13 | #include <linux/in.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/if_arp.h> |
| 15 | #include <linux/spinlock.h> |
Patrick McHardy | f01ffbd | 2007-12-17 22:38:49 -0800 | [diff] [blame] | 16 | #include <net/netfilter/nf_log.h> |
Kuo-lang Tseng | 93f6515 | 2008-06-09 15:55:45 -0700 | [diff] [blame] | 17 | #include <linux/ipv6.h> |
| 18 | #include <net/ipv6.h> |
| 19 | #include <linux/in6.h> |
Jan Engelhardt | 18219d3 | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 20 | #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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
| 25 | static DEFINE_SPINLOCK(ebt_log_lock); |
| 26 | |
Jan Engelhardt | 135367b | 2010-03-19 17:16:42 +0100 | [diff] [blame] | 27 | static int ebt_log_tg_check(const struct xt_tgchk_param *par) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | { |
Jan Engelhardt | af5d6dc | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 29 | struct ebt_log_info *info = par->targinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | if (info->bitmask & ~EBT_LOG_MASK) |
Jan Engelhardt | d6b00a5 | 2010-03-25 16:34:45 +0100 | [diff] [blame] | 32 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | if (info->loglevel >= 8) |
Jan Engelhardt | d6b00a5 | 2010-03-25 16:34:45 +0100 | [diff] [blame] | 34 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | info->prefix[EBT_LOG_PREFIX_SIZE - 1] = '\0'; |
Jan Engelhardt | d6b00a5 | 2010-03-25 16:34:45 +0100 | [diff] [blame] | 36 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | struct tcpudphdr |
| 40 | { |
Al Viro | 47c183fa | 2006-11-14 21:11:51 -0800 | [diff] [blame] | 41 | __be16 src; |
| 42 | __be16 dst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | struct 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 Tseng | 93f6515 | 2008-06-09 15:55:45 -0700 | [diff] [blame] | 53 | static void |
| 54 | print_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 Schuymer | d5228a4 | 2005-12-13 23:14:08 -0800 | [diff] [blame] | 74 | static void |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 75 | ebt_log_packet(u_int8_t pf, unsigned int hooknum, |
Bart De Schuymer | d5228a4 | 2005-12-13 23:14:08 -0800 | [diff] [blame] | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | { |
Bart De Schuymer | d5228a4 | 2005-12-13 23:14:08 -0800 | [diff] [blame] | 80 | unsigned int bitmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | spin_lock_bh(&ebt_log_lock); |
Joe Perches | 16af511 | 2012-09-12 02:04:53 +0000 | [diff] [blame] | 83 | printk(KERN_SOH "%c%s IN=%s OUT=%s MAC source = %pM MAC dest = %pM proto = 0x%04x", |
Tobias Klauser | be39ee1 | 2009-08-10 10:10:55 +0200 | [diff] [blame] | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | |
Bart De Schuymer | d5228a4 | 2005-12-13 23:14:08 -0800 | [diff] [blame] | 89 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | htons(ETH_P_IP)){ |
Jan Engelhardt | abfdf1c | 2008-01-31 03:59:24 -0800 | [diff] [blame] | 96 | const struct iphdr *ih; |
| 97 | struct iphdr _iph; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | |
| 99 | ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph); |
| 100 | if (ih == NULL) { |
| 101 | printk(" INCOMPLETE IP header"); |
| 102 | goto out; |
| 103 | } |
Harvey Harrison | 21454aa | 2008-10-31 00:54:56 -0700 | [diff] [blame] | 104 | 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 Tseng | 93f6515 | 2008-06-09 15:55:45 -0700 | [diff] [blame] | 106 | print_ports(skb, ih->protocol, ih->ihl*4); |
| 107 | goto out; |
| 108 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | |
Igor Maravić | e6373c4 | 2011-12-12 02:58:25 +0000 | [diff] [blame] | 110 | #if IS_ENABLED(CONFIG_BRIDGE_EBT_IP6) |
Kuo-lang Tseng | 93f6515 | 2008-06-09 15:55:45 -0700 | [diff] [blame] | 111 | 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 Gross | 75f2811 | 2011-11-30 17:05:51 -0800 | [diff] [blame] | 116 | __be16 frag_off; |
Kuo-lang Tseng | 93f6515 | 2008-06-09 15:55:45 -0700 | [diff] [blame] | 117 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | } |
Harvey Harrison | 5b095d989 | 2008-10-29 12:52:50 -0700 | [diff] [blame] | 124 | printk(" IPv6 SRC=%pI6 IPv6 DST=%pI6, IPv6 priority=0x%01X, Next Header=%d", |
Harvey Harrison | b189db5 | 2008-10-28 22:38:52 -0700 | [diff] [blame] | 125 | &ih->saddr, &ih->daddr, ih->priority, ih->nexthdr); |
Kuo-lang Tseng | 93f6515 | 2008-06-09 15:55:45 -0700 | [diff] [blame] | 126 | nexthdr = ih->nexthdr; |
Jesse Gross | 75f2811 | 2011-11-30 17:05:51 -0800 | [diff] [blame] | 127 | offset_ph = ipv6_skip_exthdr(skb, sizeof(_iph), &nexthdr, &frag_off); |
Kuo-lang Tseng | 93f6515 | 2008-06-09 15:55:45 -0700 | [diff] [blame] | 128 | if (offset_ph == -1) |
| 129 | goto out; |
| 130 | print_ports(skb, nexthdr, offset_ph); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | goto out; |
| 132 | } |
Randy Dunlap | f586287 | 2008-06-17 16:16:13 -0700 | [diff] [blame] | 133 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | |
Bart De Schuymer | d5228a4 | 2005-12-13 23:14:08 -0800 | [diff] [blame] | 135 | if ((bitmask & EBT_LOG_ARP) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | ((eth_hdr(skb)->h_proto == htons(ETH_P_ARP)) || |
| 137 | (eth_hdr(skb)->h_proto == htons(ETH_P_RARP)))) { |
Jan Engelhardt | abfdf1c | 2008-01-31 03:59:24 -0800 | [diff] [blame] | 138 | const struct arphdr *ah; |
| 139 | struct arphdr _arph; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | |
| 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 Viro | 47c183fa | 2006-11-14 21:11:51 -0800 | [diff] [blame] | 154 | ah->ar_pln == sizeof(__be32)) { |
Jan Engelhardt | abfdf1c | 2008-01-31 03:59:24 -0800 | [diff] [blame] | 155 | const struct arppayload *ap; |
| 156 | struct arppayload _arpp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | |
David S. Miller | 85c1937 | 2005-06-28 12:39:40 -0700 | [diff] [blame] | 158 | ap = skb_header_pointer(skb, sizeof(_arph), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | sizeof(_arpp), &_arpp); |
| 160 | if (ap == NULL) { |
| 161 | printk(" INCOMPLETE ARP payload"); |
| 162 | goto out; |
| 163 | } |
Tobias Klauser | be39ee1 | 2009-08-10 10:10:55 +0200 | [diff] [blame] | 164 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | out: |
| 169 | printk("\n"); |
| 170 | spin_unlock_bh(&ebt_log_lock); |
Bart De Schuymer | d5228a4 | 2005-12-13 23:14:08 -0800 | [diff] [blame] | 171 | |
| 172 | } |
| 173 | |
Jan Engelhardt | 2d06d4a | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 174 | static unsigned int |
Jan Engelhardt | 4b560b4 | 2009-07-05 19:43:26 +0200 | [diff] [blame] | 175 | ebt_log_tg(struct sk_buff *skb, const struct xt_action_param *par) |
Bart De Schuymer | d5228a4 | 2005-12-13 23:14:08 -0800 | [diff] [blame] | 176 | { |
Jan Engelhardt | 7eb3558 | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 177 | const struct ebt_log_info *info = par->targinfo; |
Bart De Schuymer | d5228a4 | 2005-12-13 23:14:08 -0800 | [diff] [blame] | 178 | struct nf_loginfo li; |
Gao feng | 30e0c6a | 2013-03-24 23:50:40 +0000 | [diff] [blame^] | 179 | struct net *net = dev_net(par->in ? par->in : par->out); |
Bart De Schuymer | d5228a4 | 2005-12-13 23:14:08 -0800 | [diff] [blame] | 180 | |
| 181 | li.type = NF_LOG_TYPE_LOG; |
| 182 | li.u.log.level = info->loglevel; |
| 183 | li.u.log.logflags = info->bitmask; |
| 184 | |
Patrick McHardy | bafac2a | 2006-02-27 13:04:17 -0800 | [diff] [blame] | 185 | if (info->bitmask & EBT_LOG_NFLOG) |
Gao feng | 30e0c6a | 2013-03-24 23:50:40 +0000 | [diff] [blame^] | 186 | nf_log_packet(net, NFPROTO_BRIDGE, par->hooknum, skb, |
| 187 | par->in, par->out, &li, "%s", info->prefix); |
Patrick McHardy | bafac2a | 2006-02-27 13:04:17 -0800 | [diff] [blame] | 188 | else |
Jan Engelhardt | 7eb3558 | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 189 | ebt_log_packet(NFPROTO_BRIDGE, par->hooknum, skb, par->in, |
Gao feng | 30e0c6a | 2013-03-24 23:50:40 +0000 | [diff] [blame^] | 190 | par->out, &li, info->prefix); |
Jan Engelhardt | 0ac6ab1 | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 191 | return EBT_CONTINUE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Jan Engelhardt | 043ef46 | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 194 | static struct xt_target ebt_log_tg_reg __read_mostly = { |
| 195 | .name = "log", |
Jan Engelhardt | 001a18d | 2008-10-08 11:35:14 +0200 | [diff] [blame] | 196 | .revision = 0, |
| 197 | .family = NFPROTO_BRIDGE, |
Jan Engelhardt | 2d06d4a | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 198 | .target = ebt_log_tg, |
| 199 | .checkentry = ebt_log_tg_check, |
Florian Westphal | fc0e3df | 2010-02-15 18:16:26 +0100 | [diff] [blame] | 200 | .targetsize = sizeof(struct ebt_log_info), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | .me = THIS_MODULE, |
| 202 | }; |
| 203 | |
Eric Leblond | 704b3ea | 2009-03-26 01:03:23 -0700 | [diff] [blame] | 204 | static struct nf_logger ebt_log_logger __read_mostly = { |
Bart De Schuymer | d5228a4 | 2005-12-13 23:14:08 -0800 | [diff] [blame] | 205 | .name = "ebt_log", |
| 206 | .logfn = &ebt_log_packet, |
| 207 | .me = THIS_MODULE, |
| 208 | }; |
| 209 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 210 | static int __init ebt_log_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | { |
Bart De Schuymer | d5228a4 | 2005-12-13 23:14:08 -0800 | [diff] [blame] | 212 | int ret; |
| 213 | |
Jan Engelhardt | 043ef46 | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 214 | ret = xt_register_target(&ebt_log_tg_reg); |
Bart De Schuymer | d5228a4 | 2005-12-13 23:14:08 -0800 | [diff] [blame] | 215 | if (ret < 0) |
| 216 | return ret; |
Jan Engelhardt | ee999d8 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 217 | nf_log_register(NFPROTO_BRIDGE, &ebt_log_logger); |
Patrick McHardy | 7e2acc7 | 2007-07-24 15:29:55 -0700 | [diff] [blame] | 218 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 221 | static void __exit ebt_log_fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | { |
Patrick McHardy | e92ad99 | 2007-02-12 11:11:55 -0800 | [diff] [blame] | 223 | nf_log_unregister(&ebt_log_logger); |
Jan Engelhardt | 043ef46 | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 224 | xt_unregister_target(&ebt_log_tg_reg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 227 | module_init(ebt_log_init); |
| 228 | module_exit(ebt_log_fini); |
Jan Engelhardt | f776c4c | 2008-01-31 04:00:30 -0800 | [diff] [blame] | 229 | MODULE_DESCRIPTION("Ebtables: Packet logging to syslog"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | MODULE_LICENSE("GPL"); |