2 * This is a module which is used for logging packets.
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>
18 #include <linux/spinlock.h>
19 #include <linux/icmpv6.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");
31 #include <net/route.h>
32 #include <linux/netfilter_ipv6/ip6t_LOG.h>
37 #define DEBUGP(format, args...)
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,
50 struct ipv6hdr _ip6h, *ih;
52 unsigned int hdrlen = 0;
53 unsigned int logflags;
55 if (info->type == NF_LOG_TYPE_LOG)
56 logflags = info->u.log.logflags;
58 logflags = NF_LOG_MASK;
60 ih = skb_header_pointer(skb, ip6hoff, sizeof(_ip6h), &_ip6h);
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,
75 (ntohl(*(u_int32_t *)ih) & 0x000fffff));
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);
89 /* Max length: 48 "OPT (...) " */
90 if (logflags & IP6T_LOG_IPOPT)
94 case IPPROTO_FRAGMENT: {
95 struct frag_hdr _fhdr, *fh;
98 fh = skb_header_pointer(skb, ptr, sizeof(_fhdr),
101 printk("TRUNCATED ");
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)
121 case IPPROTO_DSTOPTS:
122 case IPPROTO_ROUTING:
123 case IPPROTO_HOPOPTS:
125 if (logflags & IP6T_LOG_IPOPT)
129 hdrlen = ipv6_optlen(hp);
133 if (logflags & IP6T_LOG_IPOPT) {
134 struct ip_auth_hdr _ahdr, *ah;
136 /* Max length: 3 "AH " */
144 ah = skb_header_pointer(skb, ptr, sizeof(_ahdr),
148 * Max length: 26 "INCOMPLETE [65535
151 printk("INCOMPLETE [%u bytes] )",
156 /* Length: 15 "SPI=0xF1234567 */
157 printk("SPI=0x%x ", ntohl(ah->spi));
161 hdrlen = (hp->hdrlen+2)<<2;
164 if (logflags & IP6T_LOG_IPOPT) {
165 struct ip_esp_hdr _esph, *eh;
167 /* Max length: 4 "ESP " */
176 * Max length: 26 "INCOMPLETE [65535 bytes] )"
178 eh = skb_header_pointer(skb, ptr, sizeof(_esph),
181 printk("INCOMPLETE [%u bytes] )",
186 /* Length: 16 "SPI=0xF1234567 )" */
187 printk("SPI=0x%x )", ntohl(eh->spi) );
192 /* Max length: 20 "Unknown Ext Hdr 255" */
193 printk("Unknown Ext Hdr %u", currenthdr);
196 if (logflags & IP6T_LOG_IPOPT)
199 currenthdr = hp->nexthdr;
203 switch (currenthdr) {
205 struct tcphdr _tcph, *th;
207 /* Max length: 10 "PROTO=TCP " */
208 printk("PROTO=TCP ");
213 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
214 th = skb_header_pointer(skb, ptr, sizeof(_tcph), &_tcph);
216 printk("INCOMPLETE [%u bytes] ", skb->len - ptr);
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 " */
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;
255 unsigned int optsize = th->doff * 4
256 - sizeof(struct tcphdr);
258 op = skb_header_pointer(skb,
259 ptr + sizeof(struct tcphdr),
262 printk("OPT (TRUNCATED)");
266 /* Max length: 127 "OPT (" 15*4*2chars ") " */
268 for (i =0; i < optsize; i++)
269 printk("%02X", op[i]);
275 struct udphdr _udph, *uh;
277 /* Max length: 10 "PROTO=UDP " */
278 printk("PROTO=UDP ");
283 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
284 uh = skb_header_pointer(skb, ptr, sizeof(_udph), &_udph);
286 printk("INCOMPLETE [%u bytes] ", skb->len - ptr);
290 /* Max length: 20 "SPT=65535 DPT=65535 " */
291 printk("SPT=%u DPT=%u LEN=%u ",
292 ntohs(uh->source), ntohs(uh->dest),
296 case IPPROTO_ICMPV6: {
297 struct icmp6hdr _icmp6h, *ic;
299 /* Max length: 13 "PROTO=ICMPv6 " */
300 printk("PROTO=ICMPv6 ");
305 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
306 ic = skb_header_pointer(skb, ptr, sizeof(_icmp6h), &_icmp6h);
308 printk("INCOMPLETE [%u bytes] ", skb->len - ptr);
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));
323 case ICMPV6_MGM_QUERY:
324 case ICMPV6_MGM_REPORT:
325 case ICMPV6_MGM_REDUCTION:
328 case ICMPV6_PARAMPROB:
329 /* Max length: 17 "POINTER=ffffffff " */
330 printk("POINTER=%08x ", ntohl(ic->icmp6_pointer));
332 case ICMPV6_DEST_UNREACH:
333 case ICMPV6_PKT_TOOBIG:
334 case ICMPV6_TIME_EXCEED:
335 /* Max length: 3+maxlen */
338 dump_packet(info, skb, ptr + sizeof(_icmp6h),
343 /* Max length: 10 "MTU=65535 " */
344 if (ic->icmp6_type == ICMPV6_PKT_TOOBIG)
345 printk("MTU=%u ", ntohl(ic->icmp6_mtu));
349 /* Max length: 10 "PROTO=255 " */
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,
368 .logflags = NF_LOG_MASK,
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,
383 loginfo = &default_loginfo;
385 spin_lock_bh(&log_lock);
386 printk("<%d>%sIN=%s OUT=%s ", loginfo->u.log.level,
389 out ? out->name : "");
392 /* MAC logging for input chain only. */
394 if (skb->dev && (len = skb->dev->hard_header_len) &&
395 skb->mac.raw != skb->nh.raw) {
396 unsigned char *p = skb->mac.raw;
399 if (skb->dev->type == ARPHRD_SIT &&
400 (p -= ETH_HLEN) < skb->head)
404 for (i = 0; i < len; i++)
405 printk("%02x%s", p[i],
406 i == len - 1 ? "" : ":");
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 ",
414 NIPQUAD(iph->daddr));
420 dump_packet(loginfo, skb, (u8*)skb->nh.ipv6h - skb->data, 1);
422 spin_unlock_bh(&log_lock);
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,
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,
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)));
460 if (loginfo->level >= 8) {
461 DEBUGP("LOG: level %u >= 8\n", loginfo->level);
465 if (loginfo->prefix[sizeof(loginfo->prefix)-1] != '\0') {
466 DEBUGP("LOG: prefix term %i\n",
467 loginfo->prefix[sizeof(loginfo->prefix)-1]);
474 static struct ip6t_target ip6t_log_reg = {
476 .target = ip6t_log_target,
477 .checkentry = ip6t_log_checkentry,
481 static struct nf_logger ip6t_logger = {
483 .logfn = &ip6t_log_packet,
487 static int __init init(void)
489 if (ip6t_register_target(&ip6t_log_reg))
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 */
501 static void __exit fini(void)
503 nf_log_unregister_logger(&ip6t_logger);
504 ip6t_unregister_target(&ip6t_log_reg);