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>
26 #include <net/netfilter/nf_log.h>
28 MODULE_AUTHOR("Jan Rekorajski <baggins@pld.org.pl>");
29 MODULE_DESCRIPTION("Xtables: IPv6 packet logging to syslog");
30 MODULE_LICENSE("GPL");
33 #include <net/route.h>
34 #include <linux/netfilter_ipv6/ip6t_LOG.h>
36 /* Use lock to serialize, so printks don't overlap */
37 static DEFINE_SPINLOCK(log_lock
);
39 /* One level of recursion won't kill us */
40 static void dump_packet(const struct nf_loginfo
*info
,
41 const struct sk_buff
*skb
, unsigned int ip6hoff
,
47 const struct ipv6hdr
*ih
;
49 unsigned int hdrlen
= 0;
50 unsigned int logflags
;
52 if (info
->type
== NF_LOG_TYPE_LOG
)
53 logflags
= info
->u
.log
.logflags
;
55 logflags
= NF_LOG_MASK
;
57 ih
= skb_header_pointer(skb
, ip6hoff
, sizeof(_ip6h
), &_ip6h
);
63 /* Max length: 88 "SRC=0000.0000.0000.0000.0000.0000.0000.0000 DST=0000.0000.0000.0000.0000.0000.0000.0000 " */
64 printk("SRC=%pI6 DST=%pI6 ", &ih
->saddr
, &ih
->daddr
);
66 /* Max length: 44 "LEN=65535 TC=255 HOPLIMIT=255 FLOWLBL=FFFFF " */
67 printk("LEN=%Zu TC=%u HOPLIMIT=%u FLOWLBL=%u ",
68 ntohs(ih
->payload_len
) + sizeof(struct ipv6hdr
),
69 (ntohl(*(__be32
*)ih
) & 0x0ff00000) >> 20,
71 (ntohl(*(__be32
*)ih
) & 0x000fffff));
74 ptr
= ip6hoff
+ sizeof(struct ipv6hdr
);
75 currenthdr
= ih
->nexthdr
;
76 while (currenthdr
!= NEXTHDR_NONE
&& ip6t_ext_hdr(currenthdr
)) {
77 struct ipv6_opt_hdr _hdr
;
78 const struct ipv6_opt_hdr
*hp
;
80 hp
= skb_header_pointer(skb
, ptr
, sizeof(_hdr
), &_hdr
);
86 /* Max length: 48 "OPT (...) " */
87 if (logflags
& IP6T_LOG_IPOPT
)
91 case IPPROTO_FRAGMENT
: {
92 struct frag_hdr _fhdr
;
93 const struct frag_hdr
*fh
;
96 fh
= skb_header_pointer(skb
, ptr
, sizeof(_fhdr
),
103 /* Max length: 6 "65535 " */
104 printk("%u ", ntohs(fh
->frag_off
) & 0xFFF8);
106 /* Max length: 11 "INCOMPLETE " */
107 if (fh
->frag_off
& htons(0x0001))
108 printk("INCOMPLETE ");
110 printk("ID:%08x ", ntohl(fh
->identification
));
112 if (ntohs(fh
->frag_off
) & 0xFFF8)
119 case IPPROTO_DSTOPTS
:
120 case IPPROTO_ROUTING
:
121 case IPPROTO_HOPOPTS
:
123 if (logflags
& IP6T_LOG_IPOPT
)
127 hdrlen
= ipv6_optlen(hp
);
131 if (logflags
& IP6T_LOG_IPOPT
) {
132 struct ip_auth_hdr _ahdr
;
133 const struct ip_auth_hdr
*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
;
165 const struct ip_esp_hdr
*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
) {
206 const struct tcphdr
*th
;
208 /* Max length: 10 "PROTO=TCP " */
209 printk("PROTO=TCP ");
214 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
215 th
= skb_header_pointer(skb
, ptr
, sizeof(_tcph
), &_tcph
);
217 printk("INCOMPLETE [%u bytes] ", skb
->len
- ptr
);
221 /* Max length: 20 "SPT=65535 DPT=65535 " */
222 printk("SPT=%u DPT=%u ",
223 ntohs(th
->source
), ntohs(th
->dest
));
224 /* Max length: 30 "SEQ=4294967295 ACK=4294967295 " */
225 if (logflags
& IP6T_LOG_TCPSEQ
)
226 printk("SEQ=%u ACK=%u ",
227 ntohl(th
->seq
), ntohl(th
->ack_seq
));
228 /* Max length: 13 "WINDOW=65535 " */
229 printk("WINDOW=%u ", ntohs(th
->window
));
230 /* Max length: 9 "RES=0x3C " */
231 printk("RES=0x%02x ", (u_int8_t
)(ntohl(tcp_flag_word(th
) & TCP_RESERVED_BITS
) >> 22));
232 /* Max length: 32 "CWR ECE URG ACK PSH RST SYN FIN " */
249 /* Max length: 11 "URGP=65535 " */
250 printk("URGP=%u ", ntohs(th
->urg_ptr
));
252 if ((logflags
& IP6T_LOG_TCPOPT
)
253 && th
->doff
* 4 > sizeof(struct tcphdr
)) {
254 u_int8_t _opt
[60 - sizeof(struct tcphdr
)];
257 unsigned int optsize
= th
->doff
* 4
258 - sizeof(struct tcphdr
);
260 op
= skb_header_pointer(skb
,
261 ptr
+ sizeof(struct tcphdr
),
264 printk("OPT (TRUNCATED)");
268 /* Max length: 127 "OPT (" 15*4*2chars ") " */
270 for (i
=0; i
< optsize
; i
++)
271 printk("%02X", op
[i
]);
277 case IPPROTO_UDPLITE
: {
279 const struct udphdr
*uh
;
281 if (currenthdr
== IPPROTO_UDP
)
282 /* Max length: 10 "PROTO=UDP " */
283 printk("PROTO=UDP " );
284 else /* Max length: 14 "PROTO=UDPLITE " */
285 printk("PROTO=UDPLITE ");
290 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
291 uh
= skb_header_pointer(skb
, ptr
, sizeof(_udph
), &_udph
);
293 printk("INCOMPLETE [%u bytes] ", skb
->len
- ptr
);
297 /* Max length: 20 "SPT=65535 DPT=65535 " */
298 printk("SPT=%u DPT=%u LEN=%u ",
299 ntohs(uh
->source
), ntohs(uh
->dest
),
303 case IPPROTO_ICMPV6
: {
304 struct icmp6hdr _icmp6h
;
305 const struct icmp6hdr
*ic
;
307 /* Max length: 13 "PROTO=ICMPv6 " */
308 printk("PROTO=ICMPv6 ");
313 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
314 ic
= skb_header_pointer(skb
, ptr
, sizeof(_icmp6h
), &_icmp6h
);
316 printk("INCOMPLETE [%u bytes] ", skb
->len
- ptr
);
320 /* Max length: 18 "TYPE=255 CODE=255 " */
321 printk("TYPE=%u CODE=%u ", ic
->icmp6_type
, ic
->icmp6_code
);
323 switch (ic
->icmp6_type
) {
324 case ICMPV6_ECHO_REQUEST
:
325 case ICMPV6_ECHO_REPLY
:
326 /* Max length: 19 "ID=65535 SEQ=65535 " */
327 printk("ID=%u SEQ=%u ",
328 ntohs(ic
->icmp6_identifier
),
329 ntohs(ic
->icmp6_sequence
));
331 case ICMPV6_MGM_QUERY
:
332 case ICMPV6_MGM_REPORT
:
333 case ICMPV6_MGM_REDUCTION
:
336 case ICMPV6_PARAMPROB
:
337 /* Max length: 17 "POINTER=ffffffff " */
338 printk("POINTER=%08x ", ntohl(ic
->icmp6_pointer
));
340 case ICMPV6_DEST_UNREACH
:
341 case ICMPV6_PKT_TOOBIG
:
342 case ICMPV6_TIME_EXCEED
:
343 /* Max length: 3+maxlen */
346 dump_packet(info
, skb
, ptr
+ sizeof(_icmp6h
),
351 /* Max length: 10 "MTU=65535 " */
352 if (ic
->icmp6_type
== ICMPV6_PKT_TOOBIG
)
353 printk("MTU=%u ", ntohl(ic
->icmp6_mtu
));
357 /* Max length: 10 "PROTO=255 " */
359 printk("PROTO=%u ", currenthdr
);
362 /* Max length: 15 "UID=4294967295 " */
363 if ((logflags
& IP6T_LOG_UID
) && recurse
&& skb
->sk
) {
364 read_lock_bh(&skb
->sk
->sk_callback_lock
);
365 if (skb
->sk
->sk_socket
&& skb
->sk
->sk_socket
->file
)
366 printk("UID=%u GID=%u ",
367 skb
->sk
->sk_socket
->file
->f_cred
->fsuid
,
368 skb
->sk
->sk_socket
->file
->f_cred
->fsgid
);
369 read_unlock_bh(&skb
->sk
->sk_callback_lock
);
372 /* Max length: 16 "MARK=0xFFFFFFFF " */
373 if (!recurse
&& skb
->mark
)
374 printk("MARK=0x%x ", skb
->mark
);
377 static struct nf_loginfo default_loginfo
= {
378 .type
= NF_LOG_TYPE_LOG
,
382 .logflags
= NF_LOG_MASK
,
388 ip6t_log_packet(u_int8_t pf
,
389 unsigned int hooknum
,
390 const struct sk_buff
*skb
,
391 const struct net_device
*in
,
392 const struct net_device
*out
,
393 const struct nf_loginfo
*loginfo
,
397 loginfo
= &default_loginfo
;
399 spin_lock_bh(&log_lock
);
400 printk("<%d>%sIN=%s OUT=%s ", loginfo
->u
.log
.level
,
403 out
? out
->name
: "");
406 /* MAC logging for input chain only. */
408 if (skb
->dev
&& (len
= skb
->dev
->hard_header_len
) &&
409 skb
->mac_header
!= skb
->network_header
) {
410 const unsigned char *p
= skb_mac_header(skb
);
413 if (skb
->dev
->type
== ARPHRD_SIT
&&
414 (p
-= ETH_HLEN
) < skb
->head
)
418 for (i
= 0; i
< len
; i
++)
419 printk("%02x%s", p
[i
],
420 i
== len
- 1 ? "" : ":");
424 if (skb
->dev
->type
== ARPHRD_SIT
) {
425 const struct iphdr
*iph
=
426 (struct iphdr
*)skb_mac_header(skb
);
427 printk("TUNNEL=%pI4->%pI4 ",
428 &iph
->saddr
, &iph
->daddr
);
434 dump_packet(loginfo
, skb
, skb_network_offset(skb
), 1);
436 spin_unlock_bh(&log_lock
);
440 log_tg6(struct sk_buff
*skb
, const struct xt_target_param
*par
)
442 const struct ip6t_log_info
*loginfo
= par
->targinfo
;
443 struct nf_loginfo li
;
445 li
.type
= NF_LOG_TYPE_LOG
;
446 li
.u
.log
.level
= loginfo
->level
;
447 li
.u
.log
.logflags
= loginfo
->logflags
;
449 ip6t_log_packet(NFPROTO_IPV6
, par
->hooknum
, skb
, par
->in
, par
->out
,
450 &li
, loginfo
->prefix
);
455 static bool log_tg6_check(const struct xt_tgchk_param
*par
)
457 const struct ip6t_log_info
*loginfo
= par
->targinfo
;
459 if (loginfo
->level
>= 8) {
460 pr_debug("LOG: level %u >= 8\n", loginfo
->level
);
463 if (loginfo
->prefix
[sizeof(loginfo
->prefix
)-1] != '\0') {
464 pr_debug("LOG: prefix term %i\n",
465 loginfo
->prefix
[sizeof(loginfo
->prefix
)-1]);
471 static struct xt_target log_tg6_reg __read_mostly
= {
473 .family
= NFPROTO_IPV6
,
475 .targetsize
= sizeof(struct ip6t_log_info
),
476 .checkentry
= log_tg6_check
,
480 static struct nf_logger ip6t_logger __read_mostly
= {
482 .logfn
= &ip6t_log_packet
,
486 static int __init
log_tg6_init(void)
490 ret
= xt_register_target(&log_tg6_reg
);
493 nf_log_register(NFPROTO_IPV6
, &ip6t_logger
);
497 static void __exit
log_tg6_exit(void)
499 nf_log_unregister(&ip6t_logger
);
500 xt_unregister_target(&log_tg6_reg
);
503 module_init(log_tg6_init
);
504 module_exit(log_tg6_exit
);