netfilter: ebtables: do centralized size checking
[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  *      Harald Welte <laforge@netfilter.org>
7  *
8  *  April, 2002
9  *
10  */
11 #include <linux/module.h>
12 #include <linux/ip.h>
13 #include <linux/in.h>
14 #include <linux/if_arp.h>
15 #include <linux/spinlock.h>
16 #include <net/netfilter/nf_log.h>
17 #include <linux/ipv6.h>
18 #include <net/ipv6.h>
19 #include <linux/in6.h>
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>
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 (info->bitmask & ~EBT_LOG_MASK)
33                 return -EINVAL;
34         if (info->loglevel >= 8)
35                 return -EINVAL;
36         info->prefix[EBT_LOG_PREFIX_SIZE - 1] = '\0';
37         return 0;
38 }
39
40 struct tcpudphdr
41 {
42         __be16 src;
43         __be16 dst;
44 };
45
46 struct arppayload
47 {
48         unsigned char mac_src[ETH_ALEN];
49         unsigned char ip_src[4];
50         unsigned char mac_dst[ETH_ALEN];
51         unsigned char ip_dst[4];
52 };
53
54 static void print_MAC(const unsigned char *p)
55 {
56         int i;
57
58         for (i = 0; i < ETH_ALEN; i++, p++)
59                 printk("%02x%c", *p, i == ETH_ALEN - 1 ? ' ':':');
60 }
61
62 static void
63 print_ports(const struct sk_buff *skb, uint8_t protocol, int offset)
64 {
65         if (protocol == IPPROTO_TCP ||
66             protocol == IPPROTO_UDP ||
67             protocol == IPPROTO_UDPLITE ||
68             protocol == IPPROTO_SCTP ||
69             protocol == IPPROTO_DCCP) {
70                 const struct tcpudphdr *pptr;
71                 struct tcpudphdr _ports;
72
73                 pptr = skb_header_pointer(skb, offset,
74                                           sizeof(_ports), &_ports);
75                 if (pptr == NULL) {
76                         printk(" INCOMPLETE TCP/UDP header");
77                         return;
78                 }
79                 printk(" SPT=%u DPT=%u", ntohs(pptr->src), ntohs(pptr->dst));
80         }
81 }
82
83 #define myNIPQUAD(a) a[0], a[1], a[2], a[3]
84 static void
85 ebt_log_packet(u_int8_t pf, unsigned int hooknum,
86    const struct sk_buff *skb, const struct net_device *in,
87    const struct net_device *out, const struct nf_loginfo *loginfo,
88    const char *prefix)
89 {
90         unsigned int bitmask;
91
92         spin_lock_bh(&ebt_log_lock);
93         printk("<%c>%s IN=%s OUT=%s MAC source = ", '0' + loginfo->u.log.level,
94                prefix, in ? in->name : "", out ? out->name : "");
95
96         print_MAC(eth_hdr(skb)->h_source);
97         printk("MAC dest = ");
98         print_MAC(eth_hdr(skb)->h_dest);
99
100         printk("proto = 0x%04x", ntohs(eth_hdr(skb)->h_proto));
101
102         if (loginfo->type == NF_LOG_TYPE_LOG)
103                 bitmask = loginfo->u.log.logflags;
104         else
105                 bitmask = NF_LOG_MASK;
106
107         if ((bitmask & EBT_LOG_IP) && eth_hdr(skb)->h_proto ==
108            htons(ETH_P_IP)){
109                 const struct iphdr *ih;
110                 struct iphdr _iph;
111
112                 ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph);
113                 if (ih == NULL) {
114                         printk(" INCOMPLETE IP header");
115                         goto out;
116                 }
117                 printk(" IP SRC=%u.%u.%u.%u IP DST=%u.%u.%u.%u, IP "
118                        "tos=0x%02X, IP proto=%d", NIPQUAD(ih->saddr),
119                        NIPQUAD(ih->daddr), ih->tos, ih->protocol);
120                 print_ports(skb, ih->protocol, ih->ihl*4);
121                 goto out;
122         }
123
124 #if defined(CONFIG_BRIDGE_EBT_IP6) || defined(CONFIG_BRIDGE_EBT_IP6_MODULE)
125         if ((bitmask & EBT_LOG_IP6) && eth_hdr(skb)->h_proto ==
126            htons(ETH_P_IPV6)) {
127                 const struct ipv6hdr *ih;
128                 struct ipv6hdr _iph;
129                 uint8_t nexthdr;
130                 int offset_ph;
131
132                 ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph);
133                 if (ih == NULL) {
134                         printk(" INCOMPLETE IPv6 header");
135                         goto out;
136                 }
137                 printk(" IPv6 SRC=%x:%x:%x:%x:%x:%x:%x:%x "
138                        "IPv6 DST=%x:%x:%x:%x:%x:%x:%x:%x, IPv6 "
139                        "priority=0x%01X, Next Header=%d", NIP6(ih->saddr),
140                        NIP6(ih->daddr), ih->priority, ih->nexthdr);
141                 nexthdr = ih->nexthdr;
142                 offset_ph = ipv6_skip_exthdr(skb, sizeof(_iph), &nexthdr);
143                 if (offset_ph == -1)
144                         goto out;
145                 print_ports(skb, nexthdr, offset_ph);
146                 goto out;
147         }
148 #endif
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(NFPROTO_BRIDGE, hooknr, skb, in, out, &li,
208                               "%s", info->prefix);
209         else
210                 ebt_log_packet(NFPROTO_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         .targetsize     = XT_ALIGN(sizeof(struct ebt_log_info)),
220         .me             = THIS_MODULE,
221 };
222
223 static const struct nf_logger ebt_log_logger = {
224         .name           = "ebt_log",
225         .logfn          = &ebt_log_packet,
226         .me             = THIS_MODULE,
227 };
228
229 static int __init ebt_log_init(void)
230 {
231         int ret;
232
233         ret = ebt_register_watcher(&log);
234         if (ret < 0)
235                 return ret;
236         nf_log_register(NFPROTO_BRIDGE, &ebt_log_logger);
237         return 0;
238 }
239
240 static void __exit ebt_log_fini(void)
241 {
242         nf_log_unregister(&ebt_log_logger);
243         ebt_unregister_watcher(&log);
244 }
245
246 module_init(ebt_log_init);
247 module_exit(ebt_log_fini);
248 MODULE_DESCRIPTION("Ebtables: Packet logging to syslog");
249 MODULE_LICENSE("GPL");