[NETFILTER]: x_tables: fix return values for LOG/ULOG
[linux-2.6/cjktty.git] / net / ipv6 / netfilter / ip6t_LOG.c
blob9fe0816bb21d0b10ede09fc66243922c13ad91a6
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/if_arp.h>
17 #include <linux/ip.h>
18 #include <linux/spinlock.h>
19 #include <linux/icmpv6.h>
20 #include <net/udp.h>
21 #include <net/tcp.h>
22 #include <net/ipv6.h>
23 #include <linux/netfilter.h>
24 #include <linux/netfilter_ipv6/ip6_tables.h>
26 MODULE_AUTHOR("Jan Rekorajski <baggins@pld.org.pl>");
27 MODULE_DESCRIPTION("IP6 tables LOG target module");
28 MODULE_LICENSE("GPL");
30 struct in_device;
31 #include <net/route.h>
32 #include <linux/netfilter_ipv6/ip6t_LOG.h>
34 #if 0
35 #define DEBUGP printk
36 #else
37 #define DEBUGP(format, args...)
38 #endif
40 /* Use lock to serialize, so printks don't overlap */
41 static DEFINE_SPINLOCK(log_lock);
43 /* One level of recursion won't kill us */
44 static void dump_packet(const struct nf_loginfo *info,
45 const struct sk_buff *skb, unsigned int ip6hoff,
46 int recurse)
48 u_int8_t currenthdr;
49 int fragment;
50 struct ipv6hdr _ip6h, *ih;
51 unsigned int ptr;
52 unsigned int hdrlen = 0;
53 unsigned int logflags;
55 if (info->type == NF_LOG_TYPE_LOG)
56 logflags = info->u.log.logflags;
57 else
58 logflags = NF_LOG_MASK;
60 ih = skb_header_pointer(skb, ip6hoff, sizeof(_ip6h), &_ip6h);
61 if (ih == NULL) {
62 printk("TRUNCATED");
63 return;
66 /* Max length: 88 "SRC=0000.0000.0000.0000.0000.0000.0000.0000 DST=0000.0000.0000.0000.0000.0000.0000.0000 " */
67 printk("SRC=" NIP6_FMT " DST=" NIP6_FMT " ", NIP6(ih->saddr), NIP6(ih->daddr));
69 /* Max length: 44 "LEN=65535 TC=255 HOPLIMIT=255 FLOWLBL=FFFFF " */
70 printk("LEN=%Zu TC=%u HOPLIMIT=%u FLOWLBL=%u ",
71 ntohs(ih->payload_len) + sizeof(struct ipv6hdr),
72 (ntohl(*(__be32 *)ih) & 0x0ff00000) >> 20,
73 ih->hop_limit,
74 (ntohl(*(__be32 *)ih) & 0x000fffff));
76 fragment = 0;
77 ptr = ip6hoff + sizeof(struct ipv6hdr);
78 currenthdr = ih->nexthdr;
79 while (currenthdr != NEXTHDR_NONE && ip6t_ext_hdr(currenthdr)) {
80 struct ipv6_opt_hdr _hdr, *hp;
82 hp = skb_header_pointer(skb, ptr, sizeof(_hdr), &_hdr);
83 if (hp == NULL) {
84 printk("TRUNCATED");
85 return;
88 /* Max length: 48 "OPT (...) " */
89 if (logflags & IP6T_LOG_IPOPT)
90 printk("OPT ( ");
92 switch (currenthdr) {
93 case IPPROTO_FRAGMENT: {
94 struct frag_hdr _fhdr, *fh;
96 printk("FRAG:");
97 fh = skb_header_pointer(skb, ptr, sizeof(_fhdr),
98 &_fhdr);
99 if (fh == NULL) {
100 printk("TRUNCATED ");
101 return;
104 /* Max length: 6 "65535 " */
105 printk("%u ", ntohs(fh->frag_off) & 0xFFF8);
107 /* Max length: 11 "INCOMPLETE " */
108 if (fh->frag_off & htons(0x0001))
109 printk("INCOMPLETE ");
111 printk("ID:%08x ", ntohl(fh->identification));
113 if (ntohs(fh->frag_off) & 0xFFF8)
114 fragment = 1;
116 hdrlen = 8;
118 break;
120 case IPPROTO_DSTOPTS:
121 case IPPROTO_ROUTING:
122 case IPPROTO_HOPOPTS:
123 if (fragment) {
124 if (logflags & IP6T_LOG_IPOPT)
125 printk(")");
126 return;
128 hdrlen = ipv6_optlen(hp);
129 break;
130 /* Max Length */
131 case IPPROTO_AH:
132 if (logflags & IP6T_LOG_IPOPT) {
133 struct ip_auth_hdr _ahdr, *ah;
135 /* Max length: 3 "AH " */
136 printk("AH ");
138 if (fragment) {
139 printk(")");
140 return;
143 ah = skb_header_pointer(skb, ptr, sizeof(_ahdr),
144 &_ahdr);
145 if (ah == NULL) {
147 * Max length: 26 "INCOMPLETE [65535
148 * bytes] )"
150 printk("INCOMPLETE [%u bytes] )",
151 skb->len - ptr);
152 return;
155 /* Length: 15 "SPI=0xF1234567 */
156 printk("SPI=0x%x ", ntohl(ah->spi));
160 hdrlen = (hp->hdrlen+2)<<2;
161 break;
162 case IPPROTO_ESP:
163 if (logflags & IP6T_LOG_IPOPT) {
164 struct ip_esp_hdr _esph, *eh;
166 /* Max length: 4 "ESP " */
167 printk("ESP ");
169 if (fragment) {
170 printk(")");
171 return;
175 * Max length: 26 "INCOMPLETE [65535 bytes] )"
177 eh = skb_header_pointer(skb, ptr, sizeof(_esph),
178 &_esph);
179 if (eh == NULL) {
180 printk("INCOMPLETE [%u bytes] )",
181 skb->len - ptr);
182 return;
185 /* Length: 16 "SPI=0xF1234567 )" */
186 printk("SPI=0x%x )", ntohl(eh->spi) );
189 return;
190 default:
191 /* Max length: 20 "Unknown Ext Hdr 255" */
192 printk("Unknown Ext Hdr %u", currenthdr);
193 return;
195 if (logflags & IP6T_LOG_IPOPT)
196 printk(") ");
198 currenthdr = hp->nexthdr;
199 ptr += hdrlen;
202 switch (currenthdr) {
203 case IPPROTO_TCP: {
204 struct tcphdr _tcph, *th;
206 /* Max length: 10 "PROTO=TCP " */
207 printk("PROTO=TCP ");
209 if (fragment)
210 break;
212 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
213 th = skb_header_pointer(skb, ptr, sizeof(_tcph), &_tcph);
214 if (th == NULL) {
215 printk("INCOMPLETE [%u bytes] ", skb->len - ptr);
216 return;
219 /* Max length: 20 "SPT=65535 DPT=65535 " */
220 printk("SPT=%u DPT=%u ",
221 ntohs(th->source), ntohs(th->dest));
222 /* Max length: 30 "SEQ=4294967295 ACK=4294967295 " */
223 if (logflags & IP6T_LOG_TCPSEQ)
224 printk("SEQ=%u ACK=%u ",
225 ntohl(th->seq), ntohl(th->ack_seq));
226 /* Max length: 13 "WINDOW=65535 " */
227 printk("WINDOW=%u ", ntohs(th->window));
228 /* Max length: 9 "RES=0x3C " */
229 printk("RES=0x%02x ", (u_int8_t)(ntohl(tcp_flag_word(th) & TCP_RESERVED_BITS) >> 22));
230 /* Max length: 32 "CWR ECE URG ACK PSH RST SYN FIN " */
231 if (th->cwr)
232 printk("CWR ");
233 if (th->ece)
234 printk("ECE ");
235 if (th->urg)
236 printk("URG ");
237 if (th->ack)
238 printk("ACK ");
239 if (th->psh)
240 printk("PSH ");
241 if (th->rst)
242 printk("RST ");
243 if (th->syn)
244 printk("SYN ");
245 if (th->fin)
246 printk("FIN ");
247 /* Max length: 11 "URGP=65535 " */
248 printk("URGP=%u ", ntohs(th->urg_ptr));
250 if ((logflags & IP6T_LOG_TCPOPT)
251 && th->doff * 4 > sizeof(struct tcphdr)) {
252 u_int8_t _opt[60 - sizeof(struct tcphdr)], *op;
253 unsigned int i;
254 unsigned int optsize = th->doff * 4
255 - sizeof(struct tcphdr);
257 op = skb_header_pointer(skb,
258 ptr + sizeof(struct tcphdr),
259 optsize, _opt);
260 if (op == NULL) {
261 printk("OPT (TRUNCATED)");
262 return;
265 /* Max length: 127 "OPT (" 15*4*2chars ") " */
266 printk("OPT (");
267 for (i =0; i < optsize; i++)
268 printk("%02X", op[i]);
269 printk(") ");
271 break;
273 case IPPROTO_UDP:
274 case IPPROTO_UDPLITE: {
275 struct udphdr _udph, *uh;
277 if (currenthdr == IPPROTO_UDP)
278 /* Max length: 10 "PROTO=UDP " */
279 printk("PROTO=UDP " );
280 else /* Max length: 14 "PROTO=UDPLITE " */
281 printk("PROTO=UDPLITE ");
283 if (fragment)
284 break;
286 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
287 uh = skb_header_pointer(skb, ptr, sizeof(_udph), &_udph);
288 if (uh == NULL) {
289 printk("INCOMPLETE [%u bytes] ", skb->len - ptr);
290 return;
293 /* Max length: 20 "SPT=65535 DPT=65535 " */
294 printk("SPT=%u DPT=%u LEN=%u ",
295 ntohs(uh->source), ntohs(uh->dest),
296 ntohs(uh->len));
297 break;
299 case IPPROTO_ICMPV6: {
300 struct icmp6hdr _icmp6h, *ic;
302 /* Max length: 13 "PROTO=ICMPv6 " */
303 printk("PROTO=ICMPv6 ");
305 if (fragment)
306 break;
308 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
309 ic = skb_header_pointer(skb, ptr, sizeof(_icmp6h), &_icmp6h);
310 if (ic == NULL) {
311 printk("INCOMPLETE [%u bytes] ", skb->len - ptr);
312 return;
315 /* Max length: 18 "TYPE=255 CODE=255 " */
316 printk("TYPE=%u CODE=%u ", ic->icmp6_type, ic->icmp6_code);
318 switch (ic->icmp6_type) {
319 case ICMPV6_ECHO_REQUEST:
320 case ICMPV6_ECHO_REPLY:
321 /* Max length: 19 "ID=65535 SEQ=65535 " */
322 printk("ID=%u SEQ=%u ",
323 ntohs(ic->icmp6_identifier),
324 ntohs(ic->icmp6_sequence));
325 break;
326 case ICMPV6_MGM_QUERY:
327 case ICMPV6_MGM_REPORT:
328 case ICMPV6_MGM_REDUCTION:
329 break;
331 case ICMPV6_PARAMPROB:
332 /* Max length: 17 "POINTER=ffffffff " */
333 printk("POINTER=%08x ", ntohl(ic->icmp6_pointer));
334 /* Fall through */
335 case ICMPV6_DEST_UNREACH:
336 case ICMPV6_PKT_TOOBIG:
337 case ICMPV6_TIME_EXCEED:
338 /* Max length: 3+maxlen */
339 if (recurse) {
340 printk("[");
341 dump_packet(info, skb, ptr + sizeof(_icmp6h),
343 printk("] ");
346 /* Max length: 10 "MTU=65535 " */
347 if (ic->icmp6_type == ICMPV6_PKT_TOOBIG)
348 printk("MTU=%u ", ntohl(ic->icmp6_mtu));
350 break;
352 /* Max length: 10 "PROTO=255 " */
353 default:
354 printk("PROTO=%u ", currenthdr);
357 /* Max length: 15 "UID=4294967295 " */
358 if ((logflags & IP6T_LOG_UID) && recurse && skb->sk) {
359 read_lock_bh(&skb->sk->sk_callback_lock);
360 if (skb->sk->sk_socket && skb->sk->sk_socket->file)
361 printk("UID=%u ", skb->sk->sk_socket->file->f_uid);
362 read_unlock_bh(&skb->sk->sk_callback_lock);
366 static struct nf_loginfo default_loginfo = {
367 .type = NF_LOG_TYPE_LOG,
368 .u = {
369 .log = {
370 .level = 0,
371 .logflags = NF_LOG_MASK,
376 static void
377 ip6t_log_packet(unsigned int pf,
378 unsigned int hooknum,
379 const struct sk_buff *skb,
380 const struct net_device *in,
381 const struct net_device *out,
382 const struct nf_loginfo *loginfo,
383 const char *prefix)
385 if (!loginfo)
386 loginfo = &default_loginfo;
388 spin_lock_bh(&log_lock);
389 printk("<%d>%sIN=%s OUT=%s ", loginfo->u.log.level,
390 prefix,
391 in ? in->name : "",
392 out ? out->name : "");
393 if (in && !out) {
394 unsigned int len;
395 /* MAC logging for input chain only. */
396 printk("MAC=");
397 if (skb->dev && (len = skb->dev->hard_header_len) &&
398 skb->mac.raw != skb->nh.raw) {
399 unsigned char *p = skb->mac.raw;
400 int i;
402 if (skb->dev->type == ARPHRD_SIT &&
403 (p -= ETH_HLEN) < skb->head)
404 p = NULL;
406 if (p != NULL) {
407 for (i = 0; i < len; i++)
408 printk("%02x%s", p[i],
409 i == len - 1 ? "" : ":");
411 printk(" ");
413 if (skb->dev->type == ARPHRD_SIT) {
414 struct iphdr *iph = (struct iphdr *)skb->mac.raw;
415 printk("TUNNEL=%u.%u.%u.%u->%u.%u.%u.%u ",
416 NIPQUAD(iph->saddr),
417 NIPQUAD(iph->daddr));
419 } else
420 printk(" ");
423 dump_packet(loginfo, skb, (u8*)skb->nh.ipv6h - skb->data, 1);
424 printk("\n");
425 spin_unlock_bh(&log_lock);
428 static unsigned int
429 ip6t_log_target(struct sk_buff **pskb,
430 const struct net_device *in,
431 const struct net_device *out,
432 unsigned int hooknum,
433 const struct xt_target *target,
434 const void *targinfo)
436 const struct ip6t_log_info *loginfo = targinfo;
437 struct nf_loginfo li;
439 li.type = NF_LOG_TYPE_LOG;
440 li.u.log.level = loginfo->level;
441 li.u.log.logflags = loginfo->logflags;
443 ip6t_log_packet(PF_INET6, hooknum, *pskb, in, out, &li,
444 loginfo->prefix);
445 return IP6T_CONTINUE;
449 static int ip6t_log_checkentry(const char *tablename,
450 const void *entry,
451 const struct xt_target *target,
452 void *targinfo,
453 unsigned int hook_mask)
455 const struct ip6t_log_info *loginfo = targinfo;
457 if (loginfo->level >= 8) {
458 DEBUGP("LOG: level %u >= 8\n", loginfo->level);
459 return 0;
461 if (loginfo->prefix[sizeof(loginfo->prefix)-1] != '\0') {
462 DEBUGP("LOG: prefix term %i\n",
463 loginfo->prefix[sizeof(loginfo->prefix)-1]);
464 return 0;
466 return 1;
469 static struct ip6t_target ip6t_log_reg = {
470 .name = "LOG",
471 .target = ip6t_log_target,
472 .targetsize = sizeof(struct ip6t_log_info),
473 .checkentry = ip6t_log_checkentry,
474 .me = THIS_MODULE,
477 static struct nf_logger ip6t_logger = {
478 .name = "ip6t_LOG",
479 .logfn = &ip6t_log_packet,
480 .me = THIS_MODULE,
483 static int __init ip6t_log_init(void)
485 int ret;
487 ret = ip6t_register_target(&ip6t_log_reg);
488 if (ret < 0)
489 return ret;
490 if (nf_log_register(PF_INET6, &ip6t_logger) < 0) {
491 printk(KERN_WARNING "ip6t_LOG: not logging via system console "
492 "since somebody else already registered for PF_INET6\n");
493 /* we cannot make module load fail here, since otherwise
494 * ip6tables userspace would abort */
497 return 0;
500 static void __exit ip6t_log_fini(void)
502 nf_log_unregister_logger(&ip6t_logger);
503 ip6t_unregister_target(&ip6t_log_reg);
506 module_init(ip6t_log_init);
507 module_exit(ip6t_log_fini);