Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / ipv6 / netfilter / ip6t_LOG.c
blobbfc3d0185d19bba36278299eb55d3ee7ed1b2012
1 /*
2 * This is a module which is used for logging packets.
3 */
5 /* (C) 2001 Jan Rekorajski <baggins@pld.org.pl>
6 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
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.
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>
25 MODULE_AUTHOR("Jan Rekorajski <baggins@pld.org.pl>");
26 MODULE_DESCRIPTION("IP6 tables LOG target module");
27 MODULE_LICENSE("GPL");
29 static unsigned int nflog = 1;
30 module_param(nflog, int, 0400);
31 MODULE_PARM_DESC(nflog, "register as internal netfilter logging module");
33 struct in_device;
34 #include <net/route.h>
35 #include <linux/netfilter_ipv6/ip6t_LOG.h>
37 #if 0
38 #define DEBUGP printk
39 #else
40 #define DEBUGP(format, args...)
41 #endif
43 /* Use lock to serialize, so printks don't overlap */
44 static DEFINE_SPINLOCK(log_lock);
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)
51 u_int8_t currenthdr;
52 int fragment;
53 struct ipv6hdr _ip6h, *ih;
54 unsigned int ptr;
55 unsigned int hdrlen = 0;
57 ih = skb_header_pointer(skb, ip6hoff, sizeof(_ip6h), &_ip6h);
58 if (ih == NULL) {
59 printk("TRUNCATED");
60 return;
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));
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));
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;
80 hp = skb_header_pointer(skb, ptr, sizeof(_hdr), &_hdr);
81 if (hp == NULL) {
82 printk("TRUNCATED");
83 return;
86 /* Max length: 48 "OPT (...) " */
87 if (info->logflags & IP6T_LOG_IPOPT)
88 printk("OPT ( ");
90 switch (currenthdr) {
91 case IPPROTO_FRAGMENT: {
92 struct frag_hdr _fhdr, *fh;
94 printk("FRAG:");
95 fh = skb_header_pointer(skb, ptr, sizeof(_fhdr),
96 &_fhdr);
97 if (fh == NULL) {
98 printk("TRUNCATED ");
99 return;
102 /* Max length: 6 "65535 " */
103 printk("%u ", ntohs(fh->frag_off) & 0xFFF8);
105 /* Max length: 11 "INCOMPLETE " */
106 if (fh->frag_off & htons(0x0001))
107 printk("INCOMPLETE ");
109 printk("ID:%08x ", ntohl(fh->identification));
111 if (ntohs(fh->frag_off) & 0xFFF8)
112 fragment = 1;
114 hdrlen = 8;
116 break;
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;
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;
133 /* Max length: 3 "AH " */
134 printk("AH ");
136 if (fragment) {
137 printk(")");
138 return;
141 ah = skb_header_pointer(skb, ptr, sizeof(_ahdr),
142 &_ahdr);
143 if (ah == NULL) {
145 * Max length: 26 "INCOMPLETE [65535
146 * bytes] )"
148 printk("INCOMPLETE [%u bytes] )",
149 skb->len - ptr);
150 return;
153 /* Length: 15 "SPI=0xF1234567 */
154 printk("SPI=0x%x ", ntohl(ah->spi));
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;
164 /* Max length: 4 "ESP " */
165 printk("ESP ");
167 if (fragment) {
168 printk(")");
169 return;
173 * Max length: 26 "INCOMPLETE [65535 bytes] )"
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;
183 /* Length: 16 "SPI=0xF1234567 )" */
184 printk("SPI=0x%x )", ntohl(eh->spi) );
187 return;
188 default:
189 /* Max length: 20 "Unknown Ext Hdr 255" */
190 printk("Unknown Ext Hdr %u", currenthdr);
191 return;
193 if (info->logflags & IP6T_LOG_IPOPT)
194 printk(") ");
196 currenthdr = hp->nexthdr;
197 ptr += hdrlen;
200 switch (currenthdr) {
201 case IPPROTO_TCP: {
202 struct tcphdr _tcph, *th;
204 /* Max length: 10 "PROTO=TCP " */
205 printk("PROTO=TCP ");
207 if (fragment)
208 break;
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;
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));
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);
255 op = skb_header_pointer(skb,
256 ptr + sizeof(struct tcphdr),
257 optsize, _opt);
258 if (op == NULL) {
259 printk("OPT (TRUNCATED)");
260 return;
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(") ");
269 break;
271 case IPPROTO_UDP: {
272 struct udphdr _udph, *uh;
274 /* Max length: 10 "PROTO=UDP " */
275 printk("PROTO=UDP ");
277 if (fragment)
278 break;
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;
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;
293 case IPPROTO_ICMPV6: {
294 struct icmp6hdr _icmp6h, *ic;
296 /* Max length: 13 "PROTO=ICMPv6 " */
297 printk("PROTO=ICMPv6 ");
299 if (fragment)
300 break;
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;
309 /* Max length: 18 "TYPE=255 CODE=255 " */
310 printk("TYPE=%u CODE=%u ", ic->icmp6_type, ic->icmp6_code);
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;
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),
337 printk("] ");
340 /* Max length: 10 "MTU=65535 " */
341 if (ic->icmp6_type == ICMPV6_PKT_TOOBIG)
342 printk("MTU=%u ", ntohl(ic->icmp6_mtu));
344 break;
346 /* Max length: 10 "PROTO=255 " */
347 default:
348 printk("PROTO=%u ", currenthdr);
351 /* Max length: 15 "UID=4294967295 " */
352 if ((info->logflags & IP6T_LOG_UID) && recurse && skb->sk) {
353 read_lock_bh(&skb->sk->sk_callback_lock);
354 if (skb->sk->sk_socket && skb->sk->sk_socket->file)
355 printk("UID=%u ", skb->sk->sk_socket->file->f_uid);
356 read_unlock_bh(&skb->sk->sk_callback_lock);
360 static void
361 ip6t_log_packet(unsigned int hooknum,
362 const struct sk_buff *skb,
363 const struct net_device *in,
364 const struct net_device *out,
365 const struct ip6t_log_info *loginfo,
366 const char *level_string,
367 const char *prefix)
369 struct ipv6hdr *ipv6h = skb->nh.ipv6h;
371 spin_lock_bh(&log_lock);
372 printk(level_string);
373 printk("%sIN=%s OUT=%s ",
374 prefix == NULL ? loginfo->prefix : prefix,
375 in ? in->name : "",
376 out ? out->name : "");
377 if (in && !out) {
378 /* MAC logging for input chain only. */
379 printk("MAC=");
380 if (skb->dev && skb->dev->hard_header_len && skb->mac.raw != (void*)ipv6h) {
381 if (skb->dev->type != ARPHRD_SIT){
382 int i;
383 unsigned char *p = skb->mac.raw;
384 for (i = 0; i < skb->dev->hard_header_len; i++,p++)
385 printk("%02x%c", *p,
386 i==skb->dev->hard_header_len - 1
387 ? ' ':':');
388 } else {
389 int i;
390 unsigned char *p = skb->mac.raw;
391 if ( p - (ETH_ALEN*2+2) > skb->head ){
392 p -= (ETH_ALEN+2);
393 for (i = 0; i < (ETH_ALEN); i++,p++)
394 printk("%02x%s", *p,
395 i == ETH_ALEN-1 ? "->" : ":");
396 p -= (ETH_ALEN*2);
397 for (i = 0; i < (ETH_ALEN); i++,p++)
398 printk("%02x%c", *p,
399 i == ETH_ALEN-1 ? ' ' : ':');
402 if ((skb->dev->addr_len == 4) &&
403 skb->dev->hard_header_len > 20){
404 printk("TUNNEL=");
405 p = skb->mac.raw + 12;
406 for (i = 0; i < 4; i++,p++)
407 printk("%3d%s", *p,
408 i == 3 ? "->" : ".");
409 for (i = 0; i < 4; i++,p++)
410 printk("%3d%c", *p,
411 i == 3 ? ' ' : '.');
414 } else
415 printk(" ");
418 dump_packet(loginfo, skb, (u8*)skb->nh.ipv6h - skb->data, 1);
419 printk("\n");
420 spin_unlock_bh(&log_lock);
423 static unsigned int
424 ip6t_log_target(struct sk_buff **pskb,
425 const struct net_device *in,
426 const struct net_device *out,
427 unsigned int hooknum,
428 const void *targinfo,
429 void *userinfo)
431 const struct ip6t_log_info *loginfo = targinfo;
432 char level_string[4] = "< >";
434 level_string[1] = '0' + (loginfo->level % 8);
435 ip6t_log_packet(hooknum, *pskb, in, out, loginfo, level_string, NULL);
437 return IP6T_CONTINUE;
440 static void
441 ip6t_logfn(unsigned int hooknum,
442 const struct sk_buff *skb,
443 const struct net_device *in,
444 const struct net_device *out,
445 const char *prefix)
447 struct ip6t_log_info loginfo = {
448 .level = 0,
449 .logflags = IP6T_LOG_MASK,
450 .prefix = ""
453 ip6t_log_packet(hooknum, skb, in, out, &loginfo, KERN_WARNING, prefix);
456 static int ip6t_log_checkentry(const char *tablename,
457 const struct ip6t_entry *e,
458 void *targinfo,
459 unsigned int targinfosize,
460 unsigned int hook_mask)
462 const struct ip6t_log_info *loginfo = targinfo;
464 if (targinfosize != IP6T_ALIGN(sizeof(struct ip6t_log_info))) {
465 DEBUGP("LOG: targinfosize %u != %u\n",
466 targinfosize, IP6T_ALIGN(sizeof(struct ip6t_log_info)));
467 return 0;
470 if (loginfo->level >= 8) {
471 DEBUGP("LOG: level %u >= 8\n", loginfo->level);
472 return 0;
475 if (loginfo->prefix[sizeof(loginfo->prefix)-1] != '\0') {
476 DEBUGP("LOG: prefix term %i\n",
477 loginfo->prefix[sizeof(loginfo->prefix)-1]);
478 return 0;
481 return 1;
484 static struct ip6t_target ip6t_log_reg = {
485 .name = "LOG",
486 .target = ip6t_log_target,
487 .checkentry = ip6t_log_checkentry,
488 .me = THIS_MODULE,
491 static int __init init(void)
493 if (ip6t_register_target(&ip6t_log_reg))
494 return -EINVAL;
495 if (nflog)
496 nf_log_register(PF_INET6, &ip6t_logfn);
498 return 0;
501 static void __exit fini(void)
503 if (nflog)
504 nf_log_unregister(PF_INET6, &ip6t_logfn);
505 ip6t_unregister_target(&ip6t_log_reg);
508 module_init(init);
509 module_exit(fini);