]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - net/bridge/netfilter/ebt_log.c
c883ec8a28b46c77673b1ada6316fd10ae983353
[linux-2.6-omap-h63xx.git] / net / bridge / netfilter / ebt_log.c
1 /*
2  *  ebt_log
3  *
4  *      Authors:
5  *      Bart De Schuymer <bdschuym@pandora.be>
6  *      Harald Welte <laforge@netfilter.org>
7  *
8  *  April, 2002
9  *
10  */
11
12 #include <linux/netfilter_bridge/ebtables.h>
13 #include <linux/netfilter_bridge/ebt_log.h>
14 #include <linux/netfilter.h>
15 #include <linux/module.h>
16 #include <linux/ip.h>
17 #include <linux/in.h>
18 #include <linux/if_arp.h>
19 #include <linux/spinlock.h>
20 #include <net/netfilter/nf_log.h>
21 #include <linux/ipv6.h>
22 #include <net/ipv6.h>
23 #include <linux/in6.h>
24
25 static DEFINE_SPINLOCK(ebt_log_lock);
26
27 static int ebt_log_check(const char *tablename, unsigned int hookmask,
28    const struct ebt_entry *e, void *data, unsigned int datalen)
29 {
30         struct ebt_log_info *info = data;
31
32         if (datalen != EBT_ALIGN(sizeof(struct ebt_log_info)))
33                 return -EINVAL;
34         if (info->bitmask & ~EBT_LOG_MASK)
35                 return -EINVAL;
36         if (info->loglevel >= 8)
37                 return -EINVAL;
38         info->prefix[EBT_LOG_PREFIX_SIZE - 1] = '\0';
39         return 0;
40 }
41
42 struct tcpudphdr
43 {
44         __be16 src;
45         __be16 dst;
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
56 static void print_MAC(const unsigned char *p)
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
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
85 #define myNIPQUAD(a) a[0], a[1], a[2], a[3]
86 static void
87 ebt_log_packet(unsigned int pf, unsigned int hooknum,
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)
91 {
92         unsigned int bitmask;
93
94         spin_lock_bh(&ebt_log_lock);
95         printk("<%c>%s IN=%s OUT=%s MAC source = ", '0' + loginfo->u.log.level,
96                prefix, in ? in->name : "", out ? out->name : "");
97
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
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 ==
110            htons(ETH_P_IP)){
111                 const struct iphdr *ih;
112                 struct iphdr _iph;
113
114                 ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph);
115                 if (ih == NULL) {
116                         printk(" INCOMPLETE IP header");
117                         goto out;
118                 }
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);
122                 print_ports(skb, ih->protocol, ih->ihl*4);
123                 goto out;
124         }
125
126         if ((bitmask & EBT_LOG_IP6) && eth_hdr(skb)->h_proto ==
127            htons(ETH_P_IPV6)) {
128                 const struct ipv6hdr *ih;
129                 struct ipv6hdr _iph;
130                 uint8_t nexthdr;
131                 int offset_ph;
132
133                 ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph);
134                 if (ih == NULL) {
135                         printk(" INCOMPLETE IPv6 header");
136                         goto out;
137                 }
138                 printk(" IPv6 SRC=%x:%x:%x:%x:%x:%x:%x:%x "
139                        "IPv6 DST=%x:%x:%x:%x:%x:%x:%x:%x, IPv6 "
140                        "priority=0x%01X, Next Header=%d", NIP6(ih->saddr),
141                        NIP6(ih->daddr), ih->priority, ih->nexthdr);
142                 nexthdr = ih->nexthdr;
143                 offset_ph = ipv6_skip_exthdr(skb, sizeof(_iph), &nexthdr);
144                 if (offset_ph == -1)
145                         goto out;
146                 print_ports(skb, nexthdr, offset_ph);
147                 goto out;
148         }
149
150         if ((bitmask & EBT_LOG_ARP) &&
151             ((eth_hdr(skb)->h_proto == htons(ETH_P_ARP)) ||
152              (eth_hdr(skb)->h_proto == htons(ETH_P_RARP)))) {
153                 const struct arphdr *ah;
154                 struct arphdr _arph;
155
156                 ah = skb_header_pointer(skb, 0, sizeof(_arph), &_arph);
157                 if (ah == NULL) {
158                         printk(" INCOMPLETE ARP header");
159                         goto out;
160                 }
161                 printk(" ARP HTYPE=%d, PTYPE=0x%04x, OPCODE=%d",
162                        ntohs(ah->ar_hrd), ntohs(ah->ar_pro),
163                        ntohs(ah->ar_op));
164
165                 /* If it's for Ethernet and the lengths are OK,
166                  * then log the ARP payload */
167                 if (ah->ar_hrd == htons(1) &&
168                     ah->ar_hln == ETH_ALEN &&
169                     ah->ar_pln == sizeof(__be32)) {
170                         const struct arppayload *ap;
171                         struct arppayload _arpp;
172
173                         ap = skb_header_pointer(skb, sizeof(_arph),
174                                                 sizeof(_arpp), &_arpp);
175                         if (ap == NULL) {
176                                 printk(" INCOMPLETE ARP payload");
177                                 goto out;
178                         }
179                         printk(" ARP MAC SRC=");
180                         print_MAC(ap->mac_src);
181                         printk(" ARP IP SRC=%u.%u.%u.%u",
182                                myNIPQUAD(ap->ip_src));
183                         printk(" ARP MAC DST=");
184                         print_MAC(ap->mac_dst);
185                         printk(" ARP IP DST=%u.%u.%u.%u",
186                                myNIPQUAD(ap->ip_dst));
187                 }
188         }
189 out:
190         printk("\n");
191         spin_unlock_bh(&ebt_log_lock);
192
193 }
194
195 static void ebt_log(const struct sk_buff *skb, unsigned int hooknr,
196    const struct net_device *in, const struct net_device *out,
197    const void *data, unsigned int datalen)
198 {
199         const struct ebt_log_info *info = data;
200         struct nf_loginfo li;
201
202         li.type = NF_LOG_TYPE_LOG;
203         li.u.log.level = info->loglevel;
204         li.u.log.logflags = info->bitmask;
205
206         if (info->bitmask & EBT_LOG_NFLOG)
207                 nf_log_packet(PF_BRIDGE, hooknr, skb, in, out, &li,
208                               "%s", info->prefix);
209         else
210                 ebt_log_packet(PF_BRIDGE, hooknr, skb, in, out, &li,
211                                info->prefix);
212 }
213
214 static struct ebt_watcher log =
215 {
216         .name           = EBT_LOG_WATCHER,
217         .watcher        = ebt_log,
218         .check          = ebt_log_check,
219         .me             = THIS_MODULE,
220 };
221
222 static const struct nf_logger ebt_log_logger = {
223         .name           = "ebt_log",
224         .logfn          = &ebt_log_packet,
225         .me             = THIS_MODULE,
226 };
227
228 static int __init ebt_log_init(void)
229 {
230         int ret;
231
232         ret = ebt_register_watcher(&log);
233         if (ret < 0)
234                 return ret;
235         nf_log_register(PF_BRIDGE, &ebt_log_logger);
236         return 0;
237 }
238
239 static void __exit ebt_log_fini(void)
240 {
241         nf_log_unregister(&ebt_log_logger);
242         ebt_unregister_watcher(&log);
243 }
244
245 module_init(ebt_log_init);
246 module_exit(ebt_log_fini);
247 MODULE_DESCRIPTION("Ebtables: Packet logging to syslog");
248 MODULE_LICENSE("GPL");