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 | 2d06d4a | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 27 | static bool |
| 28 | ebt_log_tg_check(const char *table, const void *entry, |
| 29 | const struct xt_target *target, void *data, |
| 30 | unsigned int hook_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | { |
Jan Engelhardt | abfdf1c | 2008-01-31 03:59:24 -0800 | [diff] [blame] | 32 | struct ebt_log_info *info = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | if (info->bitmask & ~EBT_LOG_MASK) |
Jan Engelhardt | 19eda87 | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 35 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | if (info->loglevel >= 8) |
Jan Engelhardt | 19eda87 | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 37 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | info->prefix[EBT_LOG_PREFIX_SIZE - 1] = '\0'; |
Jan Engelhardt | 19eda87 | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 39 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | struct tcpudphdr |
| 43 | { |
Al Viro | 47c183fa | 2006-11-14 21:11:51 -0800 | [diff] [blame] | 44 | __be16 src; |
| 45 | __be16 dst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | struct arppayload |
| 49 | { |
| 50 | unsigned char mac_src[ETH_ALEN]; |
| 51 | unsigned char ip_src[4]; |
| 52 | unsigned char mac_dst[ETH_ALEN]; |
| 53 | unsigned char ip_dst[4]; |
| 54 | }; |
| 55 | |
Jan Engelhardt | abfdf1c | 2008-01-31 03:59:24 -0800 | [diff] [blame] | 56 | static void print_MAC(const unsigned char *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | { |
| 58 | int i; |
| 59 | |
| 60 | for (i = 0; i < ETH_ALEN; i++, p++) |
| 61 | printk("%02x%c", *p, i == ETH_ALEN - 1 ? ' ':':'); |
| 62 | } |
| 63 | |
Kuo-lang Tseng | 93f6515 | 2008-06-09 15:55:45 -0700 | [diff] [blame] | 64 | static void |
| 65 | print_ports(const struct sk_buff *skb, uint8_t protocol, int offset) |
| 66 | { |
| 67 | if (protocol == IPPROTO_TCP || |
| 68 | protocol == IPPROTO_UDP || |
| 69 | protocol == IPPROTO_UDPLITE || |
| 70 | protocol == IPPROTO_SCTP || |
| 71 | protocol == IPPROTO_DCCP) { |
| 72 | const struct tcpudphdr *pptr; |
| 73 | struct tcpudphdr _ports; |
| 74 | |
| 75 | pptr = skb_header_pointer(skb, offset, |
| 76 | sizeof(_ports), &_ports); |
| 77 | if (pptr == NULL) { |
| 78 | printk(" INCOMPLETE TCP/UDP header"); |
| 79 | return; |
| 80 | } |
| 81 | printk(" SPT=%u DPT=%u", ntohs(pptr->src), ntohs(pptr->dst)); |
| 82 | } |
| 83 | } |
| 84 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | #define myNIPQUAD(a) a[0], a[1], a[2], a[3] |
Bart De Schuymer | d5228a4 | 2005-12-13 23:14:08 -0800 | [diff] [blame] | 86 | static void |
Jan Engelhardt | 76108ce | 2008-10-08 11:35:00 +0200 | [diff] [blame] | 87 | ebt_log_packet(u_int8_t pf, unsigned int hooknum, |
Bart De Schuymer | d5228a4 | 2005-12-13 23:14:08 -0800 | [diff] [blame] | 88 | const struct sk_buff *skb, const struct net_device *in, |
| 89 | const struct net_device *out, const struct nf_loginfo *loginfo, |
| 90 | const char *prefix) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | { |
Bart De Schuymer | d5228a4 | 2005-12-13 23:14:08 -0800 | [diff] [blame] | 92 | unsigned int bitmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | spin_lock_bh(&ebt_log_lock); |
Bart De Schuymer | d5228a4 | 2005-12-13 23:14:08 -0800 | [diff] [blame] | 95 | printk("<%c>%s IN=%s OUT=%s MAC source = ", '0' + loginfo->u.log.level, |
| 96 | prefix, in ? in->name : "", out ? out->name : ""); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | print_MAC(eth_hdr(skb)->h_source); |
| 99 | printk("MAC dest = "); |
| 100 | print_MAC(eth_hdr(skb)->h_dest); |
| 101 | |
| 102 | printk("proto = 0x%04x", ntohs(eth_hdr(skb)->h_proto)); |
| 103 | |
Bart De Schuymer | d5228a4 | 2005-12-13 23:14:08 -0800 | [diff] [blame] | 104 | if (loginfo->type == NF_LOG_TYPE_LOG) |
| 105 | bitmask = loginfo->u.log.logflags; |
| 106 | else |
| 107 | bitmask = NF_LOG_MASK; |
| 108 | |
| 109 | if ((bitmask & EBT_LOG_IP) && eth_hdr(skb)->h_proto == |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | htons(ETH_P_IP)){ |
Jan Engelhardt | abfdf1c | 2008-01-31 03:59:24 -0800 | [diff] [blame] | 111 | const struct iphdr *ih; |
| 112 | struct iphdr _iph; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | |
| 114 | ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph); |
| 115 | if (ih == NULL) { |
| 116 | printk(" INCOMPLETE IP header"); |
| 117 | goto out; |
| 118 | } |
Bart De Schuymer | d5228a4 | 2005-12-13 23:14:08 -0800 | [diff] [blame] | 119 | printk(" IP SRC=%u.%u.%u.%u IP DST=%u.%u.%u.%u, IP " |
| 120 | "tos=0x%02X, IP proto=%d", NIPQUAD(ih->saddr), |
| 121 | NIPQUAD(ih->daddr), ih->tos, ih->protocol); |
Kuo-lang Tseng | 93f6515 | 2008-06-09 15:55:45 -0700 | [diff] [blame] | 122 | print_ports(skb, ih->protocol, ih->ihl*4); |
| 123 | goto out; |
| 124 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | |
Randy Dunlap | f586287 | 2008-06-17 16:16:13 -0700 | [diff] [blame] | 126 | #if defined(CONFIG_BRIDGE_EBT_IP6) || defined(CONFIG_BRIDGE_EBT_IP6_MODULE) |
Kuo-lang Tseng | 93f6515 | 2008-06-09 15:55:45 -0700 | [diff] [blame] | 127 | if ((bitmask & EBT_LOG_IP6) && eth_hdr(skb)->h_proto == |
| 128 | htons(ETH_P_IPV6)) { |
| 129 | const struct ipv6hdr *ih; |
| 130 | struct ipv6hdr _iph; |
| 131 | uint8_t nexthdr; |
| 132 | int offset_ph; |
| 133 | |
| 134 | ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph); |
| 135 | if (ih == NULL) { |
| 136 | printk(" INCOMPLETE IPv6 header"); |
| 137 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | } |
Kuo-lang Tseng | 93f6515 | 2008-06-09 15:55:45 -0700 | [diff] [blame] | 139 | printk(" IPv6 SRC=%x:%x:%x:%x:%x:%x:%x:%x " |
| 140 | "IPv6 DST=%x:%x:%x:%x:%x:%x:%x:%x, IPv6 " |
| 141 | "priority=0x%01X, Next Header=%d", NIP6(ih->saddr), |
| 142 | NIP6(ih->daddr), ih->priority, ih->nexthdr); |
| 143 | nexthdr = ih->nexthdr; |
| 144 | offset_ph = ipv6_skip_exthdr(skb, sizeof(_iph), &nexthdr); |
| 145 | if (offset_ph == -1) |
| 146 | goto out; |
| 147 | print_ports(skb, nexthdr, offset_ph); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | goto out; |
| 149 | } |
Randy Dunlap | f586287 | 2008-06-17 16:16:13 -0700 | [diff] [blame] | 150 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | |
Bart De Schuymer | d5228a4 | 2005-12-13 23:14:08 -0800 | [diff] [blame] | 152 | if ((bitmask & EBT_LOG_ARP) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | ((eth_hdr(skb)->h_proto == htons(ETH_P_ARP)) || |
| 154 | (eth_hdr(skb)->h_proto == htons(ETH_P_RARP)))) { |
Jan Engelhardt | abfdf1c | 2008-01-31 03:59:24 -0800 | [diff] [blame] | 155 | const struct arphdr *ah; |
| 156 | struct arphdr _arph; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | |
| 158 | ah = skb_header_pointer(skb, 0, sizeof(_arph), &_arph); |
| 159 | if (ah == NULL) { |
| 160 | printk(" INCOMPLETE ARP header"); |
| 161 | goto out; |
| 162 | } |
| 163 | printk(" ARP HTYPE=%d, PTYPE=0x%04x, OPCODE=%d", |
| 164 | ntohs(ah->ar_hrd), ntohs(ah->ar_pro), |
| 165 | ntohs(ah->ar_op)); |
| 166 | |
| 167 | /* If it's for Ethernet and the lengths are OK, |
| 168 | * then log the ARP payload */ |
| 169 | if (ah->ar_hrd == htons(1) && |
| 170 | ah->ar_hln == ETH_ALEN && |
Al Viro | 47c183fa | 2006-11-14 21:11:51 -0800 | [diff] [blame] | 171 | ah->ar_pln == sizeof(__be32)) { |
Jan Engelhardt | abfdf1c | 2008-01-31 03:59:24 -0800 | [diff] [blame] | 172 | const struct arppayload *ap; |
| 173 | struct arppayload _arpp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | |
David S. Miller | 85c1937 | 2005-06-28 12:39:40 -0700 | [diff] [blame] | 175 | ap = skb_header_pointer(skb, sizeof(_arph), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | sizeof(_arpp), &_arpp); |
| 177 | if (ap == NULL) { |
| 178 | printk(" INCOMPLETE ARP payload"); |
| 179 | goto out; |
| 180 | } |
| 181 | printk(" ARP MAC SRC="); |
| 182 | print_MAC(ap->mac_src); |
| 183 | printk(" ARP IP SRC=%u.%u.%u.%u", |
| 184 | myNIPQUAD(ap->ip_src)); |
| 185 | printk(" ARP MAC DST="); |
| 186 | print_MAC(ap->mac_dst); |
| 187 | printk(" ARP IP DST=%u.%u.%u.%u", |
| 188 | myNIPQUAD(ap->ip_dst)); |
| 189 | } |
| 190 | } |
| 191 | out: |
| 192 | printk("\n"); |
| 193 | spin_unlock_bh(&ebt_log_lock); |
Bart De Schuymer | d5228a4 | 2005-12-13 23:14:08 -0800 | [diff] [blame] | 194 | |
| 195 | } |
| 196 | |
Jan Engelhardt | 2d06d4a | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 197 | static unsigned int |
Jan Engelhardt | 7eb3558 | 2008-10-08 11:35:19 +0200 | [diff] [blame^] | 198 | ebt_log_tg(struct sk_buff *skb, const struct xt_target_param *par) |
Bart De Schuymer | d5228a4 | 2005-12-13 23:14:08 -0800 | [diff] [blame] | 199 | { |
Jan Engelhardt | 7eb3558 | 2008-10-08 11:35:19 +0200 | [diff] [blame^] | 200 | const struct ebt_log_info *info = par->targinfo; |
Bart De Schuymer | d5228a4 | 2005-12-13 23:14:08 -0800 | [diff] [blame] | 201 | struct nf_loginfo li; |
| 202 | |
| 203 | li.type = NF_LOG_TYPE_LOG; |
| 204 | li.u.log.level = info->loglevel; |
| 205 | li.u.log.logflags = info->bitmask; |
| 206 | |
Patrick McHardy | bafac2a | 2006-02-27 13:04:17 -0800 | [diff] [blame] | 207 | if (info->bitmask & EBT_LOG_NFLOG) |
Jan Engelhardt | 7eb3558 | 2008-10-08 11:35:19 +0200 | [diff] [blame^] | 208 | nf_log_packet(NFPROTO_BRIDGE, par->hooknum, skb, par->in, |
| 209 | par->out, &li, "%s", info->prefix); |
Patrick McHardy | bafac2a | 2006-02-27 13:04:17 -0800 | [diff] [blame] | 210 | else |
Jan Engelhardt | 7eb3558 | 2008-10-08 11:35:19 +0200 | [diff] [blame^] | 211 | ebt_log_packet(NFPROTO_BRIDGE, par->hooknum, skb, par->in, |
| 212 | par->out, &li, info->prefix); |
Jan Engelhardt | 0ac6ab1 | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 213 | return EBT_CONTINUE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Jan Engelhardt | 043ef46 | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 216 | static struct xt_target ebt_log_tg_reg __read_mostly = { |
| 217 | .name = "log", |
Jan Engelhardt | 001a18d | 2008-10-08 11:35:14 +0200 | [diff] [blame] | 218 | .revision = 0, |
| 219 | .family = NFPROTO_BRIDGE, |
Jan Engelhardt | 2d06d4a | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 220 | .target = ebt_log_tg, |
| 221 | .checkentry = ebt_log_tg_check, |
Jan Engelhardt | 18219d3 | 2008-10-08 11:35:13 +0200 | [diff] [blame] | 222 | .targetsize = XT_ALIGN(sizeof(struct ebt_log_info)), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | .me = THIS_MODULE, |
| 224 | }; |
| 225 | |
Patrick McHardy | 7b2f963 | 2007-12-17 22:39:08 -0800 | [diff] [blame] | 226 | static const struct nf_logger ebt_log_logger = { |
Bart De Schuymer | d5228a4 | 2005-12-13 23:14:08 -0800 | [diff] [blame] | 227 | .name = "ebt_log", |
| 228 | .logfn = &ebt_log_packet, |
| 229 | .me = THIS_MODULE, |
| 230 | }; |
| 231 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 232 | static int __init ebt_log_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | { |
Bart De Schuymer | d5228a4 | 2005-12-13 23:14:08 -0800 | [diff] [blame] | 234 | int ret; |
| 235 | |
Jan Engelhardt | 043ef46 | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 236 | ret = xt_register_target(&ebt_log_tg_reg); |
Bart De Schuymer | d5228a4 | 2005-12-13 23:14:08 -0800 | [diff] [blame] | 237 | if (ret < 0) |
| 238 | return ret; |
Jan Engelhardt | ee999d8 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 239 | nf_log_register(NFPROTO_BRIDGE, &ebt_log_logger); |
Patrick McHardy | 7e2acc7 | 2007-07-24 15:29:55 -0700 | [diff] [blame] | 240 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | } |
| 242 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 243 | static void __exit ebt_log_fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | { |
Patrick McHardy | e92ad99 | 2007-02-12 11:11:55 -0800 | [diff] [blame] | 245 | nf_log_unregister(&ebt_log_logger); |
Jan Engelhardt | 043ef46 | 2008-10-08 11:35:15 +0200 | [diff] [blame] | 246 | xt_unregister_target(&ebt_log_tg_reg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 249 | module_init(ebt_log_init); |
| 250 | module_exit(ebt_log_fini); |
Jan Engelhardt | f776c4c | 2008-01-31 04:00:30 -0800 | [diff] [blame] | 251 | MODULE_DESCRIPTION("Ebtables: Packet logging to syslog"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | MODULE_LICENSE("GPL"); |