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.
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/module.h>
14 #include <linux/skbuff.h>
15 #include <linux/if_arp.h>
17 #include <linux/spinlock.h>
18 #include <linux/icmpv6.h>
22 #include <linux/netfilter.h>
23 #include <linux/netfilter/x_tables.h>
24 #include <linux/netfilter_ipv6/ip6_tables.h>
25 #include <net/netfilter/nf_log.h>
26 #include <net/netfilter/xt_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 /* One level of recursion won't kill us */
37 static void dump_packet(struct sbuff
*m
,
38 const struct nf_loginfo
*info
,
39 const struct sk_buff
*skb
, unsigned int ip6hoff
,
45 const struct ipv6hdr
*ih
;
47 unsigned int hdrlen
= 0;
48 unsigned int logflags
;
50 if (info
->type
== NF_LOG_TYPE_LOG
)
51 logflags
= info
->u
.log
.logflags
;
53 logflags
= NF_LOG_MASK
;
55 ih
= skb_header_pointer(skb
, ip6hoff
, sizeof(_ip6h
), &_ip6h
);
57 sb_add(m
, "TRUNCATED");
61 /* Max length: 88 "SRC=0000.0000.0000.0000.0000.0000.0000.0000 DST=0000.0000.0000.0000.0000.0000.0000.0000 " */
62 sb_add(m
, "SRC=%pI6 DST=%pI6 ", &ih
->saddr
, &ih
->daddr
);
64 /* Max length: 44 "LEN=65535 TC=255 HOPLIMIT=255 FLOWLBL=FFFFF " */
65 sb_add(m
, "LEN=%Zu TC=%u HOPLIMIT=%u FLOWLBL=%u ",
66 ntohs(ih
->payload_len
) + sizeof(struct ipv6hdr
),
67 (ntohl(*(__be32
*)ih
) & 0x0ff00000) >> 20,
69 (ntohl(*(__be32
*)ih
) & 0x000fffff));
72 ptr
= ip6hoff
+ sizeof(struct ipv6hdr
);
73 currenthdr
= ih
->nexthdr
;
74 while (currenthdr
!= NEXTHDR_NONE
&& ip6t_ext_hdr(currenthdr
)) {
75 struct ipv6_opt_hdr _hdr
;
76 const struct ipv6_opt_hdr
*hp
;
78 hp
= skb_header_pointer(skb
, ptr
, sizeof(_hdr
), &_hdr
);
80 sb_add(m
, "TRUNCATED");
84 /* Max length: 48 "OPT (...) " */
85 if (logflags
& IP6T_LOG_IPOPT
)
89 case IPPROTO_FRAGMENT
: {
90 struct frag_hdr _fhdr
;
91 const struct frag_hdr
*fh
;
94 fh
= skb_header_pointer(skb
, ptr
, sizeof(_fhdr
),
97 sb_add(m
, "TRUNCATED ");
101 /* Max length: 6 "65535 " */
102 sb_add(m
, "%u ", ntohs(fh
->frag_off
) & 0xFFF8);
104 /* Max length: 11 "INCOMPLETE " */
105 if (fh
->frag_off
& htons(0x0001))
106 sb_add(m
, "INCOMPLETE ");
108 sb_add(m
, "ID:%08x ", ntohl(fh
->identification
));
110 if (ntohs(fh
->frag_off
) & 0xFFF8)
117 case IPPROTO_DSTOPTS
:
118 case IPPROTO_ROUTING
:
119 case IPPROTO_HOPOPTS
:
121 if (logflags
& IP6T_LOG_IPOPT
)
125 hdrlen
= ipv6_optlen(hp
);
129 if (logflags
& IP6T_LOG_IPOPT
) {
130 struct ip_auth_hdr _ahdr
;
131 const struct ip_auth_hdr
*ah
;
133 /* Max length: 3 "AH " */
141 ah
= skb_header_pointer(skb
, ptr
, sizeof(_ahdr
),
145 * Max length: 26 "INCOMPLETE [65535
148 sb_add(m
, "INCOMPLETE [%u bytes] )",
153 /* Length: 15 "SPI=0xF1234567 */
154 sb_add(m
, "SPI=0x%x ", ntohl(ah
->spi
));
158 hdrlen
= (hp
->hdrlen
+2)<<2;
161 if (logflags
& IP6T_LOG_IPOPT
) {
162 struct ip_esp_hdr _esph
;
163 const struct ip_esp_hdr
*eh
;
165 /* Max length: 4 "ESP " */
174 * Max length: 26 "INCOMPLETE [65535 bytes] )"
176 eh
= skb_header_pointer(skb
, ptr
, sizeof(_esph
),
179 sb_add(m
, "INCOMPLETE [%u bytes] )",
184 /* Length: 16 "SPI=0xF1234567 )" */
185 sb_add(m
, "SPI=0x%x )", ntohl(eh
->spi
) );
190 /* Max length: 20 "Unknown Ext Hdr 255" */
191 sb_add(m
, "Unknown Ext Hdr %u", currenthdr
);
194 if (logflags
& IP6T_LOG_IPOPT
)
197 currenthdr
= hp
->nexthdr
;
201 switch (currenthdr
) {
204 const struct tcphdr
*th
;
206 /* Max length: 10 "PROTO=TCP " */
207 sb_add(m
, "PROTO=TCP ");
212 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
213 th
= skb_header_pointer(skb
, ptr
, sizeof(_tcph
), &_tcph
);
215 sb_add(m
, "INCOMPLETE [%u bytes] ", skb
->len
- ptr
);
219 /* Max length: 20 "SPT=65535 DPT=65535 " */
220 sb_add(m
, "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 sb_add(m
, "SEQ=%u ACK=%u ",
225 ntohl(th
->seq
), ntohl(th
->ack_seq
));
226 /* Max length: 13 "WINDOW=65535 " */
227 sb_add(m
, "WINDOW=%u ", ntohs(th
->window
));
228 /* Max length: 9 "RES=0x3C " */
229 sb_add(m
, "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 sb_add(m
, "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
)];
255 unsigned int optsize
= th
->doff
* 4
256 - sizeof(struct tcphdr
);
258 op
= skb_header_pointer(skb
,
259 ptr
+ sizeof(struct tcphdr
),
262 sb_add(m
, "OPT (TRUNCATED)");
266 /* Max length: 127 "OPT (" 15*4*2chars ") " */
268 for (i
=0; i
< optsize
; i
++)
269 sb_add(m
, "%02X", op
[i
]);
275 case IPPROTO_UDPLITE
: {
277 const struct udphdr
*uh
;
279 if (currenthdr
== IPPROTO_UDP
)
280 /* Max length: 10 "PROTO=UDP " */
281 sb_add(m
, "PROTO=UDP " );
282 else /* Max length: 14 "PROTO=UDPLITE " */
283 sb_add(m
, "PROTO=UDPLITE ");
288 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
289 uh
= skb_header_pointer(skb
, ptr
, sizeof(_udph
), &_udph
);
291 sb_add(m
, "INCOMPLETE [%u bytes] ", skb
->len
- ptr
);
295 /* Max length: 20 "SPT=65535 DPT=65535 " */
296 sb_add(m
, "SPT=%u DPT=%u LEN=%u ",
297 ntohs(uh
->source
), ntohs(uh
->dest
),
301 case IPPROTO_ICMPV6
: {
302 struct icmp6hdr _icmp6h
;
303 const struct icmp6hdr
*ic
;
305 /* Max length: 13 "PROTO=ICMPv6 " */
306 sb_add(m
, "PROTO=ICMPv6 ");
311 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
312 ic
= skb_header_pointer(skb
, ptr
, sizeof(_icmp6h
), &_icmp6h
);
314 sb_add(m
, "INCOMPLETE [%u bytes] ", skb
->len
- ptr
);
318 /* Max length: 18 "TYPE=255 CODE=255 " */
319 sb_add(m
, "TYPE=%u CODE=%u ", ic
->icmp6_type
, ic
->icmp6_code
);
321 switch (ic
->icmp6_type
) {
322 case ICMPV6_ECHO_REQUEST
:
323 case ICMPV6_ECHO_REPLY
:
324 /* Max length: 19 "ID=65535 SEQ=65535 " */
325 sb_add(m
, "ID=%u SEQ=%u ",
326 ntohs(ic
->icmp6_identifier
),
327 ntohs(ic
->icmp6_sequence
));
329 case ICMPV6_MGM_QUERY
:
330 case ICMPV6_MGM_REPORT
:
331 case ICMPV6_MGM_REDUCTION
:
334 case ICMPV6_PARAMPROB
:
335 /* Max length: 17 "POINTER=ffffffff " */
336 sb_add(m
, "POINTER=%08x ", ntohl(ic
->icmp6_pointer
));
338 case ICMPV6_DEST_UNREACH
:
339 case ICMPV6_PKT_TOOBIG
:
340 case ICMPV6_TIME_EXCEED
:
341 /* Max length: 3+maxlen */
344 dump_packet(m
, info
, skb
,
345 ptr
+ sizeof(_icmp6h
), 0);
349 /* Max length: 10 "MTU=65535 " */
350 if (ic
->icmp6_type
== ICMPV6_PKT_TOOBIG
)
351 sb_add(m
, "MTU=%u ", ntohl(ic
->icmp6_mtu
));
355 /* Max length: 10 "PROTO=255 " */
357 sb_add(m
, "PROTO=%u ", currenthdr
);
360 /* Max length: 15 "UID=4294967295 " */
361 if ((logflags
& IP6T_LOG_UID
) && recurse
&& skb
->sk
) {
362 read_lock_bh(&skb
->sk
->sk_callback_lock
);
363 if (skb
->sk
->sk_socket
&& skb
->sk
->sk_socket
->file
)
364 sb_add(m
, "UID=%u GID=%u ",
365 skb
->sk
->sk_socket
->file
->f_cred
->fsuid
,
366 skb
->sk
->sk_socket
->file
->f_cred
->fsgid
);
367 read_unlock_bh(&skb
->sk
->sk_callback_lock
);
370 /* Max length: 16 "MARK=0xFFFFFFFF " */
371 if (!recurse
&& skb
->mark
)
372 sb_add(m
, "MARK=0x%x ", skb
->mark
);
375 static void dump_mac_header(struct sbuff
*m
,
376 const struct nf_loginfo
*info
,
377 const struct sk_buff
*skb
)
379 struct net_device
*dev
= skb
->dev
;
380 unsigned int logflags
= 0;
382 if (info
->type
== NF_LOG_TYPE_LOG
)
383 logflags
= info
->u
.log
.logflags
;
385 if (!(logflags
& IP6T_LOG_MACDECODE
))
390 sb_add(m
, "MACSRC=%pM MACDST=%pM MACPROTO=%04x ",
391 eth_hdr(skb
)->h_source
, eth_hdr(skb
)->h_dest
,
392 ntohs(eth_hdr(skb
)->h_proto
));
400 if (dev
->hard_header_len
&&
401 skb
->mac_header
!= skb
->network_header
) {
402 const unsigned char *p
= skb_mac_header(skb
);
403 unsigned int len
= dev
->hard_header_len
;
406 if (dev
->type
== ARPHRD_SIT
&&
407 (p
-= ETH_HLEN
) < skb
->head
)
411 sb_add(m
, "%02x", *p
++);
412 for (i
= 1; i
< len
; i
++)
413 sb_add(m
, ":%02x", *p
++);
417 if (dev
->type
== ARPHRD_SIT
) {
418 const struct iphdr
*iph
=
419 (struct iphdr
*)skb_mac_header(skb
);
420 sb_add(m
, "TUNNEL=%pI4->%pI4 ", &iph
->saddr
, &iph
->daddr
);
426 static struct nf_loginfo default_loginfo
= {
427 .type
= NF_LOG_TYPE_LOG
,
431 .logflags
= NF_LOG_MASK
,
437 ip6t_log_packet(u_int8_t pf
,
438 unsigned int hooknum
,
439 const struct sk_buff
*skb
,
440 const struct net_device
*in
,
441 const struct net_device
*out
,
442 const struct nf_loginfo
*loginfo
,
445 struct sbuff
*m
= sb_open();
448 loginfo
= &default_loginfo
;
450 sb_add(m
, "<%d>%sIN=%s OUT=%s ", loginfo
->u
.log
.level
,
453 out
? out
->name
: "");
456 dump_mac_header(m
, loginfo
, skb
);
458 dump_packet(m
, loginfo
, skb
, skb_network_offset(skb
), 1);
464 log_tg6(struct sk_buff
*skb
, const struct xt_action_param
*par
)
466 const struct ip6t_log_info
*loginfo
= par
->targinfo
;
467 struct nf_loginfo li
;
469 li
.type
= NF_LOG_TYPE_LOG
;
470 li
.u
.log
.level
= loginfo
->level
;
471 li
.u
.log
.logflags
= loginfo
->logflags
;
473 ip6t_log_packet(NFPROTO_IPV6
, par
->hooknum
, skb
, par
->in
, par
->out
,
474 &li
, loginfo
->prefix
);
479 static int log_tg6_check(const struct xt_tgchk_param
*par
)
481 const struct ip6t_log_info
*loginfo
= par
->targinfo
;
483 if (loginfo
->level
>= 8) {
484 pr_debug("level %u >= 8\n", loginfo
->level
);
487 if (loginfo
->prefix
[sizeof(loginfo
->prefix
)-1] != '\0') {
488 pr_debug("prefix not null-terminated\n");
494 static struct xt_target log_tg6_reg __read_mostly
= {
496 .family
= NFPROTO_IPV6
,
498 .targetsize
= sizeof(struct ip6t_log_info
),
499 .checkentry
= log_tg6_check
,
503 static struct nf_logger ip6t_logger __read_mostly
= {
505 .logfn
= &ip6t_log_packet
,
509 static int __init
log_tg6_init(void)
513 ret
= xt_register_target(&log_tg6_reg
);
516 nf_log_register(NFPROTO_IPV6
, &ip6t_logger
);
520 static void __exit
log_tg6_exit(void)
522 nf_log_unregister(&ip6t_logger
);
523 xt_unregister_target(&log_tg6_reg
);
526 module_init(log_tg6_init
);
527 module_exit(log_tg6_exit
);