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 | */ |
| 11 | |
| 12 | #include <linux/netfilter_bridge/ebtables.h> |
| 13 | #include <linux/netfilter_bridge/ebt_log.h> |
Bart De Schuymer | d5228a4 | 2005-12-13 23:14:08 -0800 | [diff] [blame] | 14 | #include <linux/netfilter.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/module.h> |
| 16 | #include <linux/ip.h> |
Harald Welte | 2e4e6a1 | 2006-01-12 13:30:04 -0800 | [diff] [blame] | 17 | #include <linux/in.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/if_arp.h> |
| 19 | #include <linux/spinlock.h> |
Patrick McHardy | f01ffbd | 2007-12-17 22:38:49 -0800 | [diff] [blame] | 20 | #include <net/netfilter/nf_log.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
| 22 | static DEFINE_SPINLOCK(ebt_log_lock); |
| 23 | |
| 24 | static int ebt_log_check(const char *tablename, unsigned int hookmask, |
| 25 | const struct ebt_entry *e, void *data, unsigned int datalen) |
| 26 | { |
| 27 | struct ebt_log_info *info = (struct ebt_log_info *)data; |
| 28 | |
| 29 | if (datalen != EBT_ALIGN(sizeof(struct ebt_log_info))) |
| 30 | return -EINVAL; |
| 31 | if (info->bitmask & ~EBT_LOG_MASK) |
| 32 | return -EINVAL; |
| 33 | if (info->loglevel >= 8) |
| 34 | return -EINVAL; |
| 35 | info->prefix[EBT_LOG_PREFIX_SIZE - 1] = '\0'; |
| 36 | return 0; |
| 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 | |
| 53 | static void print_MAC(unsigned char *p) |
| 54 | { |
| 55 | int i; |
| 56 | |
| 57 | for (i = 0; i < ETH_ALEN; i++, p++) |
| 58 | printk("%02x%c", *p, i == ETH_ALEN - 1 ? ' ':':'); |
| 59 | } |
| 60 | |
| 61 | #define myNIPQUAD(a) a[0], a[1], a[2], a[3] |
Bart De Schuymer | d5228a4 | 2005-12-13 23:14:08 -0800 | [diff] [blame] | 62 | static void |
| 63 | ebt_log_packet(unsigned int pf, unsigned int hooknum, |
| 64 | const struct sk_buff *skb, const struct net_device *in, |
| 65 | const struct net_device *out, const struct nf_loginfo *loginfo, |
| 66 | const char *prefix) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | { |
Bart De Schuymer | d5228a4 | 2005-12-13 23:14:08 -0800 | [diff] [blame] | 68 | unsigned int bitmask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | spin_lock_bh(&ebt_log_lock); |
Bart De Schuymer | d5228a4 | 2005-12-13 23:14:08 -0800 | [diff] [blame] | 71 | printk("<%c>%s IN=%s OUT=%s MAC source = ", '0' + loginfo->u.log.level, |
| 72 | prefix, in ? in->name : "", out ? out->name : ""); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | print_MAC(eth_hdr(skb)->h_source); |
| 75 | printk("MAC dest = "); |
| 76 | print_MAC(eth_hdr(skb)->h_dest); |
| 77 | |
| 78 | printk("proto = 0x%04x", ntohs(eth_hdr(skb)->h_proto)); |
| 79 | |
Bart De Schuymer | d5228a4 | 2005-12-13 23:14:08 -0800 | [diff] [blame] | 80 | if (loginfo->type == NF_LOG_TYPE_LOG) |
| 81 | bitmask = loginfo->u.log.logflags; |
| 82 | else |
| 83 | bitmask = NF_LOG_MASK; |
| 84 | |
| 85 | if ((bitmask & EBT_LOG_IP) && eth_hdr(skb)->h_proto == |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | htons(ETH_P_IP)){ |
| 87 | struct iphdr _iph, *ih; |
| 88 | |
| 89 | ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph); |
| 90 | if (ih == NULL) { |
| 91 | printk(" INCOMPLETE IP header"); |
| 92 | goto out; |
| 93 | } |
Bart De Schuymer | d5228a4 | 2005-12-13 23:14:08 -0800 | [diff] [blame] | 94 | printk(" IP SRC=%u.%u.%u.%u IP DST=%u.%u.%u.%u, IP " |
| 95 | "tos=0x%02X, IP proto=%d", NIPQUAD(ih->saddr), |
| 96 | NIPQUAD(ih->daddr), ih->tos, ih->protocol); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | if (ih->protocol == IPPROTO_TCP || |
Patrick McHardy | ab67a4d | 2006-01-17 13:01:31 -0800 | [diff] [blame] | 98 | ih->protocol == IPPROTO_UDP || |
Patrick McHardy | a8d0f95 | 2007-02-07 15:07:43 -0800 | [diff] [blame] | 99 | ih->protocol == IPPROTO_UDPLITE || |
Patrick McHardy | ab67a4d | 2006-01-17 13:01:31 -0800 | [diff] [blame] | 100 | ih->protocol == IPPROTO_SCTP || |
| 101 | ih->protocol == IPPROTO_DCCP) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | struct tcpudphdr _ports, *pptr; |
| 103 | |
| 104 | pptr = skb_header_pointer(skb, ih->ihl*4, |
| 105 | sizeof(_ports), &_ports); |
| 106 | if (pptr == NULL) { |
| 107 | printk(" INCOMPLETE TCP/UDP header"); |
| 108 | goto out; |
| 109 | } |
| 110 | printk(" SPT=%u DPT=%u", ntohs(pptr->src), |
| 111 | ntohs(pptr->dst)); |
| 112 | } |
| 113 | goto out; |
| 114 | } |
| 115 | |
Bart De Schuymer | d5228a4 | 2005-12-13 23:14:08 -0800 | [diff] [blame] | 116 | if ((bitmask & EBT_LOG_ARP) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | ((eth_hdr(skb)->h_proto == htons(ETH_P_ARP)) || |
| 118 | (eth_hdr(skb)->h_proto == htons(ETH_P_RARP)))) { |
| 119 | struct arphdr _arph, *ah; |
| 120 | |
| 121 | ah = skb_header_pointer(skb, 0, sizeof(_arph), &_arph); |
| 122 | if (ah == NULL) { |
| 123 | printk(" INCOMPLETE ARP header"); |
| 124 | goto out; |
| 125 | } |
| 126 | printk(" ARP HTYPE=%d, PTYPE=0x%04x, OPCODE=%d", |
| 127 | ntohs(ah->ar_hrd), ntohs(ah->ar_pro), |
| 128 | ntohs(ah->ar_op)); |
| 129 | |
| 130 | /* If it's for Ethernet and the lengths are OK, |
| 131 | * then log the ARP payload */ |
| 132 | if (ah->ar_hrd == htons(1) && |
| 133 | ah->ar_hln == ETH_ALEN && |
Al Viro | 47c183fa | 2006-11-14 21:11:51 -0800 | [diff] [blame] | 134 | ah->ar_pln == sizeof(__be32)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | struct arppayload _arpp, *ap; |
| 136 | |
David S. Miller | 85c1937 | 2005-06-28 12:39:40 -0700 | [diff] [blame] | 137 | ap = skb_header_pointer(skb, sizeof(_arph), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | sizeof(_arpp), &_arpp); |
| 139 | if (ap == NULL) { |
| 140 | printk(" INCOMPLETE ARP payload"); |
| 141 | goto out; |
| 142 | } |
| 143 | printk(" ARP MAC SRC="); |
| 144 | print_MAC(ap->mac_src); |
| 145 | printk(" ARP IP SRC=%u.%u.%u.%u", |
| 146 | myNIPQUAD(ap->ip_src)); |
| 147 | printk(" ARP MAC DST="); |
| 148 | print_MAC(ap->mac_dst); |
| 149 | printk(" ARP IP DST=%u.%u.%u.%u", |
| 150 | myNIPQUAD(ap->ip_dst)); |
| 151 | } |
| 152 | } |
| 153 | out: |
| 154 | printk("\n"); |
| 155 | spin_unlock_bh(&ebt_log_lock); |
Bart De Schuymer | d5228a4 | 2005-12-13 23:14:08 -0800 | [diff] [blame] | 156 | |
| 157 | } |
| 158 | |
| 159 | static void ebt_log(const struct sk_buff *skb, unsigned int hooknr, |
| 160 | const struct net_device *in, const struct net_device *out, |
| 161 | const void *data, unsigned int datalen) |
| 162 | { |
| 163 | struct ebt_log_info *info = (struct ebt_log_info *)data; |
| 164 | struct nf_loginfo li; |
| 165 | |
| 166 | li.type = NF_LOG_TYPE_LOG; |
| 167 | li.u.log.level = info->loglevel; |
| 168 | li.u.log.logflags = info->bitmask; |
| 169 | |
Patrick McHardy | bafac2a | 2006-02-27 13:04:17 -0800 | [diff] [blame] | 170 | if (info->bitmask & EBT_LOG_NFLOG) |
| 171 | nf_log_packet(PF_BRIDGE, hooknr, skb, in, out, &li, |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 172 | "%s", info->prefix); |
Patrick McHardy | bafac2a | 2006-02-27 13:04:17 -0800 | [diff] [blame] | 173 | else |
| 174 | ebt_log_packet(PF_BRIDGE, hooknr, skb, in, out, &li, |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 175 | info->prefix); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | static struct ebt_watcher log = |
| 179 | { |
| 180 | .name = EBT_LOG_WATCHER, |
| 181 | .watcher = ebt_log, |
| 182 | .check = ebt_log_check, |
| 183 | .me = THIS_MODULE, |
| 184 | }; |
| 185 | |
Patrick McHardy | 7b2f963 | 2007-12-17 22:39:08 -0800 | [diff] [blame] | 186 | static const struct nf_logger ebt_log_logger = { |
Bart De Schuymer | d5228a4 | 2005-12-13 23:14:08 -0800 | [diff] [blame] | 187 | .name = "ebt_log", |
| 188 | .logfn = &ebt_log_packet, |
| 189 | .me = THIS_MODULE, |
| 190 | }; |
| 191 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 192 | static int __init ebt_log_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | { |
Bart De Schuymer | d5228a4 | 2005-12-13 23:14:08 -0800 | [diff] [blame] | 194 | int ret; |
| 195 | |
| 196 | ret = ebt_register_watcher(&log); |
| 197 | if (ret < 0) |
| 198 | return ret; |
Patrick McHardy | 7e2acc7 | 2007-07-24 15:29:55 -0700 | [diff] [blame] | 199 | nf_log_register(PF_BRIDGE, &ebt_log_logger); |
| 200 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 203 | static void __exit ebt_log_fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | { |
Patrick McHardy | e92ad99 | 2007-02-12 11:11:55 -0800 | [diff] [blame] | 205 | nf_log_unregister(&ebt_log_logger); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | ebt_unregister_watcher(&log); |
| 207 | } |
| 208 | |
Andrew Morton | 65b4b4e | 2006-03-28 16:37:06 -0800 | [diff] [blame] | 209 | module_init(ebt_log_init); |
| 210 | module_exit(ebt_log_fini); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | MODULE_LICENSE("GPL"); |