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=" 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(*(u_int32_t
*)ih
) & 0x0ff00000) >> 20,
74 (ntohl(*(u_int32_t
*)ih
) & 0x000fffff));
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
);
88 /* Max length: 48 "OPT (...) " */
89 if (logflags
& IP6T_LOG_IPOPT
)
93 case IPPROTO_FRAGMENT
: {
94 struct frag_hdr _fhdr
, *fh
;
97 fh
= skb_header_pointer(skb
, ptr
, sizeof(_fhdr
),
100 printk("TRUNCATED ");
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)
120 case IPPROTO_DSTOPTS
:
121 case IPPROTO_ROUTING
:
122 case IPPROTO_HOPOPTS
:
124 if (logflags
& IP6T_LOG_IPOPT
)
128 hdrlen
= ipv6_optlen(hp
);
132 if (logflags
& IP6T_LOG_IPOPT
) {
133 struct ip_auth_hdr _ahdr
, *ah
;
135 /* Max length: 3 "AH " */
143 ah
= skb_header_pointer(skb
, ptr
, sizeof(_ahdr
),
147 * Max length: 26 "INCOMPLETE [65535
150 printk("INCOMPLETE [%u bytes] )",
155 /* Length: 15 "SPI=0xF1234567 */
156 printk("SPI=0x%x ", ntohl(ah
->spi
));
160 hdrlen
= (hp
->hdrlen
+2)<<2;
163 if (logflags
& IP6T_LOG_IPOPT
) {
164 struct ip_esp_hdr _esph
, *eh
;
166 /* Max length: 4 "ESP " */
175 * Max length: 26 "INCOMPLETE [65535 bytes] )"
177 eh
= skb_header_pointer(skb
, ptr
, sizeof(_esph
),
180 printk("INCOMPLETE [%u bytes] )",
185 /* Length: 16 "SPI=0xF1234567 )" */
186 printk("SPI=0x%x )", ntohl(eh
->spi
) );
191 /* Max length: 20 "Unknown Ext Hdr 255" */
192 printk("Unknown Ext Hdr %u", currenthdr
);
195 if (logflags
& IP6T_LOG_IPOPT
)
198 currenthdr
= hp
->nexthdr
;
202 switch (currenthdr
) {
204 struct tcphdr _tcph
, *th
;
206 /* Max length: 10 "PROTO=TCP " */
207 printk("PROTO=TCP ");
212 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
213 th
= skb_header_pointer(skb
, ptr
, sizeof(_tcph
), &_tcph
);
215 printk("INCOMPLETE [%u bytes] ", skb
->len
- ptr
);
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 " */
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
;
254 unsigned int optsize
= th
->doff
* 4
255 - sizeof(struct tcphdr
);
257 op
= skb_header_pointer(skb
,
258 ptr
+ sizeof(struct tcphdr
),
261 printk("OPT (TRUNCATED)");
265 /* Max length: 127 "OPT (" 15*4*2chars ") " */
267 for (i
=0; i
< optsize
; i
++)
268 printk("%02X", op
[i
]);
274 struct udphdr _udph
, *uh
;
276 /* Max length: 10 "PROTO=UDP " */
277 printk("PROTO=UDP ");
282 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
283 uh
= skb_header_pointer(skb
, ptr
, sizeof(_udph
), &_udph
);
285 printk("INCOMPLETE [%u bytes] ", skb
->len
- ptr
);
289 /* Max length: 20 "SPT=65535 DPT=65535 " */
290 printk("SPT=%u DPT=%u LEN=%u ",
291 ntohs(uh
->source
), ntohs(uh
->dest
),
295 case IPPROTO_ICMPV6
: {
296 struct icmp6hdr _icmp6h
, *ic
;
298 /* Max length: 13 "PROTO=ICMPv6 " */
299 printk("PROTO=ICMPv6 ");
304 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
305 ic
= skb_header_pointer(skb
, ptr
, sizeof(_icmp6h
), &_icmp6h
);
307 printk("INCOMPLETE [%u bytes] ", skb
->len
- ptr
);
311 /* Max length: 18 "TYPE=255 CODE=255 " */
312 printk("TYPE=%u CODE=%u ", ic
->icmp6_type
, ic
->icmp6_code
);
314 switch (ic
->icmp6_type
) {
315 case ICMPV6_ECHO_REQUEST
:
316 case ICMPV6_ECHO_REPLY
:
317 /* Max length: 19 "ID=65535 SEQ=65535 " */
318 printk("ID=%u SEQ=%u ",
319 ntohs(ic
->icmp6_identifier
),
320 ntohs(ic
->icmp6_sequence
));
322 case ICMPV6_MGM_QUERY
:
323 case ICMPV6_MGM_REPORT
:
324 case ICMPV6_MGM_REDUCTION
:
327 case ICMPV6_PARAMPROB
:
328 /* Max length: 17 "POINTER=ffffffff " */
329 printk("POINTER=%08x ", ntohl(ic
->icmp6_pointer
));
331 case ICMPV6_DEST_UNREACH
:
332 case ICMPV6_PKT_TOOBIG
:
333 case ICMPV6_TIME_EXCEED
:
334 /* Max length: 3+maxlen */
337 dump_packet(info
, skb
, ptr
+ sizeof(_icmp6h
),
342 /* Max length: 10 "MTU=65535 " */
343 if (ic
->icmp6_type
== ICMPV6_PKT_TOOBIG
)
344 printk("MTU=%u ", ntohl(ic
->icmp6_mtu
));
348 /* Max length: 10 "PROTO=255 " */
350 printk("PROTO=%u ", currenthdr
);
353 /* Max length: 15 "UID=4294967295 " */
354 if ((logflags
& IP6T_LOG_UID
) && recurse
&& skb
->sk
) {
355 read_lock_bh(&skb
->sk
->sk_callback_lock
);
356 if (skb
->sk
->sk_socket
&& skb
->sk
->sk_socket
->file
)
357 printk("UID=%u ", skb
->sk
->sk_socket
->file
->f_uid
);
358 read_unlock_bh(&skb
->sk
->sk_callback_lock
);
362 static struct nf_loginfo default_loginfo
= {
363 .type
= NF_LOG_TYPE_LOG
,
367 .logflags
= NF_LOG_MASK
,
373 ip6t_log_packet(unsigned int pf
,
374 unsigned int hooknum
,
375 const struct sk_buff
*skb
,
376 const struct net_device
*in
,
377 const struct net_device
*out
,
378 const struct nf_loginfo
*loginfo
,
382 loginfo
= &default_loginfo
;
384 spin_lock_bh(&log_lock
);
385 printk("<%d>%sIN=%s OUT=%s ", loginfo
->u
.log
.level
,
388 out
? out
->name
: "");
391 /* MAC logging for input chain only. */
393 if (skb
->dev
&& (len
= skb
->dev
->hard_header_len
) &&
394 skb
->mac
.raw
!= skb
->nh
.raw
) {
395 unsigned char *p
= skb
->mac
.raw
;
398 if (skb
->dev
->type
== ARPHRD_SIT
&&
399 (p
-= ETH_HLEN
) < skb
->head
)
403 for (i
= 0; i
< len
; i
++)
404 printk("%02x%s", p
[i
],
405 i
== len
- 1 ? "" : ":");
409 if (skb
->dev
->type
== ARPHRD_SIT
) {
410 struct iphdr
*iph
= (struct iphdr
*)skb
->mac
.raw
;
411 printk("TUNNEL=%u.%u.%u.%u->%u.%u.%u.%u ",
413 NIPQUAD(iph
->daddr
));
419 dump_packet(loginfo
, skb
, (u8
*)skb
->nh
.ipv6h
- skb
->data
, 1);
421 spin_unlock_bh(&log_lock
);
425 ip6t_log_target(struct sk_buff
**pskb
,
426 const struct net_device
*in
,
427 const struct net_device
*out
,
428 unsigned int hooknum
,
429 const struct xt_target
*target
,
430 const void *targinfo
)
432 const struct ip6t_log_info
*loginfo
= targinfo
;
433 struct nf_loginfo li
;
435 li
.type
= NF_LOG_TYPE_LOG
;
436 li
.u
.log
.level
= loginfo
->level
;
437 li
.u
.log
.logflags
= loginfo
->logflags
;
439 if (loginfo
->logflags
& IP6T_LOG_NFLOG
)
440 nf_log_packet(PF_INET6
, hooknum
, *pskb
, in
, out
, &li
,
441 "%s", loginfo
->prefix
);
443 ip6t_log_packet(PF_INET6
, hooknum
, *pskb
, in
, out
, &li
,
446 return IP6T_CONTINUE
;
450 static int ip6t_log_checkentry(const char *tablename
,
452 const struct xt_target
*target
,
454 unsigned int hook_mask
)
456 const struct ip6t_log_info
*loginfo
= targinfo
;
458 if (loginfo
->level
>= 8) {
459 DEBUGP("LOG: level %u >= 8\n", loginfo
->level
);
462 if (loginfo
->prefix
[sizeof(loginfo
->prefix
)-1] != '\0') {
463 DEBUGP("LOG: prefix term %i\n",
464 loginfo
->prefix
[sizeof(loginfo
->prefix
)-1]);
470 static struct ip6t_target ip6t_log_reg
= {
472 .target
= ip6t_log_target
,
473 .targetsize
= sizeof(struct ip6t_log_info
),
474 .checkentry
= ip6t_log_checkentry
,
478 static struct nf_logger ip6t_logger
= {
480 .logfn
= &ip6t_log_packet
,
484 static int __init
ip6t_log_init(void)
486 if (ip6t_register_target(&ip6t_log_reg
))
488 if (nf_log_register(PF_INET6
, &ip6t_logger
) < 0) {
489 printk(KERN_WARNING
"ip6t_LOG: not logging via system console "
490 "since somebody else already registered for PF_INET6\n");
491 /* we cannot make module load fail here, since otherwise
492 * ip6tables userspace would abort */
498 static void __exit
ip6t_log_fini(void)
500 nf_log_unregister_logger(&ip6t_logger
);
501 ip6t_unregister_target(&ip6t_log_reg
);
504 module_init(ip6t_log_init
);
505 module_exit(ip6t_log_fini
);