ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / net / bridge / netfilter / ebt_log.c
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 spinlock_t ebt_log_lock = SPIN_LOCK_UNLOCKED;
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, const struct net_device *in,
59    const struct net_device *out, const void *data, unsigned int datalen)
60 {
61         struct ebt_log_info *info = (struct ebt_log_info *)data;
62         char level_string[4] = "< >";
63         union {struct iphdr iph; struct tcpudphdr ports;
64                struct arphdr arph; struct arppayload arpp;} u;
65
66         level_string[1] = '0' + info->loglevel;
67         spin_lock_bh(&ebt_log_lock);
68         printk(level_string);
69         printk("%s IN=%s OUT=%s ", info->prefix, in ? in->name : "",
70            out ? out->name : "");
71
72         printk("MAC source = ");
73         print_MAC((skb->mac.ethernet)->h_source);
74         printk("MAC dest = ");
75         print_MAC((skb->mac.ethernet)->h_dest);
76
77         printk("proto = 0x%04x", ntohs(((*skb).mac.ethernet)->h_proto));
78
79         if ((info->bitmask & EBT_LOG_IP) && skb->mac.ethernet->h_proto ==
80            htons(ETH_P_IP)){
81                 if (skb_copy_bits(skb, 0, &u.iph, sizeof(u.iph))) {
82                         printk(" INCOMPLETE IP header");
83                         goto out;
84                 }
85                 printk(" IP SRC=%u.%u.%u.%u IP DST=%u.%u.%u.%u,",
86                    NIPQUAD(u.iph.saddr), NIPQUAD(u.iph.daddr));
87                 printk(" IP tos=0x%02X, IP proto=%d", u.iph.tos,
88                        u.iph.protocol);
89                 if (u.iph.protocol == IPPROTO_TCP ||
90                     u.iph.protocol == IPPROTO_UDP) {
91                         if (skb_copy_bits(skb, u.iph.ihl*4, &u.ports,
92                             sizeof(u.ports))) {
93                                 printk(" INCOMPLETE TCP/UDP header");
94                                 goto out;
95                         }
96                         printk(" SPT=%u DPT=%u", ntohs(u.ports.src),
97                            ntohs(u.ports.dst));
98                 }
99                 goto out;
100         }
101
102         if ((info->bitmask & EBT_LOG_ARP) &&
103             ((skb->mac.ethernet->h_proto == __constant_htons(ETH_P_ARP)) ||
104             (skb->mac.ethernet->h_proto == __constant_htons(ETH_P_RARP)))) {
105                 if (skb_copy_bits(skb, 0, &u.arph, sizeof(u.arph))) {
106                         printk(" INCOMPLETE ARP header");
107                         goto out;
108                 }
109                 printk(" ARP HTYPE=%d, PTYPE=0x%04x, OPCODE=%d",
110                        ntohs(u.arph.ar_hrd), ntohs(u.arph.ar_pro),
111                        ntohs(u.arph.ar_op));
112
113                 /* If it's for Ethernet and the lengths are OK,
114                  * then log the ARP payload */
115                 if (u.arph.ar_hrd == __constant_htons(1) &&
116                     u.arph.ar_hln == ETH_ALEN &&
117                     u.arph.ar_pln == sizeof(uint32_t)) {
118                         if (skb_copy_bits(skb, sizeof(u.arph), &u.arpp,
119                             sizeof(u.arpp))) {
120                                 printk(" INCOMPLETE ARP payload");
121                                 goto out;
122                         }
123                         printk(" ARP MAC SRC=");
124                         print_MAC(u.arpp.mac_src);
125                         printk(" ARP IP SRC=%u.%u.%u.%u",
126                                myNIPQUAD(u.arpp.ip_src));
127                         printk(" ARP MAC DST=");
128                         print_MAC(u.arpp.mac_dst);
129                         printk(" ARP IP DST=%u.%u.%u.%u",
130                                myNIPQUAD(u.arpp.ip_dst));
131                 }
132         }
133 out:
134         printk("\n");
135         spin_unlock_bh(&ebt_log_lock);
136 }
137
138 static struct ebt_watcher log =
139 {
140         .name           = EBT_LOG_WATCHER,
141         .watcher        = ebt_log,
142         .check          = ebt_log_check,
143         .me             = THIS_MODULE,
144 };
145
146 static int __init init(void)
147 {
148         return ebt_register_watcher(&log);
149 }
150
151 static void __exit fini(void)
152 {
153         ebt_unregister_watcher(&log);
154 }
155
156 module_init(init);
157 module_exit(fini);
158 MODULE_LICENSE("GPL");