13 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/module.h>
17 #include <linux/if_arp.h>
25 #include <linux/netfilter.h>
26 #include <linux/netfilter/x_tables.h>
28 #include <linux/netfilter_ipv6/ip6_tables.h>
50 sb_add(m,
"PROTO=UDP ");
52 sb_add(m,
"PROTO=UDPLITE ");
58 uh = skb_header_pointer(skb, offset,
sizeof(_udph), &_udph);
60 sb_add(m,
"INCOMPLETE [%u bytes] ", skb->
len - offset);
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)
81 sb_add(m,
"PROTO=TCP ");
87 th = skb_header_pointer(skb, offset,
sizeof(_tcph), &_tcph);
89 sb_add(m,
"INCOMPLETE [%u bytes] ", skb->
len - offset);
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)");
139 for (i = 0; i < optsize; i++)
140 sb_add(m,
"%02X", op[i]);
148 static void dump_sk_uid_gid(
struct sbuff *m,
struct sock *
sk)
156 sb_add(m,
"UID=%u GID=%u ",
164 static void dump_ipv4_packet(
struct sbuff *m,
170 const struct iphdr *ih;
171 unsigned int logflags;
174 logflags = info->
u.
log.logflags;
178 ih = skb_header_pointer(skb, iphoff,
sizeof(_iph), &_iph);
180 sb_add(m,
"TRUNCATED");
187 sb_add(m,
"SRC=%pI4 DST=%pI4 ",
191 sb_add(m,
"LEN=%u TOS=0x%02X PREC=0x%02X TTL=%u ID=%u ",
208 ih->ihl * 4 >
sizeof(
struct iphdr)) {
209 const unsigned char *
op;
210 unsigned char _opt[4 * 15 -
sizeof(
struct iphdr)];
211 unsigned int i, optsize;
213 optsize = ih->ihl * 4 -
sizeof(
struct iphdr);
214 op = skb_header_pointer(skb, iphoff+
sizeof(_iph),
217 sb_add(m,
"TRUNCATED");
223 for (i = 0; i < optsize; i++)
224 sb_add(m,
"%02X", op[i]);
230 if (dump_tcp_header(m, skb, ih->
protocol,
232 iphoff+ih->ihl*4, logflags))
237 if (dump_udp_header(m, skb, ih->
protocol,
248 = 8 +
sizeof(
struct iphdr),
250 = 8 +
sizeof(
struct iphdr),
252 = 8 +
sizeof(
struct iphdr),
255 = 8 +
sizeof(
struct iphdr),
257 = 8 +
sizeof(
struct iphdr),
264 sb_add(m,
"PROTO=ICMP ");
270 ich = skb_header_pointer(skb, iphoff + ih->ihl * 4,
271 sizeof(_icmph), &_icmph);
273 sb_add(m,
"INCOMPLETE [%u bytes] ",
274 skb->
len - iphoff - ih->ihl*4);
279 sb_add(m,
"TYPE=%u CODE=%u ", ich->
type, ich->
code);
283 required_len[ich->
type] &&
284 skb->
len-iphoff-ih->ihl*4 < required_len[ich->
type]) {
285 sb_add(m,
"INCOMPLETE [%u bytes] ",
286 skb->
len - iphoff - ih->ihl*4);
294 sb_add(m,
"ID=%u SEQ=%u ",
301 sb_add(m,
"PARAMETER=%u ",
306 sb_add(m,
"GATEWAY=%pI4 ", &ich->
un.
gateway);
314 dump_ipv4_packet(m, info, skb,
315 iphoff + ih->ihl*4+
sizeof(_icmph));
335 sb_add(m,
"PROTO=AH ");
338 ah = skb_header_pointer(skb, iphoff+ih->ihl*4,
339 sizeof(_ahdr), &_ahdr);
341 sb_add(m,
"INCOMPLETE [%u bytes] ",
342 skb->
len - iphoff - ih->ihl*4);
347 sb_add(m,
"SPI=0x%x ",
ntohl(ah->
spi));
355 sb_add(m,
"PROTO=ESP ");
361 eh = skb_header_pointer(skb, iphoff+ih->ihl*4,
362 sizeof(_esph), &_esph);
364 sb_add(m,
"INCOMPLETE [%u bytes] ",
365 skb->
len - iphoff - ih->ihl*4);
370 sb_add(m,
"SPI=0x%x ",
ntohl(eh->
spi));
375 sb_add(m,
"PROTO=%u ", ih->
protocol);
380 dump_sk_uid_gid(m, skb->
sk);
383 if (!iphoff && skb->
mark)
384 sb_add(m,
"MARK=0x%x ", skb->
mark);
401 static void dump_ipv4_mac_header(
struct sbuff *m,
406 unsigned int logflags = 0;
409 logflags = info->
u.
log.logflags;
416 sb_add(m,
"MACSRC=%pM MACDST=%pM MACPROTO=%04x ",
428 const unsigned char *
p = skb_mac_header(skb);
431 sb_add(m,
"%02x", *p++);
433 sb_add(m,
":%02x", *p);
439 log_packet_common(
struct sbuff *m,
448 sb_add(m,
KERN_SOH "%c%sIN=%s OUT=%s ",
449 '0' + loginfo->
u.
log.level, prefix,
451 out ? out->
name :
"");
452 #ifdef CONFIG_BRIDGE_NETFILTER
453 if (skb->nf_bridge) {
457 physindev = skb->nf_bridge->physindev;
458 if (physindev && in != physindev)
459 sb_add(m,
"PHYSIN=%s ", physindev->
name);
460 physoutdev = skb->nf_bridge->physoutdev;
461 if (physoutdev && out != physoutdev)
462 sb_add(m,
"PHYSOUT=%s ", physoutdev->
name);
470 unsigned int hooknum,
477 struct sbuff *m = sb_open();
480 loginfo = &default_loginfo;
482 log_packet_common(m, pf, hooknum, skb, in, out, loginfo, prefix);
485 dump_ipv4_mac_header(m, loginfo, skb);
487 dump_ipv4_packet(m, loginfo, skb, 0);
492 #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
494 static void dump_ipv6_packet(
struct sbuff *m,
496 const struct sk_buff *skb,
unsigned int ip6hoff,
505 unsigned int logflags;
508 logflags = info->
u.
log.logflags;
512 ih = skb_header_pointer(skb, ip6hoff,
sizeof(_ip6h), &_ip6h);
514 sb_add(m,
"TRUNCATED");
519 sb_add(m,
"SRC=%pI6 DST=%pI6 ", &ih->
saddr, &ih->
daddr);
522 sb_add(m,
"LEN=%Zu TC=%u HOPLIMIT=%u FLOWLBL=%u ",
529 ptr = ip6hoff +
sizeof(
struct ipv6hdr);
531 while (currenthdr !=
NEXTHDR_NONE && ip6t_ext_hdr(currenthdr)) {
535 hp = skb_header_pointer(skb, ptr,
sizeof(_hdr), &_hdr);
537 sb_add(m,
"TRUNCATED");
542 if (logflags & XT_LOG_IPOPT)
545 switch (currenthdr) {
551 fh = skb_header_pointer(skb, ptr,
sizeof(_fhdr),
554 sb_add(m,
"TRUNCATED ");
563 sb_add(m,
"INCOMPLETE ");
578 if (logflags & XT_LOG_IPOPT)
586 if (logflags & XT_LOG_IPOPT) {
598 ah = skb_header_pointer(skb, ptr,
sizeof(_ahdr),
605 sb_add(m,
"INCOMPLETE [%u bytes] )",
611 sb_add(m,
"SPI=0x%x ",
ntohl(ah->
spi));
615 hdrlen = (hp->
hdrlen+2)<<2;
618 if (logflags & XT_LOG_IPOPT) {
633 eh = skb_header_pointer(skb, ptr,
sizeof(_esph),
636 sb_add(m,
"INCOMPLETE [%u bytes] )",
642 sb_add(m,
"SPI=0x%x )",
ntohl(eh->
spi));
648 sb_add(m,
"Unknown Ext Hdr %u", currenthdr);
651 if (logflags & XT_LOG_IPOPT)
658 switch (currenthdr) {
660 if (dump_tcp_header(m, skb, currenthdr, fragment, ptr,
666 if (dump_udp_header(m, skb, currenthdr, fragment, ptr))
674 sb_add(m,
"PROTO=ICMPv6 ");
680 ic = skb_header_pointer(skb, ptr,
sizeof(_icmp6h), &_icmp6h);
682 sb_add(m,
"INCOMPLETE [%u bytes] ", skb->
len - ptr);
693 sb_add(m,
"ID=%u SEQ=%u ",
694 ntohs(ic->icmp6_identifier),
695 ntohs(ic->icmp6_sequence));
704 sb_add(m,
"POINTER=%08x ",
ntohl(ic->icmp6_pointer));
712 dump_ipv6_packet(m, info, skb,
713 ptr +
sizeof(_icmp6h), 0);
719 sb_add(m,
"MTU=%u ",
ntohl(ic->icmp6_mtu));
725 sb_add(m,
"PROTO=%u ", currenthdr);
729 if ((logflags & XT_LOG_UID) && recurse)
730 dump_sk_uid_gid(m, skb->
sk);
733 if (!recurse && skb->
mark)
734 sb_add(m,
"MARK=0x%x ", skb->
mark);
737 static void dump_ipv6_mac_header(
struct sbuff *m,
742 unsigned int logflags = 0;
745 logflags = info->
u.
log.logflags;
747 if (!(logflags & XT_LOG_MACDECODE))
752 sb_add(m,
"MACSRC=%pM MACDST=%pM MACPROTO=%04x ",
764 const unsigned char *p = skb_mac_header(skb);
776 sb_add(m,
"%02x", *p++);
777 for (i = 1; i < len; i++)
778 sb_add(m,
":%02x", *p++);
783 const struct iphdr *iph =
784 (
struct iphdr *)skb_mac_header(skb);
785 sb_add(m,
"TUNNEL=%pI4->%pI4 ", &iph->
saddr,
794 unsigned int hooknum,
801 struct sbuff *m = sb_open();
804 loginfo = &default_loginfo;
806 log_packet_common(m, pf, hooknum, skb, in, out, loginfo, prefix);
809 dump_ipv6_mac_header(m, loginfo, skb);
811 dump_ipv6_packet(m, loginfo, skb, skb_network_offset(skb), 1);
824 li.u.log.level = loginfo->
level;
830 #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
848 if (loginfo->
level >= 8) {
853 if (loginfo->
prefix[
sizeof(loginfo->
prefix)-1] !=
'\0') {
854 pr_debug(
"prefix is not null-terminated\n");
867 .checkentry = log_tg_check,
870 #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
876 .checkentry = log_tg_check,
882 static struct nf_logger ipt_log_logger __read_mostly = {
884 .logfn = &ipt_log_packet,
888 #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
889 static struct nf_logger ip6t_log_logger __read_mostly = {
891 .logfn = &ip6t_log_packet,
896 static int __init log_tg_init(
void)
905 #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
911 static void __exit log_tg_exit(
void)
914 #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)