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 static void dump_sk_uid_gid(struct sbuff *m, struct sock *sk)
150 if (!sk || sk->sk_state == TCP_TIME_WAIT)
153 read_lock_bh(&sk->sk_callback_lock);
154 if (sk->sk_socket && sk->sk_socket->file) {
155 const struct cred *cred = sk->sk_socket->file->f_cred;
156 sb_add(m, "UID=%u GID=%u ",
157 from_kuid_munged(&init_user_ns, cred->fsuid),
158 from_kgid_munged(&init_user_ns, cred->fsgid));
160 read_unlock_bh(&sk->sk_callback_lock);
163 /* One level of recursion won't kill us */
164 static void dump_ipv4_packet(struct sbuff *m,
165 const struct nf_loginfo *info,
166 const struct sk_buff *skb,
170 const struct iphdr *ih;
171 unsigned int logflags;
173 if (info->type == NF_LOG_TYPE_LOG)
174 logflags = info->u.log.logflags;
176 logflags = NF_LOG_MASK;
178 ih = skb_header_pointer(skb, iphoff, sizeof(_iph), &_iph);
180 sb_add(m, "TRUNCATED");
185 * TOS, len, DF/MF, fragment offset, TTL, src, dst, options. */
186 /* Max length: 40 "SRC=255.255.255.255 DST=255.255.255.255 " */
187 sb_add(m, "SRC=%pI4 DST=%pI4 ",
188 &ih->saddr, &ih->daddr);
190 /* Max length: 46 "LEN=65535 TOS=0xFF PREC=0xFF TTL=255 ID=65535 " */
191 sb_add(m, "LEN=%u TOS=0x%02X PREC=0x%02X TTL=%u ID=%u ",
192 ntohs(ih->tot_len), ih->tos & IPTOS_TOS_MASK,
193 ih->tos & IPTOS_PREC_MASK, ih->ttl, ntohs(ih->id));
195 /* Max length: 6 "CE DF MF " */
196 if (ntohs(ih->frag_off) & IP_CE)
198 if (ntohs(ih->frag_off) & IP_DF)
200 if (ntohs(ih->frag_off) & IP_MF)
203 /* Max length: 11 "FRAG:65535 " */
204 if (ntohs(ih->frag_off) & IP_OFFSET)
205 sb_add(m, "FRAG:%u ", ntohs(ih->frag_off) & IP_OFFSET);
207 if ((logflags & XT_LOG_IPOPT) &&
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");
221 /* Max length: 127 "OPT (" 15*4*2chars ") " */
223 for (i = 0; i < optsize; i++)
224 sb_add(m, "%02X", op[i]);
228 switch (ih->protocol) {
230 if (dump_tcp_header(m, skb, ih->protocol,
231 ntohs(ih->frag_off) & IP_OFFSET,
232 iphoff+ih->ihl*4, logflags))
236 case IPPROTO_UDPLITE:
237 if (dump_udp_header(m, skb, ih->protocol,
238 ntohs(ih->frag_off) & IP_OFFSET,
243 struct icmphdr _icmph;
244 const struct icmphdr *ich;
245 static const size_t required_len[NR_ICMP_TYPES+1]
246 = { [ICMP_ECHOREPLY] = 4,
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),
258 [ICMP_TIMESTAMP] = 20,
259 [ICMP_TIMESTAMPREPLY] = 20,
261 [ICMP_ADDRESSREPLY] = 12 };
263 /* Max length: 11 "PROTO=ICMP " */
264 sb_add(m, "PROTO=ICMP ");
266 if (ntohs(ih->frag_off) & IP_OFFSET)
269 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
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);
278 /* Max length: 18 "TYPE=255 CODE=255 " */
279 sb_add(m, "TYPE=%u CODE=%u ", ich->type, ich->code);
281 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
282 if (ich->type <= NR_ICMP_TYPES &&
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);
293 /* Max length: 19 "ID=65535 SEQ=65535 " */
294 sb_add(m, "ID=%u SEQ=%u ",
295 ntohs(ich->un.echo.id),
296 ntohs(ich->un.echo.sequence));
299 case ICMP_PARAMETERPROB:
300 /* Max length: 14 "PARAMETER=255 " */
301 sb_add(m, "PARAMETER=%u ",
302 ntohl(ich->un.gateway) >> 24);
305 /* Max length: 24 "GATEWAY=255.255.255.255 " */
306 sb_add(m, "GATEWAY=%pI4 ", &ich->un.gateway);
308 case ICMP_DEST_UNREACH:
309 case ICMP_SOURCE_QUENCH:
310 case ICMP_TIME_EXCEEDED:
311 /* Max length: 3+maxlen */
312 if (!iphoff) { /* Only recurse once. */
314 dump_ipv4_packet(m, info, skb,
315 iphoff + ih->ihl*4+sizeof(_icmph));
319 /* Max length: 10 "MTU=65535 " */
320 if (ich->type == ICMP_DEST_UNREACH &&
321 ich->code == ICMP_FRAG_NEEDED)
322 sb_add(m, "MTU=%u ", ntohs(ich->un.frag.mtu));
328 struct ip_auth_hdr _ahdr;
329 const struct ip_auth_hdr *ah;
331 if (ntohs(ih->frag_off) & IP_OFFSET)
334 /* Max length: 9 "PROTO=AH " */
335 sb_add(m, "PROTO=AH ");
337 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
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);
346 /* Length: 15 "SPI=0xF1234567 " */
347 sb_add(m, "SPI=0x%x ", ntohl(ah->spi));
351 struct ip_esp_hdr _esph;
352 const struct ip_esp_hdr *eh;
354 /* Max length: 10 "PROTO=ESP " */
355 sb_add(m, "PROTO=ESP ");
357 if (ntohs(ih->frag_off) & IP_OFFSET)
360 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
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);
369 /* Length: 15 "SPI=0xF1234567 " */
370 sb_add(m, "SPI=0x%x ", ntohl(eh->spi));
373 /* Max length: 10 "PROTO 255 " */
375 sb_add(m, "PROTO=%u ", ih->protocol);
378 /* Max length: 15 "UID=4294967295 " */
379 if ((logflags & XT_LOG_UID) && !iphoff)
380 dump_sk_uid_gid(m, skb->sk);
382 /* Max length: 16 "MARK=0xFFFFFFFF " */
383 if (!iphoff && skb->mark)
384 sb_add(m, "MARK=0x%x ", skb->mark);
386 /* Proto Max log string length */
387 /* IP: 40+46+6+11+127 = 230 */
388 /* TCP: 10+max(25,20+30+13+9+32+11+127) = 252 */
389 /* UDP: 10+max(25,20) = 35 */
390 /* UDPLITE: 14+max(25,20) = 39 */
391 /* ICMP: 11+max(25, 18+25+max(19,14,24+3+n+10,3+n+10)) = 91+n */
392 /* ESP: 10+max(25)+15 = 50 */
393 /* AH: 9+max(25)+15 = 49 */
396 /* (ICMP allows recursion one level deep) */
397 /* maxlen = IP + ICMP + IP + max(TCP,UDP,ICMP,unknown) */
398 /* maxlen = 230+ 91 + 230 + 252 = 803 */
401 static void dump_ipv4_mac_header(struct sbuff *m,
402 const struct nf_loginfo *info,
403 const struct sk_buff *skb)
405 struct net_device *dev = skb->dev;
406 unsigned int logflags = 0;
408 if (info->type == NF_LOG_TYPE_LOG)
409 logflags = info->u.log.logflags;
411 if (!(logflags & XT_LOG_MACDECODE))
416 sb_add(m, "MACSRC=%pM MACDST=%pM MACPROTO=%04x ",
417 eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
418 ntohs(eth_hdr(skb)->h_proto));
426 if (dev->hard_header_len &&
427 skb->mac_header != skb->network_header) {
428 const unsigned char *p = skb_mac_header(skb);
431 sb_add(m, "%02x", *p++);
432 for (i = 1; i < dev->hard_header_len; i++, p++)
433 sb_add(m, ":%02x", *p);
439 log_packet_common(struct sbuff *m,
441 unsigned int hooknum,
442 const struct sk_buff *skb,
443 const struct net_device *in,
444 const struct net_device *out,
445 const struct nf_loginfo *loginfo,
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) {
454 const struct net_device *physindev;
455 const struct net_device *physoutdev;
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);
469 ipt_log_packet(struct net *net,
471 unsigned int hooknum,
472 const struct sk_buff *skb,
473 const struct net_device *in,
474 const struct net_device *out,
475 const struct nf_loginfo *loginfo,
480 /* FIXME: Disabled from containers until syslog ns is supported */
481 if (!net_eq(net, &init_net))
487 loginfo = &default_loginfo;
489 log_packet_common(m, pf, hooknum, skb, in, out, loginfo, prefix);
492 dump_ipv4_mac_header(m, loginfo, skb);
494 dump_ipv4_packet(m, loginfo, skb, 0);
499 #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
500 /* One level of recursion won't kill us */
501 static void dump_ipv6_packet(struct sbuff *m,
502 const struct nf_loginfo *info,
503 const struct sk_buff *skb, unsigned int ip6hoff,
508 struct ipv6hdr _ip6h;
509 const struct ipv6hdr *ih;
511 unsigned int hdrlen = 0;
512 unsigned int logflags;
514 if (info->type == NF_LOG_TYPE_LOG)
515 logflags = info->u.log.logflags;
517 logflags = NF_LOG_MASK;
519 ih = skb_header_pointer(skb, ip6hoff, sizeof(_ip6h), &_ip6h);
521 sb_add(m, "TRUNCATED");
525 /* Max length: 88 "SRC=0000.0000.0000.0000.0000.0000.0000.0000 DST=0000.0000.0000.0000.0000.0000.0000.0000 " */
526 sb_add(m, "SRC=%pI6 DST=%pI6 ", &ih->saddr, &ih->daddr);
528 /* Max length: 44 "LEN=65535 TC=255 HOPLIMIT=255 FLOWLBL=FFFFF " */
529 sb_add(m, "LEN=%Zu TC=%u HOPLIMIT=%u FLOWLBL=%u ",
530 ntohs(ih->payload_len) + sizeof(struct ipv6hdr),
531 (ntohl(*(__be32 *)ih) & 0x0ff00000) >> 20,
533 (ntohl(*(__be32 *)ih) & 0x000fffff));
536 ptr = ip6hoff + sizeof(struct ipv6hdr);
537 currenthdr = ih->nexthdr;
538 while (currenthdr != NEXTHDR_NONE && ip6t_ext_hdr(currenthdr)) {
539 struct ipv6_opt_hdr _hdr;
540 const struct ipv6_opt_hdr *hp;
542 hp = skb_header_pointer(skb, ptr, sizeof(_hdr), &_hdr);
544 sb_add(m, "TRUNCATED");
548 /* Max length: 48 "OPT (...) " */
549 if (logflags & XT_LOG_IPOPT)
552 switch (currenthdr) {
553 case IPPROTO_FRAGMENT: {
554 struct frag_hdr _fhdr;
555 const struct frag_hdr *fh;
558 fh = skb_header_pointer(skb, ptr, sizeof(_fhdr),
561 sb_add(m, "TRUNCATED ");
565 /* Max length: 6 "65535 " */
566 sb_add(m, "%u ", ntohs(fh->frag_off) & 0xFFF8);
568 /* Max length: 11 "INCOMPLETE " */
569 if (fh->frag_off & htons(0x0001))
570 sb_add(m, "INCOMPLETE ");
572 sb_add(m, "ID:%08x ", ntohl(fh->identification));
574 if (ntohs(fh->frag_off) & 0xFFF8)
581 case IPPROTO_DSTOPTS:
582 case IPPROTO_ROUTING:
583 case IPPROTO_HOPOPTS:
585 if (logflags & XT_LOG_IPOPT)
589 hdrlen = ipv6_optlen(hp);
593 if (logflags & XT_LOG_IPOPT) {
594 struct ip_auth_hdr _ahdr;
595 const struct ip_auth_hdr *ah;
597 /* Max length: 3 "AH " */
605 ah = skb_header_pointer(skb, ptr, sizeof(_ahdr),
609 * Max length: 26 "INCOMPLETE [65535
612 sb_add(m, "INCOMPLETE [%u bytes] )",
617 /* Length: 15 "SPI=0xF1234567 */
618 sb_add(m, "SPI=0x%x ", ntohl(ah->spi));
622 hdrlen = (hp->hdrlen+2)<<2;
625 if (logflags & XT_LOG_IPOPT) {
626 struct ip_esp_hdr _esph;
627 const struct ip_esp_hdr *eh;
629 /* Max length: 4 "ESP " */
638 * Max length: 26 "INCOMPLETE [65535 bytes] )"
640 eh = skb_header_pointer(skb, ptr, sizeof(_esph),
643 sb_add(m, "INCOMPLETE [%u bytes] )",
648 /* Length: 16 "SPI=0xF1234567 )" */
649 sb_add(m, "SPI=0x%x )", ntohl(eh->spi));
654 /* Max length: 20 "Unknown Ext Hdr 255" */
655 sb_add(m, "Unknown Ext Hdr %u", currenthdr);
658 if (logflags & XT_LOG_IPOPT)
661 currenthdr = hp->nexthdr;
665 switch (currenthdr) {
667 if (dump_tcp_header(m, skb, currenthdr, fragment, ptr,
672 case IPPROTO_UDPLITE:
673 if (dump_udp_header(m, skb, currenthdr, fragment, ptr))
676 case IPPROTO_ICMPV6: {
677 struct icmp6hdr _icmp6h;
678 const struct icmp6hdr *ic;
680 /* Max length: 13 "PROTO=ICMPv6 " */
681 sb_add(m, "PROTO=ICMPv6 ");
686 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
687 ic = skb_header_pointer(skb, ptr, sizeof(_icmp6h), &_icmp6h);
689 sb_add(m, "INCOMPLETE [%u bytes] ", skb->len - ptr);
693 /* Max length: 18 "TYPE=255 CODE=255 " */
694 sb_add(m, "TYPE=%u CODE=%u ", ic->icmp6_type, ic->icmp6_code);
696 switch (ic->icmp6_type) {
697 case ICMPV6_ECHO_REQUEST:
698 case ICMPV6_ECHO_REPLY:
699 /* Max length: 19 "ID=65535 SEQ=65535 " */
700 sb_add(m, "ID=%u SEQ=%u ",
701 ntohs(ic->icmp6_identifier),
702 ntohs(ic->icmp6_sequence));
704 case ICMPV6_MGM_QUERY:
705 case ICMPV6_MGM_REPORT:
706 case ICMPV6_MGM_REDUCTION:
709 case ICMPV6_PARAMPROB:
710 /* Max length: 17 "POINTER=ffffffff " */
711 sb_add(m, "POINTER=%08x ", ntohl(ic->icmp6_pointer));
713 case ICMPV6_DEST_UNREACH:
714 case ICMPV6_PKT_TOOBIG:
715 case ICMPV6_TIME_EXCEED:
716 /* Max length: 3+maxlen */
719 dump_ipv6_packet(m, info, skb,
720 ptr + sizeof(_icmp6h), 0);
724 /* Max length: 10 "MTU=65535 " */
725 if (ic->icmp6_type == ICMPV6_PKT_TOOBIG)
726 sb_add(m, "MTU=%u ", ntohl(ic->icmp6_mtu));
730 /* Max length: 10 "PROTO=255 " */
732 sb_add(m, "PROTO=%u ", currenthdr);
735 /* Max length: 15 "UID=4294967295 " */
736 if ((logflags & XT_LOG_UID) && recurse)
737 dump_sk_uid_gid(m, skb->sk);
739 /* Max length: 16 "MARK=0xFFFFFFFF " */
740 if (recurse && skb->mark)
741 sb_add(m, "MARK=0x%x ", skb->mark);
744 static void dump_ipv6_mac_header(struct sbuff *m,
745 const struct nf_loginfo *info,
746 const struct sk_buff *skb)
748 struct net_device *dev = skb->dev;
749 unsigned int logflags = 0;
751 if (info->type == NF_LOG_TYPE_LOG)
752 logflags = info->u.log.logflags;
754 if (!(logflags & XT_LOG_MACDECODE))
759 sb_add(m, "MACSRC=%pM MACDST=%pM MACPROTO=%04x ",
760 eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
761 ntohs(eth_hdr(skb)->h_proto));
769 if (dev->hard_header_len &&
770 skb->mac_header != skb->network_header) {
771 const unsigned char *p = skb_mac_header(skb);
772 unsigned int len = dev->hard_header_len;
775 if (dev->type == ARPHRD_SIT) {
783 sb_add(m, "%02x", *p++);
784 for (i = 1; i < len; i++)
785 sb_add(m, ":%02x", *p++);
789 if (dev->type == ARPHRD_SIT) {
790 const struct iphdr *iph =
791 (struct iphdr *)skb_mac_header(skb);
792 sb_add(m, "TUNNEL=%pI4->%pI4 ", &iph->saddr,
800 ip6t_log_packet(struct net *net,
802 unsigned int hooknum,
803 const struct sk_buff *skb,
804 const struct net_device *in,
805 const struct net_device *out,
806 const struct nf_loginfo *loginfo,
811 /* FIXME: Disabled from containers until syslog ns is supported */
812 if (!net_eq(net, &init_net))
818 loginfo = &default_loginfo;
820 log_packet_common(m, pf, hooknum, skb, in, out, loginfo, prefix);
823 dump_ipv6_mac_header(m, loginfo, skb);
825 dump_ipv6_packet(m, loginfo, skb, skb_network_offset(skb), 1);
832 log_tg(struct sk_buff *skb, const struct xt_action_param *par)
834 const struct xt_log_info *loginfo = par->targinfo;
835 struct nf_loginfo li;
836 struct net *net = dev_net(par->in ? par->in : par->out);
838 li.type = NF_LOG_TYPE_LOG;
839 li.u.log.level = loginfo->level;
840 li.u.log.logflags = loginfo->logflags;
842 if (par->family == NFPROTO_IPV4)
843 ipt_log_packet(net, NFPROTO_IPV4, par->hooknum, skb, par->in,
844 par->out, &li, loginfo->prefix);
845 #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
846 else if (par->family == NFPROTO_IPV6)
847 ip6t_log_packet(net, NFPROTO_IPV6, par->hooknum, skb, par->in,
848 par->out, &li, loginfo->prefix);
856 static int log_tg_check(const struct xt_tgchk_param *par)
858 const struct xt_log_info *loginfo = par->targinfo;
860 if (par->family != NFPROTO_IPV4 && par->family != NFPROTO_IPV6)
863 if (loginfo->level >= 8) {
864 pr_debug("level %u >= 8\n", loginfo->level);
868 if (loginfo->prefix[sizeof(loginfo->prefix)-1] != '\0') {
869 pr_debug("prefix is not null-terminated\n");
876 static struct xt_target log_tg_regs[] __read_mostly = {
879 .family = NFPROTO_IPV4,
881 .targetsize = sizeof(struct xt_log_info),
882 .checkentry = log_tg_check,
885 #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
888 .family = NFPROTO_IPV6,
890 .targetsize = sizeof(struct xt_log_info),
891 .checkentry = log_tg_check,
897 static struct nf_logger ipt_log_logger __read_mostly = {
899 .logfn = &ipt_log_packet,
903 #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
904 static struct nf_logger ip6t_log_logger __read_mostly = {
906 .logfn = &ip6t_log_packet,
911 static int __net_init log_net_init(struct net *net)
913 nf_log_set(net, NFPROTO_IPV4, &ipt_log_logger);
914 #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
915 nf_log_set(net, NFPROTO_IPV6, &ip6t_log_logger);
920 static void __net_exit log_net_exit(struct net *net)
922 nf_log_unset(net, &ipt_log_logger);
923 #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
924 nf_log_unset(net, &ip6t_log_logger);
928 static struct pernet_operations log_net_ops = {
929 .init = log_net_init,
930 .exit = log_net_exit,
933 static int __init log_tg_init(void)
937 ret = register_pernet_subsys(&log_net_ops);
941 ret = xt_register_targets(log_tg_regs, ARRAY_SIZE(log_tg_regs));
945 nf_log_register(NFPROTO_IPV4, &ipt_log_logger);
946 #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
947 nf_log_register(NFPROTO_IPV6, &ip6t_log_logger);
952 unregister_pernet_subsys(&log_net_ops);
957 static void __exit log_tg_exit(void)
959 unregister_pernet_subsys(&log_net_ops);
960 nf_log_unregister(&ipt_log_logger);
961 #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
962 nf_log_unregister(&ip6t_log_logger);
964 xt_unregister_targets(log_tg_regs, ARRAY_SIZE(log_tg_regs));
967 module_init(log_tg_init);
968 module_exit(log_tg_exit);
970 MODULE_LICENSE("GPL");
971 MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
972 MODULE_AUTHOR("Jan Rekorajski <baggins@pld.org.pl>");
973 MODULE_DESCRIPTION("Xtables: IPv4/IPv6 packet logging");
974 MODULE_ALIAS("ipt_LOG");
975 MODULE_ALIAS("ip6t_LOG");