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/x_tables.h>
25 #include <linux/netfilter_ipv6/ip6_tables.h>
27 MODULE_AUTHOR("Jan Rekorajski <baggins@pld.org.pl>");
28 MODULE_DESCRIPTION("IP6 tables LOG target module");
29 MODULE_LICENSE("GPL");
32 #include <net/route.h>
33 #include <linux/netfilter_ipv6/ip6t_LOG.h>
38 #define DEBUGP(format, args...)
41 /* Use lock to serialize, so printks don't overlap */
42 static DEFINE_SPINLOCK(log_lock
);
44 /* One level of recursion won't kill us */
45 static void dump_packet(const struct nf_loginfo
*info
,
46 const struct sk_buff
*skb
, unsigned int ip6hoff
,
51 struct ipv6hdr _ip6h
, *ih
;
53 unsigned int hdrlen
= 0;
54 unsigned int logflags
;
56 if (info
->type
== NF_LOG_TYPE_LOG
)
57 logflags
= info
->u
.log
.logflags
;
59 logflags
= NF_LOG_MASK
;
61 ih
= skb_header_pointer(skb
, ip6hoff
, sizeof(_ip6h
), &_ip6h
);
67 /* Max length: 88 "SRC=0000.0000.0000.0000.0000.0000.0000.0000 DST=0000.0000.0000.0000.0000.0000.0000.0000 " */
68 printk("SRC=" NIP6_FMT
" DST=" NIP6_FMT
" ", NIP6(ih
->saddr
), 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(*(__be32
*)ih
) & 0x0ff00000) >> 20,
75 (ntohl(*(__be32
*)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 case IPPROTO_UDPLITE
: {
276 struct udphdr _udph
, *uh
;
278 if (currenthdr
== IPPROTO_UDP
)
279 /* Max length: 10 "PROTO=UDP " */
280 printk("PROTO=UDP " );
281 else /* Max length: 14 "PROTO=UDPLITE " */
282 printk("PROTO=UDPLITE ");
287 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
288 uh
= skb_header_pointer(skb
, ptr
, sizeof(_udph
), &_udph
);
290 printk("INCOMPLETE [%u bytes] ", skb
->len
- ptr
);
294 /* Max length: 20 "SPT=65535 DPT=65535 " */
295 printk("SPT=%u DPT=%u LEN=%u ",
296 ntohs(uh
->source
), ntohs(uh
->dest
),
300 case IPPROTO_ICMPV6
: {
301 struct icmp6hdr _icmp6h
, *ic
;
303 /* Max length: 13 "PROTO=ICMPv6 " */
304 printk("PROTO=ICMPv6 ");
309 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
310 ic
= skb_header_pointer(skb
, ptr
, sizeof(_icmp6h
), &_icmp6h
);
312 printk("INCOMPLETE [%u bytes] ", skb
->len
- ptr
);
316 /* Max length: 18 "TYPE=255 CODE=255 " */
317 printk("TYPE=%u CODE=%u ", ic
->icmp6_type
, ic
->icmp6_code
);
319 switch (ic
->icmp6_type
) {
320 case ICMPV6_ECHO_REQUEST
:
321 case ICMPV6_ECHO_REPLY
:
322 /* Max length: 19 "ID=65535 SEQ=65535 " */
323 printk("ID=%u SEQ=%u ",
324 ntohs(ic
->icmp6_identifier
),
325 ntohs(ic
->icmp6_sequence
));
327 case ICMPV6_MGM_QUERY
:
328 case ICMPV6_MGM_REPORT
:
329 case ICMPV6_MGM_REDUCTION
:
332 case ICMPV6_PARAMPROB
:
333 /* Max length: 17 "POINTER=ffffffff " */
334 printk("POINTER=%08x ", ntohl(ic
->icmp6_pointer
));
336 case ICMPV6_DEST_UNREACH
:
337 case ICMPV6_PKT_TOOBIG
:
338 case ICMPV6_TIME_EXCEED
:
339 /* Max length: 3+maxlen */
342 dump_packet(info
, skb
, ptr
+ sizeof(_icmp6h
),
347 /* Max length: 10 "MTU=65535 " */
348 if (ic
->icmp6_type
== ICMPV6_PKT_TOOBIG
)
349 printk("MTU=%u ", ntohl(ic
->icmp6_mtu
));
353 /* Max length: 10 "PROTO=255 " */
355 printk("PROTO=%u ", currenthdr
);
358 /* Max length: 15 "UID=4294967295 " */
359 if ((logflags
& IP6T_LOG_UID
) && recurse
&& skb
->sk
) {
360 read_lock_bh(&skb
->sk
->sk_callback_lock
);
361 if (skb
->sk
->sk_socket
&& skb
->sk
->sk_socket
->file
)
362 printk("UID=%u ", skb
->sk
->sk_socket
->file
->f_uid
);
363 read_unlock_bh(&skb
->sk
->sk_callback_lock
);
367 static struct nf_loginfo default_loginfo
= {
368 .type
= NF_LOG_TYPE_LOG
,
372 .logflags
= NF_LOG_MASK
,
378 ip6t_log_packet(unsigned int pf
,
379 unsigned int hooknum
,
380 const struct sk_buff
*skb
,
381 const struct net_device
*in
,
382 const struct net_device
*out
,
383 const struct nf_loginfo
*loginfo
,
387 loginfo
= &default_loginfo
;
389 spin_lock_bh(&log_lock
);
390 printk("<%d>%sIN=%s OUT=%s ", loginfo
->u
.log
.level
,
393 out
? out
->name
: "");
396 /* MAC logging for input chain only. */
398 if (skb
->dev
&& (len
= skb
->dev
->hard_header_len
) &&
399 skb
->mac
.raw
!= skb
->nh
.raw
) {
400 unsigned char *p
= skb
->mac
.raw
;
403 if (skb
->dev
->type
== ARPHRD_SIT
&&
404 (p
-= ETH_HLEN
) < skb
->head
)
408 for (i
= 0; i
< len
; i
++)
409 printk("%02x%s", p
[i
],
410 i
== len
- 1 ? "" : ":");
414 if (skb
->dev
->type
== ARPHRD_SIT
) {
415 struct iphdr
*iph
= (struct iphdr
*)skb
->mac
.raw
;
416 printk("TUNNEL=%u.%u.%u.%u->%u.%u.%u.%u ",
418 NIPQUAD(iph
->daddr
));
424 dump_packet(loginfo
, skb
, (u8
*)skb
->nh
.ipv6h
- skb
->data
, 1);
426 spin_unlock_bh(&log_lock
);
430 ip6t_log_target(struct sk_buff
**pskb
,
431 const struct net_device
*in
,
432 const struct net_device
*out
,
433 unsigned int hooknum
,
434 const struct xt_target
*target
,
435 const void *targinfo
)
437 const struct ip6t_log_info
*loginfo
= targinfo
;
438 struct nf_loginfo li
;
440 li
.type
= NF_LOG_TYPE_LOG
;
441 li
.u
.log
.level
= loginfo
->level
;
442 li
.u
.log
.logflags
= loginfo
->logflags
;
444 ip6t_log_packet(PF_INET6
, hooknum
, *pskb
, in
, out
, &li
,
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 xt_target ip6t_log_reg
= {
473 .target
= ip6t_log_target
,
474 .targetsize
= sizeof(struct ip6t_log_info
),
475 .checkentry
= ip6t_log_checkentry
,
479 static struct nf_logger ip6t_logger
= {
481 .logfn
= &ip6t_log_packet
,
485 static int __init
ip6t_log_init(void)
489 ret
= xt_register_target(&ip6t_log_reg
);
492 if (nf_log_register(PF_INET6
, &ip6t_logger
) < 0) {
493 printk(KERN_WARNING
"ip6t_LOG: not logging via system console "
494 "since somebody else already registered for PF_INET6\n");
495 /* we cannot make module load fail here, since otherwise
496 * ip6tables userspace would abort */
502 static void __exit
ip6t_log_fini(void)
504 nf_log_unregister(&ip6t_logger
);
505 xt_unregister_target(&ip6t_log_reg
);
508 module_init(ip6t_log_init
);
509 module_exit(ip6t_log_fini
);