[INET_SOCK]: Move struct inet_sock & helper functions to net/inet_sock.h
[linux-2.6/kvm.git] / net / ipv6 / netfilter / ip6t_LOG.c
blobae4653bfd65462fe31340bf6cf46324d8cf93385
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=%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x ", NIP6(ih->saddr));
68 printk("DST=%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x ", NIP6(ih->daddr));
70 /* Max length: 44 "LEN=65535 TC=255 HOPLIMIT=255 FLOWLBL=FFFFF " */
71 printk("LEN=%Zu TC=%u HOPLIMIT=%u FLOWLBL=%u ",
72 ntohs(ih->payload_len) + sizeof(struct ipv6hdr),
73 (ntohl(*(u_int32_t *)ih) & 0x0ff00000) >> 20,
74 ih->hop_limit,
75 (ntohl(*(u_int32_t *)ih) & 0x000fffff));
77 fragment = 0;
78 ptr = ip6hoff + sizeof(struct ipv6hdr);
79 currenthdr = ih->nexthdr;
80 while (currenthdr != NEXTHDR_NONE && ip6t_ext_hdr(currenthdr)) {
81 struct ipv6_opt_hdr _hdr, *hp;
83 hp = skb_header_pointer(skb, ptr, sizeof(_hdr), &_hdr);
84 if (hp == NULL) {
85 printk("TRUNCATED");
86 return;
89 /* Max length: 48 "OPT (...) " */
90 if (logflags & IP6T_LOG_IPOPT)
91 printk("OPT ( ");
93 switch (currenthdr) {
94 case IPPROTO_FRAGMENT: {
95 struct frag_hdr _fhdr, *fh;
97 printk("FRAG:");
98 fh = skb_header_pointer(skb, ptr, sizeof(_fhdr),
99 &_fhdr);
100 if (fh == NULL) {
101 printk("TRUNCATED ");
102 return;
105 /* Max length: 6 "65535 " */
106 printk("%u ", ntohs(fh->frag_off) & 0xFFF8);
108 /* Max length: 11 "INCOMPLETE " */
109 if (fh->frag_off & htons(0x0001))
110 printk("INCOMPLETE ");
112 printk("ID:%08x ", ntohl(fh->identification));
114 if (ntohs(fh->frag_off) & 0xFFF8)
115 fragment = 1;
117 hdrlen = 8;
119 break;
121 case IPPROTO_DSTOPTS:
122 case IPPROTO_ROUTING:
123 case IPPROTO_HOPOPTS:
124 if (fragment) {
125 if (logflags & IP6T_LOG_IPOPT)
126 printk(")");
127 return;
129 hdrlen = ipv6_optlen(hp);
130 break;
131 /* Max Length */
132 case IPPROTO_AH:
133 if (logflags & IP6T_LOG_IPOPT) {
134 struct ip_auth_hdr _ahdr, *ah;
136 /* Max length: 3 "AH " */
137 printk("AH ");
139 if (fragment) {
140 printk(")");
141 return;
144 ah = skb_header_pointer(skb, ptr, sizeof(_ahdr),
145 &_ahdr);
146 if (ah == NULL) {
148 * Max length: 26 "INCOMPLETE [65535
149 * bytes] )"
151 printk("INCOMPLETE [%u bytes] )",
152 skb->len - ptr);
153 return;
156 /* Length: 15 "SPI=0xF1234567 */
157 printk("SPI=0x%x ", ntohl(ah->spi));
161 hdrlen = (hp->hdrlen+2)<<2;
162 break;
163 case IPPROTO_ESP:
164 if (logflags & IP6T_LOG_IPOPT) {
165 struct ip_esp_hdr _esph, *eh;
167 /* Max length: 4 "ESP " */
168 printk("ESP ");
170 if (fragment) {
171 printk(")");
172 return;
176 * Max length: 26 "INCOMPLETE [65535 bytes] )"
178 eh = skb_header_pointer(skb, ptr, sizeof(_esph),
179 &_esph);
180 if (eh == NULL) {
181 printk("INCOMPLETE [%u bytes] )",
182 skb->len - ptr);
183 return;
186 /* Length: 16 "SPI=0xF1234567 )" */
187 printk("SPI=0x%x )", ntohl(eh->spi) );
190 return;
191 default:
192 /* Max length: 20 "Unknown Ext Hdr 255" */
193 printk("Unknown Ext Hdr %u", currenthdr);
194 return;
196 if (logflags & IP6T_LOG_IPOPT)
197 printk(") ");
199 currenthdr = hp->nexthdr;
200 ptr += hdrlen;
203 switch (currenthdr) {
204 case IPPROTO_TCP: {
205 struct tcphdr _tcph, *th;
207 /* Max length: 10 "PROTO=TCP " */
208 printk("PROTO=TCP ");
210 if (fragment)
211 break;
213 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
214 th = skb_header_pointer(skb, ptr, sizeof(_tcph), &_tcph);
215 if (th == NULL) {
216 printk("INCOMPLETE [%u bytes] ", skb->len - ptr);
217 return;
220 /* Max length: 20 "SPT=65535 DPT=65535 " */
221 printk("SPT=%u DPT=%u ",
222 ntohs(th->source), ntohs(th->dest));
223 /* Max length: 30 "SEQ=4294967295 ACK=4294967295 " */
224 if (logflags & IP6T_LOG_TCPSEQ)
225 printk("SEQ=%u ACK=%u ",
226 ntohl(th->seq), ntohl(th->ack_seq));
227 /* Max length: 13 "WINDOW=65535 " */
228 printk("WINDOW=%u ", ntohs(th->window));
229 /* Max length: 9 "RES=0x3C " */
230 printk("RES=0x%02x ", (u_int8_t)(ntohl(tcp_flag_word(th) & TCP_RESERVED_BITS) >> 22));
231 /* Max length: 32 "CWR ECE URG ACK PSH RST SYN FIN " */
232 if (th->cwr)
233 printk("CWR ");
234 if (th->ece)
235 printk("ECE ");
236 if (th->urg)
237 printk("URG ");
238 if (th->ack)
239 printk("ACK ");
240 if (th->psh)
241 printk("PSH ");
242 if (th->rst)
243 printk("RST ");
244 if (th->syn)
245 printk("SYN ");
246 if (th->fin)
247 printk("FIN ");
248 /* Max length: 11 "URGP=65535 " */
249 printk("URGP=%u ", ntohs(th->urg_ptr));
251 if ((logflags & IP6T_LOG_TCPOPT)
252 && th->doff * 4 > sizeof(struct tcphdr)) {
253 u_int8_t _opt[60 - sizeof(struct tcphdr)], *op;
254 unsigned int i;
255 unsigned int optsize = th->doff * 4
256 - sizeof(struct tcphdr);
258 op = skb_header_pointer(skb,
259 ptr + sizeof(struct tcphdr),
260 optsize, _opt);
261 if (op == NULL) {
262 printk("OPT (TRUNCATED)");
263 return;
266 /* Max length: 127 "OPT (" 15*4*2chars ") " */
267 printk("OPT (");
268 for (i =0; i < optsize; i++)
269 printk("%02X", op[i]);
270 printk(") ");
272 break;
274 case IPPROTO_UDP: {
275 struct udphdr _udph, *uh;
277 /* Max length: 10 "PROTO=UDP " */
278 printk("PROTO=UDP ");
280 if (fragment)
281 break;
283 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
284 uh = skb_header_pointer(skb, ptr, sizeof(_udph), &_udph);
285 if (uh == NULL) {
286 printk("INCOMPLETE [%u bytes] ", skb->len - ptr);
287 return;
290 /* Max length: 20 "SPT=65535 DPT=65535 " */
291 printk("SPT=%u DPT=%u LEN=%u ",
292 ntohs(uh->source), ntohs(uh->dest),
293 ntohs(uh->len));
294 break;
296 case IPPROTO_ICMPV6: {
297 struct icmp6hdr _icmp6h, *ic;
299 /* Max length: 13 "PROTO=ICMPv6 " */
300 printk("PROTO=ICMPv6 ");
302 if (fragment)
303 break;
305 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
306 ic = skb_header_pointer(skb, ptr, sizeof(_icmp6h), &_icmp6h);
307 if (ic == NULL) {
308 printk("INCOMPLETE [%u bytes] ", skb->len - ptr);
309 return;
312 /* Max length: 18 "TYPE=255 CODE=255 " */
313 printk("TYPE=%u CODE=%u ", ic->icmp6_type, ic->icmp6_code);
315 switch (ic->icmp6_type) {
316 case ICMPV6_ECHO_REQUEST:
317 case ICMPV6_ECHO_REPLY:
318 /* Max length: 19 "ID=65535 SEQ=65535 " */
319 printk("ID=%u SEQ=%u ",
320 ntohs(ic->icmp6_identifier),
321 ntohs(ic->icmp6_sequence));
322 break;
323 case ICMPV6_MGM_QUERY:
324 case ICMPV6_MGM_REPORT:
325 case ICMPV6_MGM_REDUCTION:
326 break;
328 case ICMPV6_PARAMPROB:
329 /* Max length: 17 "POINTER=ffffffff " */
330 printk("POINTER=%08x ", ntohl(ic->icmp6_pointer));
331 /* Fall through */
332 case ICMPV6_DEST_UNREACH:
333 case ICMPV6_PKT_TOOBIG:
334 case ICMPV6_TIME_EXCEED:
335 /* Max length: 3+maxlen */
336 if (recurse) {
337 printk("[");
338 dump_packet(info, skb, ptr + sizeof(_icmp6h),
340 printk("] ");
343 /* Max length: 10 "MTU=65535 " */
344 if (ic->icmp6_type == ICMPV6_PKT_TOOBIG)
345 printk("MTU=%u ", ntohl(ic->icmp6_mtu));
347 break;
349 /* Max length: 10 "PROTO=255 " */
350 default:
351 printk("PROTO=%u ", currenthdr);
354 /* Max length: 15 "UID=4294967295 " */
355 if ((logflags & IP6T_LOG_UID) && recurse && skb->sk) {
356 read_lock_bh(&skb->sk->sk_callback_lock);
357 if (skb->sk->sk_socket && skb->sk->sk_socket->file)
358 printk("UID=%u ", skb->sk->sk_socket->file->f_uid);
359 read_unlock_bh(&skb->sk->sk_callback_lock);
363 static struct nf_loginfo default_loginfo = {
364 .type = NF_LOG_TYPE_LOG,
365 .u = {
366 .log = {
367 .level = 0,
368 .logflags = NF_LOG_MASK,
373 static void
374 ip6t_log_packet(unsigned int pf,
375 unsigned int hooknum,
376 const struct sk_buff *skb,
377 const struct net_device *in,
378 const struct net_device *out,
379 const struct nf_loginfo *loginfo,
380 const char *prefix)
382 if (!loginfo)
383 loginfo = &default_loginfo;
385 spin_lock_bh(&log_lock);
386 printk("<%d>%sIN=%s OUT=%s ", loginfo->u.log.level,
387 prefix,
388 in ? in->name : "",
389 out ? out->name : "");
390 if (in && !out) {
391 unsigned int len;
392 /* MAC logging for input chain only. */
393 printk("MAC=");
394 if (skb->dev && (len = skb->dev->hard_header_len) &&
395 skb->mac.raw != skb->nh.raw) {
396 unsigned char *p = skb->mac.raw;
397 int i;
399 if (skb->dev->type == ARPHRD_SIT &&
400 (p -= ETH_HLEN) < skb->head)
401 p = NULL;
403 if (p != NULL) {
404 for (i = 0; i < len; i++)
405 printk("%02x%s", p[i],
406 i == len - 1 ? "" : ":");
408 printk(" ");
410 if (skb->dev->type == ARPHRD_SIT) {
411 struct iphdr *iph = (struct iphdr *)skb->mac.raw;
412 printk("TUNNEL=%u.%u.%u.%u->%u.%u.%u.%u ",
413 NIPQUAD(iph->saddr),
414 NIPQUAD(iph->daddr));
416 } else
417 printk(" ");
420 dump_packet(loginfo, skb, (u8*)skb->nh.ipv6h - skb->data, 1);
421 printk("\n");
422 spin_unlock_bh(&log_lock);
425 static unsigned int
426 ip6t_log_target(struct sk_buff **pskb,
427 const struct net_device *in,
428 const struct net_device *out,
429 unsigned int hooknum,
430 const void *targinfo,
431 void *userinfo)
433 const struct ip6t_log_info *loginfo = targinfo;
434 struct nf_loginfo li;
436 li.type = NF_LOG_TYPE_LOG;
437 li.u.log.level = loginfo->level;
438 li.u.log.logflags = loginfo->logflags;
440 nf_log_packet(PF_INET6, hooknum, *pskb, in, out, &li, loginfo->prefix);
442 return IP6T_CONTINUE;
446 static int ip6t_log_checkentry(const char *tablename,
447 const struct ip6t_entry *e,
448 void *targinfo,
449 unsigned int targinfosize,
450 unsigned int hook_mask)
452 const struct ip6t_log_info *loginfo = targinfo;
454 if (targinfosize != IP6T_ALIGN(sizeof(struct ip6t_log_info))) {
455 DEBUGP("LOG: targinfosize %u != %u\n",
456 targinfosize, IP6T_ALIGN(sizeof(struct ip6t_log_info)));
457 return 0;
460 if (loginfo->level >= 8) {
461 DEBUGP("LOG: level %u >= 8\n", loginfo->level);
462 return 0;
465 if (loginfo->prefix[sizeof(loginfo->prefix)-1] != '\0') {
466 DEBUGP("LOG: prefix term %i\n",
467 loginfo->prefix[sizeof(loginfo->prefix)-1]);
468 return 0;
471 return 1;
474 static struct ip6t_target ip6t_log_reg = {
475 .name = "LOG",
476 .target = ip6t_log_target,
477 .checkentry = ip6t_log_checkentry,
478 .me = THIS_MODULE,
481 static struct nf_logger ip6t_logger = {
482 .name = "ip6t_LOG",
483 .logfn = &ip6t_log_packet,
484 .me = THIS_MODULE,
487 static int __init init(void)
489 if (ip6t_register_target(&ip6t_log_reg))
490 return -EINVAL;
491 if (nf_log_register(PF_INET6, &ip6t_logger) < 0) {
492 printk(KERN_WARNING "ip6t_LOG: not logging via system console "
493 "since somebody else already registered for PF_INET6\n");
494 /* we cannot make module load fail here, since otherwise
495 * ip6tables userspace would abort */
498 return 0;
501 static void __exit fini(void)
503 nf_log_unregister_logger(&ip6t_logger);
504 ip6t_unregister_target(&ip6t_log_reg);
507 module_init(init);
508 module_exit(fini);