2 * This is a module which is used for logging packets.
5 /* (C) 1999-2001 Paul `Rusty' Russell
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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/module.h>
15 #include <linux/spinlock.h>
16 #include <linux/skbuff.h>
17 #include <linux/if_arp.h>
23 #include <net/route.h>
25 #include <linux/netfilter.h>
26 #include <linux/netfilter/x_tables.h>
27 #include <linux/netfilter/xt_LOG.h>
28 #include <linux/netfilter_ipv6/ip6_tables.h>
29 #include <net/netfilter/nf_log.h>
30 #include <net/netfilter/xt_log.h>
32 static struct nf_loginfo default_loginfo
= {
33 .type
= NF_LOG_TYPE_LOG
,
37 .logflags
= NF_LOG_MASK
,
42 static int dump_udp_header(struct sbuff
*m
, const struct sk_buff
*skb
,
43 u8 proto
, int fragment
, unsigned int offset
)
46 const struct udphdr
*uh
;
48 if (proto
== IPPROTO_UDP
)
49 /* Max length: 10 "PROTO=UDP " */
50 sb_add(m
, "PROTO=UDP ");
51 else /* Max length: 14 "PROTO=UDPLITE " */
52 sb_add(m
, "PROTO=UDPLITE ");
57 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
58 uh
= skb_header_pointer(skb
, offset
, sizeof(_udph
), &_udph
);
60 sb_add(m
, "INCOMPLETE [%u bytes] ", skb
->len
- offset
);
65 /* Max length: 20 "SPT=65535 DPT=65535 " */
66 sb_add(m
, "SPT=%u DPT=%u LEN=%u ", ntohs(uh
->source
), ntohs(uh
->dest
),
73 static int dump_tcp_header(struct sbuff
*m
, const struct sk_buff
*skb
,
74 u8 proto
, int fragment
, unsigned int offset
,
75 unsigned int logflags
)
78 const struct tcphdr
*th
;
80 /* Max length: 10 "PROTO=TCP " */
81 sb_add(m
, "PROTO=TCP ");
86 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
87 th
= skb_header_pointer(skb
, offset
, sizeof(_tcph
), &_tcph
);
89 sb_add(m
, "INCOMPLETE [%u bytes] ", skb
->len
- offset
);
93 /* Max length: 20 "SPT=65535 DPT=65535 " */
94 sb_add(m
, "SPT=%u DPT=%u ", ntohs(th
->source
), ntohs(th
->dest
));
95 /* Max length: 30 "SEQ=4294967295 ACK=4294967295 " */
96 if (logflags
& XT_LOG_TCPSEQ
)
97 sb_add(m
, "SEQ=%u ACK=%u ", ntohl(th
->seq
), ntohl(th
->ack_seq
));
99 /* Max length: 13 "WINDOW=65535 " */
100 sb_add(m
, "WINDOW=%u ", ntohs(th
->window
));
101 /* Max length: 9 "RES=0x3C " */
102 sb_add(m
, "RES=0x%02x ", (u_int8_t
)(ntohl(tcp_flag_word(th
) &
103 TCP_RESERVED_BITS
) >> 22));
104 /* Max length: 32 "CWR ECE URG ACK PSH RST SYN FIN " */
121 /* Max length: 11 "URGP=65535 " */
122 sb_add(m
, "URGP=%u ", ntohs(th
->urg_ptr
));
124 if ((logflags
& XT_LOG_TCPOPT
) && th
->doff
*4 > sizeof(struct tcphdr
)) {
125 u_int8_t _opt
[60 - sizeof(struct tcphdr
)];
128 unsigned int optsize
= th
->doff
*4 - sizeof(struct tcphdr
);
130 op
= skb_header_pointer(skb
, offset
+ sizeof(struct tcphdr
),
133 sb_add(m
, "OPT (TRUNCATED)");
137 /* Max length: 127 "OPT (" 15*4*2chars ") " */
139 for (i
= 0; i
< optsize
; i
++)
140 sb_add(m
, "%02X", op
[i
]);
148 /* One level of recursion won't kill us */
149 static void dump_ipv4_packet(struct sbuff
*m
,
150 const struct nf_loginfo
*info
,
151 const struct sk_buff
*skb
,
155 const struct iphdr
*ih
;
156 unsigned int logflags
;
158 if (info
->type
== NF_LOG_TYPE_LOG
)
159 logflags
= info
->u
.log
.logflags
;
161 logflags
= NF_LOG_MASK
;
163 ih
= skb_header_pointer(skb
, iphoff
, sizeof(_iph
), &_iph
);
165 sb_add(m
, "TRUNCATED");
170 * TOS, len, DF/MF, fragment offset, TTL, src, dst, options. */
171 /* Max length: 40 "SRC=255.255.255.255 DST=255.255.255.255 " */
172 sb_add(m
, "SRC=%pI4 DST=%pI4 ",
173 &ih
->saddr
, &ih
->daddr
);
175 /* Max length: 46 "LEN=65535 TOS=0xFF PREC=0xFF TTL=255 ID=65535 " */
176 sb_add(m
, "LEN=%u TOS=0x%02X PREC=0x%02X TTL=%u ID=%u ",
177 ntohs(ih
->tot_len
), ih
->tos
& IPTOS_TOS_MASK
,
178 ih
->tos
& IPTOS_PREC_MASK
, ih
->ttl
, ntohs(ih
->id
));
180 /* Max length: 6 "CE DF MF " */
181 if (ntohs(ih
->frag_off
) & IP_CE
)
183 if (ntohs(ih
->frag_off
) & IP_DF
)
185 if (ntohs(ih
->frag_off
) & IP_MF
)
188 /* Max length: 11 "FRAG:65535 " */
189 if (ntohs(ih
->frag_off
) & IP_OFFSET
)
190 sb_add(m
, "FRAG:%u ", ntohs(ih
->frag_off
) & IP_OFFSET
);
192 if ((logflags
& XT_LOG_IPOPT
) &&
193 ih
->ihl
* 4 > sizeof(struct iphdr
)) {
194 const unsigned char *op
;
195 unsigned char _opt
[4 * 15 - sizeof(struct iphdr
)];
196 unsigned int i
, optsize
;
198 optsize
= ih
->ihl
* 4 - sizeof(struct iphdr
);
199 op
= skb_header_pointer(skb
, iphoff
+sizeof(_iph
),
202 sb_add(m
, "TRUNCATED");
206 /* Max length: 127 "OPT (" 15*4*2chars ") " */
208 for (i
= 0; i
< optsize
; i
++)
209 sb_add(m
, "%02X", op
[i
]);
213 switch (ih
->protocol
) {
215 if (dump_tcp_header(m
, skb
, ih
->protocol
,
216 ntohs(ih
->frag_off
) & IP_OFFSET
,
217 iphoff
+ih
->ihl
*4, logflags
))
221 case IPPROTO_UDPLITE
:
222 if (dump_udp_header(m
, skb
, ih
->protocol
,
223 ntohs(ih
->frag_off
) & IP_OFFSET
,
228 struct icmphdr _icmph
;
229 const struct icmphdr
*ich
;
230 static const size_t required_len
[NR_ICMP_TYPES
+1]
231 = { [ICMP_ECHOREPLY
] = 4,
233 = 8 + sizeof(struct iphdr
),
235 = 8 + sizeof(struct iphdr
),
237 = 8 + sizeof(struct iphdr
),
240 = 8 + sizeof(struct iphdr
),
242 = 8 + sizeof(struct iphdr
),
243 [ICMP_TIMESTAMP
] = 20,
244 [ICMP_TIMESTAMPREPLY
] = 20,
246 [ICMP_ADDRESSREPLY
] = 12 };
248 /* Max length: 11 "PROTO=ICMP " */
249 sb_add(m
, "PROTO=ICMP ");
251 if (ntohs(ih
->frag_off
) & IP_OFFSET
)
254 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
255 ich
= skb_header_pointer(skb
, iphoff
+ ih
->ihl
* 4,
256 sizeof(_icmph
), &_icmph
);
258 sb_add(m
, "INCOMPLETE [%u bytes] ",
259 skb
->len
- iphoff
- ih
->ihl
*4);
263 /* Max length: 18 "TYPE=255 CODE=255 " */
264 sb_add(m
, "TYPE=%u CODE=%u ", ich
->type
, ich
->code
);
266 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
267 if (ich
->type
<= NR_ICMP_TYPES
&&
268 required_len
[ich
->type
] &&
269 skb
->len
-iphoff
-ih
->ihl
*4 < required_len
[ich
->type
]) {
270 sb_add(m
, "INCOMPLETE [%u bytes] ",
271 skb
->len
- iphoff
- ih
->ihl
*4);
278 /* Max length: 19 "ID=65535 SEQ=65535 " */
279 sb_add(m
, "ID=%u SEQ=%u ",
280 ntohs(ich
->un
.echo
.id
),
281 ntohs(ich
->un
.echo
.sequence
));
284 case ICMP_PARAMETERPROB
:
285 /* Max length: 14 "PARAMETER=255 " */
286 sb_add(m
, "PARAMETER=%u ",
287 ntohl(ich
->un
.gateway
) >> 24);
290 /* Max length: 24 "GATEWAY=255.255.255.255 " */
291 sb_add(m
, "GATEWAY=%pI4 ", &ich
->un
.gateway
);
293 case ICMP_DEST_UNREACH
:
294 case ICMP_SOURCE_QUENCH
:
295 case ICMP_TIME_EXCEEDED
:
296 /* Max length: 3+maxlen */
297 if (!iphoff
) { /* Only recurse once. */
299 dump_ipv4_packet(m
, info
, skb
,
300 iphoff
+ ih
->ihl
*4+sizeof(_icmph
));
304 /* Max length: 10 "MTU=65535 " */
305 if (ich
->type
== ICMP_DEST_UNREACH
&&
306 ich
->code
== ICMP_FRAG_NEEDED
)
307 sb_add(m
, "MTU=%u ", ntohs(ich
->un
.frag
.mtu
));
313 struct ip_auth_hdr _ahdr
;
314 const struct ip_auth_hdr
*ah
;
316 if (ntohs(ih
->frag_off
) & IP_OFFSET
)
319 /* Max length: 9 "PROTO=AH " */
320 sb_add(m
, "PROTO=AH ");
322 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
323 ah
= skb_header_pointer(skb
, iphoff
+ih
->ihl
*4,
324 sizeof(_ahdr
), &_ahdr
);
326 sb_add(m
, "INCOMPLETE [%u bytes] ",
327 skb
->len
- iphoff
- ih
->ihl
*4);
331 /* Length: 15 "SPI=0xF1234567 " */
332 sb_add(m
, "SPI=0x%x ", ntohl(ah
->spi
));
336 struct ip_esp_hdr _esph
;
337 const struct ip_esp_hdr
*eh
;
339 /* Max length: 10 "PROTO=ESP " */
340 sb_add(m
, "PROTO=ESP ");
342 if (ntohs(ih
->frag_off
) & IP_OFFSET
)
345 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
346 eh
= skb_header_pointer(skb
, iphoff
+ih
->ihl
*4,
347 sizeof(_esph
), &_esph
);
349 sb_add(m
, "INCOMPLETE [%u bytes] ",
350 skb
->len
- iphoff
- ih
->ihl
*4);
354 /* Length: 15 "SPI=0xF1234567 " */
355 sb_add(m
, "SPI=0x%x ", ntohl(eh
->spi
));
358 /* Max length: 10 "PROTO 255 " */
360 sb_add(m
, "PROTO=%u ", ih
->protocol
);
363 /* Max length: 15 "UID=4294967295 " */
364 if ((logflags
& XT_LOG_UID
) && !iphoff
&& skb
->sk
) {
365 read_lock_bh(&skb
->sk
->sk_callback_lock
);
366 if (skb
->sk
->sk_socket
&& skb
->sk
->sk_socket
->file
)
367 sb_add(m
, "UID=%u GID=%u ",
368 skb
->sk
->sk_socket
->file
->f_cred
->fsuid
,
369 skb
->sk
->sk_socket
->file
->f_cred
->fsgid
);
370 read_unlock_bh(&skb
->sk
->sk_callback_lock
);
373 /* Max length: 16 "MARK=0xFFFFFFFF " */
374 if (!iphoff
&& skb
->mark
)
375 sb_add(m
, "MARK=0x%x ", skb
->mark
);
377 /* Proto Max log string length */
378 /* IP: 40+46+6+11+127 = 230 */
379 /* TCP: 10+max(25,20+30+13+9+32+11+127) = 252 */
380 /* UDP: 10+max(25,20) = 35 */
381 /* UDPLITE: 14+max(25,20) = 39 */
382 /* ICMP: 11+max(25, 18+25+max(19,14,24+3+n+10,3+n+10)) = 91+n */
383 /* ESP: 10+max(25)+15 = 50 */
384 /* AH: 9+max(25)+15 = 49 */
387 /* (ICMP allows recursion one level deep) */
388 /* maxlen = IP + ICMP + IP + max(TCP,UDP,ICMP,unknown) */
389 /* maxlen = 230+ 91 + 230 + 252 = 803 */
392 static void dump_ipv4_mac_header(struct sbuff
*m
,
393 const struct nf_loginfo
*info
,
394 const struct sk_buff
*skb
)
396 struct net_device
*dev
= skb
->dev
;
397 unsigned int logflags
= 0;
399 if (info
->type
== NF_LOG_TYPE_LOG
)
400 logflags
= info
->u
.log
.logflags
;
402 if (!(logflags
& XT_LOG_MACDECODE
))
407 sb_add(m
, "MACSRC=%pM MACDST=%pM MACPROTO=%04x ",
408 eth_hdr(skb
)->h_source
, eth_hdr(skb
)->h_dest
,
409 ntohs(eth_hdr(skb
)->h_proto
));
417 if (dev
->hard_header_len
&&
418 skb
->mac_header
!= skb
->network_header
) {
419 const unsigned char *p
= skb_mac_header(skb
);
422 sb_add(m
, "%02x", *p
++);
423 for (i
= 1; i
< dev
->hard_header_len
; i
++, p
++)
424 sb_add(m
, ":%02x", *p
);
430 log_packet_common(struct sbuff
*m
,
432 unsigned int hooknum
,
433 const struct sk_buff
*skb
,
434 const struct net_device
*in
,
435 const struct net_device
*out
,
436 const struct nf_loginfo
*loginfo
,
439 sb_add(m
, "<%d>%sIN=%s OUT=%s ", loginfo
->u
.log
.level
,
442 out
? out
->name
: "");
443 #ifdef CONFIG_BRIDGE_NETFILTER
444 if (skb
->nf_bridge
) {
445 const struct net_device
*physindev
;
446 const struct net_device
*physoutdev
;
448 physindev
= skb
->nf_bridge
->physindev
;
449 if (physindev
&& in
!= physindev
)
450 sb_add(m
, "PHYSIN=%s ", physindev
->name
);
451 physoutdev
= skb
->nf_bridge
->physoutdev
;
452 if (physoutdev
&& out
!= physoutdev
)
453 sb_add(m
, "PHYSOUT=%s ", physoutdev
->name
);
460 ipt_log_packet(u_int8_t pf
,
461 unsigned int hooknum
,
462 const struct sk_buff
*skb
,
463 const struct net_device
*in
,
464 const struct net_device
*out
,
465 const struct nf_loginfo
*loginfo
,
468 struct sbuff
*m
= sb_open();
471 loginfo
= &default_loginfo
;
473 log_packet_common(m
, pf
, hooknum
, skb
, in
, out
, loginfo
, prefix
);
476 dump_ipv4_mac_header(m
, loginfo
, skb
);
478 dump_ipv4_packet(m
, loginfo
, skb
, 0);
483 #if IS_ENABLED(CONFIG_IPV6)
484 /* One level of recursion won't kill us */
485 static void dump_ipv6_packet(struct sbuff
*m
,
486 const struct nf_loginfo
*info
,
487 const struct sk_buff
*skb
, unsigned int ip6hoff
,
492 struct ipv6hdr _ip6h
;
493 const struct ipv6hdr
*ih
;
495 unsigned int hdrlen
= 0;
496 unsigned int logflags
;
498 if (info
->type
== NF_LOG_TYPE_LOG
)
499 logflags
= info
->u
.log
.logflags
;
501 logflags
= NF_LOG_MASK
;
503 ih
= skb_header_pointer(skb
, ip6hoff
, sizeof(_ip6h
), &_ip6h
);
505 sb_add(m
, "TRUNCATED");
509 /* Max length: 88 "SRC=0000.0000.0000.0000.0000.0000.0000.0000 DST=0000.0000.0000.0000.0000.0000.0000.0000 " */
510 sb_add(m
, "SRC=%pI6 DST=%pI6 ", &ih
->saddr
, &ih
->daddr
);
512 /* Max length: 44 "LEN=65535 TC=255 HOPLIMIT=255 FLOWLBL=FFFFF " */
513 sb_add(m
, "LEN=%Zu TC=%u HOPLIMIT=%u FLOWLBL=%u ",
514 ntohs(ih
->payload_len
) + sizeof(struct ipv6hdr
),
515 (ntohl(*(__be32
*)ih
) & 0x0ff00000) >> 20,
517 (ntohl(*(__be32
*)ih
) & 0x000fffff));
520 ptr
= ip6hoff
+ sizeof(struct ipv6hdr
);
521 currenthdr
= ih
->nexthdr
;
522 while (currenthdr
!= NEXTHDR_NONE
&& ip6t_ext_hdr(currenthdr
)) {
523 struct ipv6_opt_hdr _hdr
;
524 const struct ipv6_opt_hdr
*hp
;
526 hp
= skb_header_pointer(skb
, ptr
, sizeof(_hdr
), &_hdr
);
528 sb_add(m
, "TRUNCATED");
532 /* Max length: 48 "OPT (...) " */
533 if (logflags
& XT_LOG_IPOPT
)
536 switch (currenthdr
) {
537 case IPPROTO_FRAGMENT
: {
538 struct frag_hdr _fhdr
;
539 const struct frag_hdr
*fh
;
542 fh
= skb_header_pointer(skb
, ptr
, sizeof(_fhdr
),
545 sb_add(m
, "TRUNCATED ");
549 /* Max length: 6 "65535 " */
550 sb_add(m
, "%u ", ntohs(fh
->frag_off
) & 0xFFF8);
552 /* Max length: 11 "INCOMPLETE " */
553 if (fh
->frag_off
& htons(0x0001))
554 sb_add(m
, "INCOMPLETE ");
556 sb_add(m
, "ID:%08x ", ntohl(fh
->identification
));
558 if (ntohs(fh
->frag_off
) & 0xFFF8)
565 case IPPROTO_DSTOPTS
:
566 case IPPROTO_ROUTING
:
567 case IPPROTO_HOPOPTS
:
569 if (logflags
& XT_LOG_IPOPT
)
573 hdrlen
= ipv6_optlen(hp
);
577 if (logflags
& XT_LOG_IPOPT
) {
578 struct ip_auth_hdr _ahdr
;
579 const struct ip_auth_hdr
*ah
;
581 /* Max length: 3 "AH " */
589 ah
= skb_header_pointer(skb
, ptr
, sizeof(_ahdr
),
593 * Max length: 26 "INCOMPLETE [65535
596 sb_add(m
, "INCOMPLETE [%u bytes] )",
601 /* Length: 15 "SPI=0xF1234567 */
602 sb_add(m
, "SPI=0x%x ", ntohl(ah
->spi
));
606 hdrlen
= (hp
->hdrlen
+2)<<2;
609 if (logflags
& XT_LOG_IPOPT
) {
610 struct ip_esp_hdr _esph
;
611 const struct ip_esp_hdr
*eh
;
613 /* Max length: 4 "ESP " */
622 * Max length: 26 "INCOMPLETE [65535 bytes] )"
624 eh
= skb_header_pointer(skb
, ptr
, sizeof(_esph
),
627 sb_add(m
, "INCOMPLETE [%u bytes] )",
632 /* Length: 16 "SPI=0xF1234567 )" */
633 sb_add(m
, "SPI=0x%x )", ntohl(eh
->spi
));
638 /* Max length: 20 "Unknown Ext Hdr 255" */
639 sb_add(m
, "Unknown Ext Hdr %u", currenthdr
);
642 if (logflags
& XT_LOG_IPOPT
)
645 currenthdr
= hp
->nexthdr
;
649 switch (currenthdr
) {
651 if (dump_tcp_header(m
, skb
, currenthdr
, fragment
, ptr
,
656 case IPPROTO_UDPLITE
:
657 if (dump_udp_header(m
, skb
, currenthdr
, fragment
, ptr
))
660 case IPPROTO_ICMPV6
: {
661 struct icmp6hdr _icmp6h
;
662 const struct icmp6hdr
*ic
;
664 /* Max length: 13 "PROTO=ICMPv6 " */
665 sb_add(m
, "PROTO=ICMPv6 ");
670 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
671 ic
= skb_header_pointer(skb
, ptr
, sizeof(_icmp6h
), &_icmp6h
);
673 sb_add(m
, "INCOMPLETE [%u bytes] ", skb
->len
- ptr
);
677 /* Max length: 18 "TYPE=255 CODE=255 " */
678 sb_add(m
, "TYPE=%u CODE=%u ", ic
->icmp6_type
, ic
->icmp6_code
);
680 switch (ic
->icmp6_type
) {
681 case ICMPV6_ECHO_REQUEST
:
682 case ICMPV6_ECHO_REPLY
:
683 /* Max length: 19 "ID=65535 SEQ=65535 " */
684 sb_add(m
, "ID=%u SEQ=%u ",
685 ntohs(ic
->icmp6_identifier
),
686 ntohs(ic
->icmp6_sequence
));
688 case ICMPV6_MGM_QUERY
:
689 case ICMPV6_MGM_REPORT
:
690 case ICMPV6_MGM_REDUCTION
:
693 case ICMPV6_PARAMPROB
:
694 /* Max length: 17 "POINTER=ffffffff " */
695 sb_add(m
, "POINTER=%08x ", ntohl(ic
->icmp6_pointer
));
697 case ICMPV6_DEST_UNREACH
:
698 case ICMPV6_PKT_TOOBIG
:
699 case ICMPV6_TIME_EXCEED
:
700 /* Max length: 3+maxlen */
703 dump_ipv6_packet(m
, info
, skb
,
704 ptr
+ sizeof(_icmp6h
), 0);
708 /* Max length: 10 "MTU=65535 " */
709 if (ic
->icmp6_type
== ICMPV6_PKT_TOOBIG
)
710 sb_add(m
, "MTU=%u ", ntohl(ic
->icmp6_mtu
));
714 /* Max length: 10 "PROTO=255 " */
716 sb_add(m
, "PROTO=%u ", currenthdr
);
719 /* Max length: 15 "UID=4294967295 " */
720 if ((logflags
& XT_LOG_UID
) && recurse
&& skb
->sk
) {
721 read_lock_bh(&skb
->sk
->sk_callback_lock
);
722 if (skb
->sk
->sk_socket
&& skb
->sk
->sk_socket
->file
)
723 sb_add(m
, "UID=%u GID=%u ",
724 skb
->sk
->sk_socket
->file
->f_cred
->fsuid
,
725 skb
->sk
->sk_socket
->file
->f_cred
->fsgid
);
726 read_unlock_bh(&skb
->sk
->sk_callback_lock
);
729 /* Max length: 16 "MARK=0xFFFFFFFF " */
730 if (!recurse
&& skb
->mark
)
731 sb_add(m
, "MARK=0x%x ", skb
->mark
);
734 static void dump_ipv6_mac_header(struct sbuff
*m
,
735 const struct nf_loginfo
*info
,
736 const struct sk_buff
*skb
)
738 struct net_device
*dev
= skb
->dev
;
739 unsigned int logflags
= 0;
741 if (info
->type
== NF_LOG_TYPE_LOG
)
742 logflags
= info
->u
.log
.logflags
;
744 if (!(logflags
& XT_LOG_MACDECODE
))
749 sb_add(m
, "MACSRC=%pM MACDST=%pM MACPROTO=%04x ",
750 eth_hdr(skb
)->h_source
, eth_hdr(skb
)->h_dest
,
751 ntohs(eth_hdr(skb
)->h_proto
));
759 if (dev
->hard_header_len
&&
760 skb
->mac_header
!= skb
->network_header
) {
761 const unsigned char *p
= skb_mac_header(skb
);
762 unsigned int len
= dev
->hard_header_len
;
765 if (dev
->type
== ARPHRD_SIT
) {
773 sb_add(m
, "%02x", *p
++);
774 for (i
= 1; i
< len
; i
++)
775 sb_add(m
, ":%02x", *p
++);
779 if (dev
->type
== ARPHRD_SIT
) {
780 const struct iphdr
*iph
=
781 (struct iphdr
*)skb_mac_header(skb
);
782 sb_add(m
, "TUNNEL=%pI4->%pI4 ", &iph
->saddr
,
790 ip6t_log_packet(u_int8_t pf
,
791 unsigned int hooknum
,
792 const struct sk_buff
*skb
,
793 const struct net_device
*in
,
794 const struct net_device
*out
,
795 const struct nf_loginfo
*loginfo
,
798 struct sbuff
*m
= sb_open();
801 loginfo
= &default_loginfo
;
803 log_packet_common(m
, pf
, hooknum
, skb
, in
, out
, loginfo
, prefix
);
806 dump_ipv6_mac_header(m
, loginfo
, skb
);
808 dump_ipv6_packet(m
, loginfo
, skb
, skb_network_offset(skb
), 1);
815 log_tg(struct sk_buff
*skb
, const struct xt_action_param
*par
)
817 const struct xt_log_info
*loginfo
= par
->targinfo
;
818 struct nf_loginfo li
;
820 li
.type
= NF_LOG_TYPE_LOG
;
821 li
.u
.log
.level
= loginfo
->level
;
822 li
.u
.log
.logflags
= loginfo
->logflags
;
824 if (par
->family
== NFPROTO_IPV4
)
825 ipt_log_packet(NFPROTO_IPV4
, par
->hooknum
, skb
, par
->in
,
826 par
->out
, &li
, loginfo
->prefix
);
827 #if IS_ENABLED(CONFIG_IPV6)
828 else if (par
->family
== NFPROTO_IPV6
)
829 ip6t_log_packet(NFPROTO_IPV6
, par
->hooknum
, skb
, par
->in
,
830 par
->out
, &li
, loginfo
->prefix
);
838 static int log_tg_check(const struct xt_tgchk_param
*par
)
840 const struct xt_log_info
*loginfo
= par
->targinfo
;
842 if (par
->family
!= NFPROTO_IPV4
&& par
->family
!= NFPROTO_IPV6
)
845 if (loginfo
->level
>= 8) {
846 pr_debug("level %u >= 8\n", loginfo
->level
);
850 if (loginfo
->prefix
[sizeof(loginfo
->prefix
)-1] != '\0') {
851 pr_debug("prefix is not null-terminated\n");
858 static struct xt_target log_tg_regs
[] __read_mostly
= {
861 .family
= NFPROTO_IPV4
,
863 .targetsize
= sizeof(struct xt_log_info
),
864 .checkentry
= log_tg_check
,
867 #if IS_ENABLED(CONFIG_IPV6)
870 .family
= NFPROTO_IPV6
,
872 .targetsize
= sizeof(struct xt_log_info
),
873 .checkentry
= log_tg_check
,
879 static struct nf_logger ipt_log_logger __read_mostly
= {
881 .logfn
= &ipt_log_packet
,
885 #if IS_ENABLED(CONFIG_IPV6)
886 static struct nf_logger ip6t_log_logger __read_mostly
= {
888 .logfn
= &ip6t_log_packet
,
893 static int __init
log_tg_init(void)
897 ret
= xt_register_targets(log_tg_regs
, ARRAY_SIZE(log_tg_regs
));
901 nf_log_register(NFPROTO_IPV4
, &ipt_log_logger
);
902 #if IS_ENABLED(CONFIG_IPV6)
903 nf_log_register(NFPROTO_IPV6
, &ip6t_log_logger
);
908 static void __exit
log_tg_exit(void)
910 nf_log_unregister(&ipt_log_logger
);
911 #if IS_ENABLED(CONFIG_IPV6)
912 nf_log_unregister(&ip6t_log_logger
);
914 xt_unregister_targets(log_tg_regs
, ARRAY_SIZE(log_tg_regs
));
917 module_init(log_tg_init
);
918 module_exit(log_tg_exit
);
920 MODULE_LICENSE("GPL");
921 MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
922 MODULE_AUTHOR("Jan Rekorajski <baggins@pld.org.pl>");
923 MODULE_DESCRIPTION("Xtables: IPv4/IPv6 packet logging");
924 MODULE_ALIAS("ipt_LOG");
925 MODULE_ALIAS("ip6t_LOG");