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> |
| 6 | * |
| 7 | * April, 2002 |
| 8 | * |
| 9 | */ |
| 10 | |
| 11 | #include <linux/netfilter_bridge/ebtables.h> |
| 12 | #include <linux/netfilter_bridge/ebt_log.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/ip.h> |
| 15 | #include <linux/if_arp.h> |
| 16 | #include <linux/spinlock.h> |
| 17 | |
| 18 | static DEFINE_SPINLOCK(ebt_log_lock); |
| 19 | |
| 20 | static int ebt_log_check(const char *tablename, unsigned int hookmask, |
| 21 | const struct ebt_entry *e, void *data, unsigned int datalen) |
| 22 | { |
| 23 | struct ebt_log_info *info = (struct ebt_log_info *)data; |
| 24 | |
| 25 | if (datalen != EBT_ALIGN(sizeof(struct ebt_log_info))) |
| 26 | return -EINVAL; |
| 27 | if (info->bitmask & ~EBT_LOG_MASK) |
| 28 | return -EINVAL; |
| 29 | if (info->loglevel >= 8) |
| 30 | return -EINVAL; |
| 31 | info->prefix[EBT_LOG_PREFIX_SIZE - 1] = '\0'; |
| 32 | return 0; |
| 33 | } |
| 34 | |
| 35 | struct tcpudphdr |
| 36 | { |
| 37 | uint16_t src; |
| 38 | uint16_t dst; |
| 39 | }; |
| 40 | |
| 41 | struct arppayload |
| 42 | { |
| 43 | unsigned char mac_src[ETH_ALEN]; |
| 44 | unsigned char ip_src[4]; |
| 45 | unsigned char mac_dst[ETH_ALEN]; |
| 46 | unsigned char ip_dst[4]; |
| 47 | }; |
| 48 | |
| 49 | static void print_MAC(unsigned char *p) |
| 50 | { |
| 51 | int i; |
| 52 | |
| 53 | for (i = 0; i < ETH_ALEN; i++, p++) |
| 54 | printk("%02x%c", *p, i == ETH_ALEN - 1 ? ' ':':'); |
| 55 | } |
| 56 | |
| 57 | #define myNIPQUAD(a) a[0], a[1], a[2], a[3] |
| 58 | static void ebt_log(const struct sk_buff *skb, unsigned int hooknr, |
| 59 | const struct net_device *in, const struct net_device *out, |
| 60 | const void *data, unsigned int datalen) |
| 61 | { |
| 62 | struct ebt_log_info *info = (struct ebt_log_info *)data; |
| 63 | char level_string[4] = "< >"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
| 65 | level_string[1] = '0' + info->loglevel; |
| 66 | spin_lock_bh(&ebt_log_lock); |
| 67 | printk(level_string); |
| 68 | printk("%s IN=%s OUT=%s ", info->prefix, in ? in->name : "", |
| 69 | out ? out->name : ""); |
| 70 | |
| 71 | printk("MAC source = "); |
| 72 | print_MAC(eth_hdr(skb)->h_source); |
| 73 | printk("MAC dest = "); |
| 74 | print_MAC(eth_hdr(skb)->h_dest); |
| 75 | |
| 76 | printk("proto = 0x%04x", ntohs(eth_hdr(skb)->h_proto)); |
| 77 | |
| 78 | if ((info->bitmask & EBT_LOG_IP) && eth_hdr(skb)->h_proto == |
| 79 | htons(ETH_P_IP)){ |
| 80 | struct iphdr _iph, *ih; |
| 81 | |
| 82 | ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph); |
| 83 | if (ih == NULL) { |
| 84 | printk(" INCOMPLETE IP header"); |
| 85 | goto out; |
| 86 | } |
| 87 | printk(" IP SRC=%u.%u.%u.%u IP DST=%u.%u.%u.%u,", |
| 88 | NIPQUAD(ih->saddr), NIPQUAD(ih->daddr)); |
David S. Miller | 85c1937 | 2005-06-28 12:39:40 -0700 | [diff] [blame^] | 89 | printk(" IP tos=0x%02X, IP proto=%d", ih->tos, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | ih->protocol); |
| 91 | if (ih->protocol == IPPROTO_TCP || |
| 92 | ih->protocol == IPPROTO_UDP) { |
| 93 | struct tcpudphdr _ports, *pptr; |
| 94 | |
| 95 | pptr = skb_header_pointer(skb, ih->ihl*4, |
| 96 | sizeof(_ports), &_ports); |
| 97 | if (pptr == NULL) { |
| 98 | printk(" INCOMPLETE TCP/UDP header"); |
| 99 | goto out; |
| 100 | } |
| 101 | printk(" SPT=%u DPT=%u", ntohs(pptr->src), |
| 102 | ntohs(pptr->dst)); |
| 103 | } |
| 104 | goto out; |
| 105 | } |
| 106 | |
| 107 | if ((info->bitmask & EBT_LOG_ARP) && |
| 108 | ((eth_hdr(skb)->h_proto == htons(ETH_P_ARP)) || |
| 109 | (eth_hdr(skb)->h_proto == htons(ETH_P_RARP)))) { |
| 110 | struct arphdr _arph, *ah; |
| 111 | |
| 112 | ah = skb_header_pointer(skb, 0, sizeof(_arph), &_arph); |
| 113 | if (ah == NULL) { |
| 114 | printk(" INCOMPLETE ARP header"); |
| 115 | goto out; |
| 116 | } |
| 117 | printk(" ARP HTYPE=%d, PTYPE=0x%04x, OPCODE=%d", |
| 118 | ntohs(ah->ar_hrd), ntohs(ah->ar_pro), |
| 119 | ntohs(ah->ar_op)); |
| 120 | |
| 121 | /* If it's for Ethernet and the lengths are OK, |
| 122 | * then log the ARP payload */ |
| 123 | if (ah->ar_hrd == htons(1) && |
| 124 | ah->ar_hln == ETH_ALEN && |
| 125 | ah->ar_pln == sizeof(uint32_t)) { |
| 126 | struct arppayload _arpp, *ap; |
| 127 | |
David S. Miller | 85c1937 | 2005-06-28 12:39:40 -0700 | [diff] [blame^] | 128 | ap = skb_header_pointer(skb, sizeof(_arph), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | sizeof(_arpp), &_arpp); |
| 130 | if (ap == NULL) { |
| 131 | printk(" INCOMPLETE ARP payload"); |
| 132 | goto out; |
| 133 | } |
| 134 | printk(" ARP MAC SRC="); |
| 135 | print_MAC(ap->mac_src); |
| 136 | printk(" ARP IP SRC=%u.%u.%u.%u", |
| 137 | myNIPQUAD(ap->ip_src)); |
| 138 | printk(" ARP MAC DST="); |
| 139 | print_MAC(ap->mac_dst); |
| 140 | printk(" ARP IP DST=%u.%u.%u.%u", |
| 141 | myNIPQUAD(ap->ip_dst)); |
| 142 | } |
| 143 | } |
| 144 | out: |
| 145 | printk("\n"); |
| 146 | spin_unlock_bh(&ebt_log_lock); |
| 147 | } |
| 148 | |
| 149 | static struct ebt_watcher log = |
| 150 | { |
| 151 | .name = EBT_LOG_WATCHER, |
| 152 | .watcher = ebt_log, |
| 153 | .check = ebt_log_check, |
| 154 | .me = THIS_MODULE, |
| 155 | }; |
| 156 | |
| 157 | static int __init init(void) |
| 158 | { |
| 159 | return ebt_register_watcher(&log); |
| 160 | } |
| 161 | |
| 162 | static void __exit fini(void) |
| 163 | { |
| 164 | ebt_unregister_watcher(&log); |
| 165 | } |
| 166 | |
| 167 | module_init(init); |
| 168 | module_exit(fini); |
| 169 | MODULE_LICENSE("GPL"); |