netfilter: ebtables: fix one wrong return value
[safe/jmp/linux-2.6] / 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 bool
28 ebt_log_tg_check(const char *table, const void *entry,
29                  const struct xt_target *target, void *data,
30                  unsigned int hook_mask)
31 {
32         struct ebt_log_info *info = data;
33
34         if (info->bitmask & ~EBT_LOG_MASK)
35                 return false;
36         if (info->loglevel >= 8)
37                 return false;
38         info->prefix[EBT_LOG_PREFIX_SIZE - 1] = '\0';
39         return true;
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(u_int8_t 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 defined(CONFIG_BRIDGE_EBT_IP6) || defined(CONFIG_BRIDGE_EBT_IP6_MODULE)
127         if ((bitmask & EBT_LOG_IP6) && eth_hdr(skb)->h_proto ==
128            htons(ETH_P_IPV6)) {
129                 const struct ipv6hdr *ih;
130                 struct ipv6hdr _iph;
131                 uint8_t nexthdr;
132                 int offset_ph;
133
134                 ih = skb_header_pointer(skb, 0, sizeof(_iph), &_iph);
135                 if (ih == NULL) {
136                         printk(" INCOMPLETE IPv6 header");
137                         goto out;
138                 }
139                 printk(" IPv6 SRC=%x:%x:%x:%x:%x:%x:%x:%x "
140                        "IPv6 DST=%x:%x:%x:%x:%x:%x:%x:%x, IPv6 "
141                        "priority=0x%01X, Next Header=%d", NIP6(ih->saddr),
142                        NIP6(ih->daddr), ih->priority, ih->nexthdr);
143                 nexthdr = ih->nexthdr;
144                 offset_ph = ipv6_skip_exthdr(skb, sizeof(_iph), &nexthdr);
145                 if (offset_ph == -1)
146                         goto out;
147                 print_ports(skb, nexthdr, offset_ph);
148                 goto out;
149         }
150 #endif
151
152         if ((bitmask & EBT_LOG_ARP) &&
153             ((eth_hdr(skb)->h_proto == htons(ETH_P_ARP)) ||
154              (eth_hdr(skb)->h_proto == htons(ETH_P_RARP)))) {
155                 const struct arphdr *ah;
156                 struct arphdr _arph;
157
158                 ah = skb_header_pointer(skb, 0, sizeof(_arph), &_arph);
159                 if (ah == NULL) {
160                         printk(" INCOMPLETE ARP header");
161                         goto out;
162                 }
163                 printk(" ARP HTYPE=%d, PTYPE=0x%04x, OPCODE=%d",
164                        ntohs(ah->ar_hrd), ntohs(ah->ar_pro),
165                        ntohs(ah->ar_op));
166
167                 /* If it's for Ethernet and the lengths are OK,
168                  * then log the ARP payload */
169                 if (ah->ar_hrd == htons(1) &&
170                     ah->ar_hln == ETH_ALEN &&
171                     ah->ar_pln == sizeof(__be32)) {
172                         const struct arppayload *ap;
173                         struct arppayload _arpp;
174
175                         ap = skb_header_pointer(skb, sizeof(_arph),
176                                                 sizeof(_arpp), &_arpp);
177                         if (ap == NULL) {
178                                 printk(" INCOMPLETE ARP payload");
179                                 goto out;
180                         }
181                         printk(" ARP MAC SRC=");
182                         print_MAC(ap->mac_src);
183                         printk(" ARP IP SRC=%u.%u.%u.%u",
184                                myNIPQUAD(ap->ip_src));
185                         printk(" ARP MAC DST=");
186                         print_MAC(ap->mac_dst);
187                         printk(" ARP IP DST=%u.%u.%u.%u",
188                                myNIPQUAD(ap->ip_dst));
189                 }
190         }
191 out:
192         printk("\n");
193         spin_unlock_bh(&ebt_log_lock);
194
195 }
196
197 static unsigned int
198 ebt_log_tg(struct sk_buff *skb, const struct net_device *in,
199            const struct net_device *out, unsigned int hooknr,
200            const struct xt_target *target, const void *data)
201 {
202         const struct ebt_log_info *info = data;
203         struct nf_loginfo li;
204
205         li.type = NF_LOG_TYPE_LOG;
206         li.u.log.level = info->loglevel;
207         li.u.log.logflags = info->bitmask;
208
209         if (info->bitmask & EBT_LOG_NFLOG)
210                 nf_log_packet(NFPROTO_BRIDGE, hooknr, skb, in, out, &li,
211                               "%s", info->prefix);
212         else
213                 ebt_log_packet(NFPROTO_BRIDGE, hooknr, skb, in, out, &li,
214                                info->prefix);
215         return EBT_CONTINUE;
216 }
217
218 static struct xt_target ebt_log_tg_reg __read_mostly = {
219         .name           = "log",
220         .revision       = 0,
221         .family         = NFPROTO_BRIDGE,
222         .target         = ebt_log_tg,
223         .checkentry     = ebt_log_tg_check,
224         .targetsize     = XT_ALIGN(sizeof(struct ebt_log_info)),
225         .me             = THIS_MODULE,
226 };
227
228 static const struct nf_logger ebt_log_logger = {
229         .name           = "ebt_log",
230         .logfn          = &ebt_log_packet,
231         .me             = THIS_MODULE,
232 };
233
234 static int __init ebt_log_init(void)
235 {
236         int ret;
237
238         ret = xt_register_target(&ebt_log_tg_reg);
239         if (ret < 0)
240                 return ret;
241         nf_log_register(NFPROTO_BRIDGE, &ebt_log_logger);
242         return 0;
243 }
244
245 static void __exit ebt_log_fini(void)
246 {
247         nf_log_unregister(&ebt_log_logger);
248         xt_unregister_target(&ebt_log_tg_reg);
249 }
250
251 module_init(ebt_log_init);
252 module_exit(ebt_log_fini);
253 MODULE_DESCRIPTION("Ebtables: Packet logging to syslog");
254 MODULE_LICENSE("GPL");