upgrade to linux 2.6.10-1.12_FC2
[linux-2.6.git] / net / ipv6 / netfilter / ip6t_LOG.c
1 /*
2  * This is a module which is used for logging packets.
3  */
4
5 /* (C) 2001 Jan Rekorajski <baggins@pld.org.pl>
6  * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
15 #include <linux/skbuff.h>
16 #include <linux/ip.h>
17 #include <linux/spinlock.h>
18 #include <linux/icmpv6.h>
19 #include <net/udp.h>
20 #include <net/tcp.h>
21 #include <net/ipv6.h>
22 #include <linux/netfilter.h>
23 #include <linux/netfilter_ipv6/ip6_tables.h>
24
25 MODULE_AUTHOR("Jan Rekorajski <baggins@pld.org.pl>");
26 MODULE_DESCRIPTION("IP6 tables LOG target module");
27 MODULE_LICENSE("GPL");
28
29 static unsigned int nflog = 1;
30 module_param(nflog, int, 0400);
31 MODULE_PARM_DESC(nflog, "register as internal netfilter logging module");
32  
33 struct in_device;
34 #include <net/route.h>
35 #include <linux/netfilter_ipv6/ip6t_LOG.h>
36
37 #if 0
38 #define DEBUGP printk
39 #else
40 #define DEBUGP(format, args...)
41 #endif
42
43 /* Use lock to serialize, so printks don't overlap */
44 static spinlock_t log_lock = SPIN_LOCK_UNLOCKED;
45
46 /* One level of recursion won't kill us */
47 static void dump_packet(const struct ip6t_log_info *info,
48                         const struct sk_buff *skb, unsigned int ip6hoff,
49                         int recurse)
50 {
51         u_int8_t currenthdr;
52         int fragment;
53         struct ipv6hdr _ip6h, *ih;
54         unsigned int ptr;
55         unsigned int hdrlen = 0;
56
57         ih = skb_header_pointer(skb, ip6hoff, sizeof(_ip6h), &_ip6h);
58         if (ih == NULL) {
59                 printk("TRUNCATED");
60                 return;
61         }
62
63         /* Max length: 88 "SRC=0000.0000.0000.0000.0000.0000.0000.0000 DST=0000.0000.0000.0000.0000.0000.0000.0000" */
64         printk("SRC=%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x ", NIP6(ih->saddr));
65         printk("DST=%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x ", NIP6(ih->daddr));
66
67         /* Max length: 44 "LEN=65535 TC=255 HOPLIMIT=255 FLOWLBL=FFFFF " */
68         printk("LEN=%Zu TC=%u HOPLIMIT=%u FLOWLBL=%u ",
69                ntohs(ih->payload_len) + sizeof(struct ipv6hdr),
70                (ntohl(*(u_int32_t *)ih) & 0x0ff00000) >> 20,
71                ih->hop_limit,
72                (ntohl(*(u_int32_t *)ih) & 0x000fffff));
73
74         fragment = 0;
75         ptr = ip6hoff + sizeof(struct ipv6hdr);
76         currenthdr = ih->nexthdr;
77         while (currenthdr != NEXTHDR_NONE && ip6t_ext_hdr(currenthdr)) {
78                 struct ipv6_opt_hdr _hdr, *hp;
79
80                 hp = skb_header_pointer(skb, ptr, sizeof(_hdr), &_hdr);
81                 if (hp == NULL) {
82                         printk("TRUNCATED");
83                         return;
84                 }
85
86                 /* Max length: 48 "OPT (...) " */
87                 if (info->logflags & IP6T_LOG_IPOPT)
88                         printk("OPT ( ");
89
90                 switch (currenthdr) {
91                 case IPPROTO_FRAGMENT: {
92                         struct frag_hdr _fhdr, *fh;
93
94                         printk("FRAG:");
95                         fh = skb_header_pointer(skb, ptr, sizeof(_fhdr),
96                                                 &_fhdr);
97                         if (fh == NULL) {
98                                 printk("TRUNCATED ");
99                                 return;
100                         }
101
102                         /* Max length: 6 "65535 " */
103                         printk("%u ", ntohs(fh->frag_off) & 0xFFF8);
104
105                         /* Max length: 11 "INCOMPLETE " */
106                         if (fh->frag_off & htons(0x0001))
107                                 printk("INCOMPLETE ");
108
109                         printk("ID:%08x ", ntohl(fh->identification));
110
111                         if (ntohs(fh->frag_off) & 0xFFF8)
112                                 fragment = 1;
113
114                         hdrlen = 8;
115
116                         break;
117                 }
118                 case IPPROTO_DSTOPTS:
119                 case IPPROTO_ROUTING:
120                 case IPPROTO_HOPOPTS:
121                         if (fragment) {
122                                 if (info->logflags & IP6T_LOG_IPOPT)
123                                         printk(")");
124                                 return;
125                         }
126                         hdrlen = ipv6_optlen(hp);
127                         break;
128                 /* Max Length */
129                 case IPPROTO_AH:
130                         if (info->logflags & IP6T_LOG_IPOPT) {
131                                 struct ip_auth_hdr _ahdr, *ah;
132
133                                 /* Max length: 3 "AH " */
134                                 printk("AH ");
135
136                                 if (fragment) {
137                                         printk(")");
138                                         return;
139                                 }
140
141                                 ah = skb_header_pointer(skb, ptr, sizeof(_ahdr),
142                                                         &_ahdr);
143                                 if (ah == NULL) {
144                                         /*
145                                          * Max length: 26 "INCOMPLETE [65535    
146                                          *  bytes] )"
147                                          */
148                                         printk("INCOMPLETE [%u bytes] )",
149                                                skb->len - ptr);
150                                         return;
151                                 }
152
153                                 /* Length: 15 "SPI=0xF1234567 */
154                                 printk("SPI=0x%x ", ntohl(ah->spi));
155
156                         }
157
158                         hdrlen = (hp->hdrlen+2)<<2;
159                         break;
160                 case IPPROTO_ESP:
161                         if (info->logflags & IP6T_LOG_IPOPT) {
162                                 struct ip_esp_hdr _esph, *eh;
163
164                                 /* Max length: 4 "ESP " */
165                                 printk("ESP ");
166
167                                 if (fragment) {
168                                         printk(")");
169                                         return;
170                                 }
171
172                                 /*
173                                  * Max length: 26 "INCOMPLETE [65535 bytes] )"
174                                  */
175                                 eh = skb_header_pointer(skb, ptr, sizeof(_esph),
176                                                         &_esph);
177                                 if (eh == NULL) {
178                                         printk("INCOMPLETE [%u bytes] )",
179                                                skb->len - ptr);
180                                         return;
181                                 }
182
183                                 /* Length: 16 "SPI=0xF1234567 )" */
184                                 printk("SPI=0x%x )", ntohl(eh->spi) );
185
186                         }
187                         return;
188                 default:
189                         /* Max length: 20 "Unknown Ext Hdr 255" */
190                         printk("Unknown Ext Hdr %u", currenthdr);
191                         return;
192                 }
193                 if (info->logflags & IP6T_LOG_IPOPT)
194                         printk(") ");
195
196                 currenthdr = hp->nexthdr;
197                 ptr += hdrlen;
198         }
199
200         switch (currenthdr) {
201         case IPPROTO_TCP: {
202                 struct tcphdr _tcph, *th;
203
204                 /* Max length: 10 "PROTO=TCP " */
205                 printk("PROTO=TCP ");
206
207                 if (fragment)
208                         break;
209
210                 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
211                 th = skb_header_pointer(skb, ptr, sizeof(_tcph), &_tcph);
212                 if (th == NULL) {
213                         printk("INCOMPLETE [%u bytes] ", skb->len - ptr);
214                         return;
215                 }
216
217                 /* Max length: 20 "SPT=65535 DPT=65535 " */
218                 printk("SPT=%u DPT=%u ",
219                        ntohs(th->source), ntohs(th->dest));
220                 /* Max length: 30 "SEQ=4294967295 ACK=4294967295 " */
221                 if (info->logflags & IP6T_LOG_TCPSEQ)
222                         printk("SEQ=%u ACK=%u ",
223                                ntohl(th->seq), ntohl(th->ack_seq));
224                 /* Max length: 13 "WINDOW=65535 " */
225                 printk("WINDOW=%u ", ntohs(th->window));
226                 /* Max length: 9 "RES=0x3C " */
227                 printk("RES=0x%02x ", (u_int8_t)(ntohl(tcp_flag_word(th) & TCP_RESERVED_BITS) >> 22));
228                 /* Max length: 32 "CWR ECE URG ACK PSH RST SYN FIN " */
229                 if (th->cwr)
230                         printk("CWR ");
231                 if (th->ece)
232                         printk("ECE ");
233                 if (th->urg)
234                         printk("URG ");
235                 if (th->ack)
236                         printk("ACK ");
237                 if (th->psh)
238                         printk("PSH ");
239                 if (th->rst)
240                         printk("RST ");
241                 if (th->syn)
242                         printk("SYN ");
243                 if (th->fin)
244                         printk("FIN ");
245                 /* Max length: 11 "URGP=65535 " */
246                 printk("URGP=%u ", ntohs(th->urg_ptr));
247
248                 if ((info->logflags & IP6T_LOG_TCPOPT)
249                     && th->doff * 4 > sizeof(struct tcphdr)) {
250                         u_int8_t _opt[60 - sizeof(struct tcphdr)], *op;
251                         unsigned int i;
252                         unsigned int optsize = th->doff * 4
253                                                - sizeof(struct tcphdr);
254
255                         op = skb_header_pointer(skb,
256                                                 ptr + sizeof(struct tcphdr),
257                                                 optsize, _opt);
258                         if (op == NULL) {
259                                 printk("OPT (TRUNCATED)");
260                                 return;
261                         }
262
263                         /* Max length: 127 "OPT (" 15*4*2chars ") " */
264                         printk("OPT (");
265                         for (i =0; i < optsize; i++)
266                                 printk("%02X", op[i]);
267                         printk(") ");
268                 }
269                 break;
270         }
271         case IPPROTO_UDP: {
272                 struct udphdr _udph, *uh;
273
274                 /* Max length: 10 "PROTO=UDP " */
275                 printk("PROTO=UDP ");
276
277                 if (fragment)
278                         break;
279
280                 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
281                 uh = skb_header_pointer(skb, ptr, sizeof(_udph), &_udph);
282                 if (uh == NULL) {
283                         printk("INCOMPLETE [%u bytes] ", skb->len - ptr);
284                         return;
285                 }
286
287                 /* Max length: 20 "SPT=65535 DPT=65535 " */
288                 printk("SPT=%u DPT=%u LEN=%u ",
289                        ntohs(uh->source), ntohs(uh->dest),
290                        ntohs(uh->len));
291                 break;
292         }
293         case IPPROTO_ICMPV6: {
294                 struct icmp6hdr _icmp6h, *ic;
295
296                 /* Max length: 13 "PROTO=ICMPv6 " */
297                 printk("PROTO=ICMPv6 ");
298
299                 if (fragment)
300                         break;
301
302                 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
303                 ic = skb_header_pointer(skb, ptr, sizeof(_icmp6h), &_icmp6h);
304                 if (ic == NULL) {
305                         printk("INCOMPLETE [%u bytes] ", skb->len - ptr);
306                         return;
307                 }
308
309                 /* Max length: 18 "TYPE=255 CODE=255 " */
310                 printk("TYPE=%u CODE=%u ", ic->icmp6_type, ic->icmp6_code);
311
312                 switch (ic->icmp6_type) {
313                 case ICMPV6_ECHO_REQUEST:
314                 case ICMPV6_ECHO_REPLY:
315                         /* Max length: 19 "ID=65535 SEQ=65535 " */
316                         printk("ID=%u SEQ=%u ",
317                                 ntohs(ic->icmp6_identifier),
318                                 ntohs(ic->icmp6_sequence));
319                         break;
320                 case ICMPV6_MGM_QUERY:
321                 case ICMPV6_MGM_REPORT:
322                 case ICMPV6_MGM_REDUCTION:
323                         break;
324
325                 case ICMPV6_PARAMPROB:
326                         /* Max length: 17 "POINTER=ffffffff " */
327                         printk("POINTER=%08x ", ntohl(ic->icmp6_pointer));
328                         /* Fall through */
329                 case ICMPV6_DEST_UNREACH:
330                 case ICMPV6_PKT_TOOBIG:
331                 case ICMPV6_TIME_EXCEED:
332                         /* Max length: 3+maxlen */
333                         if (recurse) {
334                                 printk("[");
335                                 dump_packet(info, skb, ptr + sizeof(_icmp6h),
336                                             0);
337                                 printk("] ");
338                         }
339
340                         /* Max length: 10 "MTU=65535 " */
341                         if (ic->icmp6_type == ICMPV6_PKT_TOOBIG)
342                                 printk("MTU=%u ", ntohl(ic->icmp6_mtu));
343                 }
344                 break;
345         }
346         /* Max length: 10 "PROTO=255 " */
347         default:
348                 printk("PROTO=%u ", currenthdr);
349         }
350 }
351
352 static void
353 ip6t_log_packet(unsigned int hooknum,
354                 const struct sk_buff *skb,
355                 const struct net_device *in,
356                 const struct net_device *out,
357                 const struct ip6t_log_info *loginfo,
358                 const char *level_string,
359                 const char *prefix)
360 {
361         struct ipv6hdr *ipv6h = skb->nh.ipv6h;
362
363         spin_lock_bh(&log_lock);
364         printk(level_string);
365         printk("%sIN=%s OUT=%s ",
366                 prefix == NULL ? loginfo->prefix : prefix,
367                 in ? in->name : "",
368                 out ? out->name : "");
369         if (in && !out) {
370                 /* MAC logging for input chain only. */
371                 printk("MAC=");
372                 if (skb->dev && skb->dev->hard_header_len && skb->mac.raw != (void*)ipv6h) {
373                         if (skb->dev->type != ARPHRD_SIT){
374                           int i;
375                           unsigned char *p = skb->mac.raw;
376                           for (i = 0; i < skb->dev->hard_header_len; i++,p++)
377                                 printk("%02x%c", *p,
378                                         i==skb->dev->hard_header_len - 1
379                                         ? ' ':':');
380                         } else {
381                           int i;
382                           unsigned char *p = skb->mac.raw;
383                           if ( p - (ETH_ALEN*2+2) > skb->head ){
384                             p -= (ETH_ALEN+2);
385                             for (i = 0; i < (ETH_ALEN); i++,p++)
386                                 printk("%02x%s", *p,
387                                         i == ETH_ALEN-1 ? "->" : ":");
388                             p -= (ETH_ALEN*2);
389                             for (i = 0; i < (ETH_ALEN); i++,p++)
390                                 printk("%02x%c", *p,
391                                         i == ETH_ALEN-1 ? ' ' : ':');
392                           }
393                           
394                           if ((skb->dev->addr_len == 4) &&
395                               skb->dev->hard_header_len > 20){
396                             printk("TUNNEL=");
397                             p = skb->mac.raw + 12;
398                             for (i = 0; i < 4; i++,p++)
399                                 printk("%3d%s", *p,
400                                         i == 3 ? "->" : ".");
401                             for (i = 0; i < 4; i++,p++)
402                                 printk("%3d%c", *p,
403                                         i == 3 ? ' ' : '.');
404                           }
405                         }
406                 } else
407                         printk(" ");
408         }
409
410         dump_packet(loginfo, skb, (u8*)skb->nh.ipv6h - skb->data, 1);
411         printk("\n");
412         spin_unlock_bh(&log_lock);
413 }
414
415 static unsigned int
416 ip6t_log_target(struct sk_buff **pskb,
417                 const struct net_device *in,
418                 const struct net_device *out,
419                 unsigned int hooknum,
420                 const void *targinfo,
421                 void *userinfo)
422 {
423         const struct ip6t_log_info *loginfo = targinfo;
424         char level_string[4] = "< >";
425
426         level_string[1] = '0' + (loginfo->level % 8);
427         ip6t_log_packet(hooknum, *pskb, in, out, loginfo, level_string, NULL);
428
429         return IP6T_CONTINUE;
430 }
431
432 static void
433 ip6t_logfn(unsigned int hooknum,
434            const struct sk_buff *skb,
435            const struct net_device *in,
436            const struct net_device *out,
437            const char *prefix)
438 {
439         struct ip6t_log_info loginfo = {
440                 .level = 0,
441                 .logflags = IP6T_LOG_MASK,
442                 .prefix = ""
443         };
444
445         ip6t_log_packet(hooknum, skb, in, out, &loginfo, KERN_WARNING, prefix);
446 }
447
448 static int ip6t_log_checkentry(const char *tablename,
449                                const struct ip6t_entry *e,
450                                void *targinfo,
451                                unsigned int targinfosize,
452                                unsigned int hook_mask)
453 {
454         const struct ip6t_log_info *loginfo = targinfo;
455
456         if (targinfosize != IP6T_ALIGN(sizeof(struct ip6t_log_info))) {
457                 DEBUGP("LOG: targinfosize %u != %u\n",
458                        targinfosize, IP6T_ALIGN(sizeof(struct ip6t_log_info)));
459                 return 0;
460         }
461
462         if (loginfo->level >= 8) {
463                 DEBUGP("LOG: level %u >= 8\n", loginfo->level);
464                 return 0;
465         }
466
467         if (loginfo->prefix[sizeof(loginfo->prefix)-1] != '\0') {
468                 DEBUGP("LOG: prefix term %i\n",
469                        loginfo->prefix[sizeof(loginfo->prefix)-1]);
470                 return 0;
471         }
472
473         return 1;
474 }
475
476 static struct ip6t_target ip6t_log_reg = {
477         .name           = "LOG",
478         .target         = ip6t_log_target, 
479         .checkentry     = ip6t_log_checkentry, 
480         .me             = THIS_MODULE,
481 };
482
483 static int __init init(void)
484 {
485         if (ip6t_register_target(&ip6t_log_reg))
486                 return -EINVAL;
487         if (nflog)
488                 nf_log_register(PF_INET6, &ip6t_logfn);
489
490         return 0;
491 }
492
493 static void __exit fini(void)
494 {
495         if (nflog)
496                 nf_log_unregister(PF_INET6, &ip6t_logfn);
497         ip6t_unregister_target(&ip6t_log_reg);
498 }
499
500 module_init(init);
501 module_exit(fini);