1 // SPDX-License-Identifier: GPL-2.0-only
2 /* (C) 1999-2001 Paul `Rusty' Russell
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/spinlock.h>
11 #include <linux/skbuff.h>
12 #include <linux/if_arp.h>
18 #include <net/route.h>
20 #include <linux/netfilter.h>
21 #include <linux/netfilter_bridge.h>
22 #include <linux/netfilter_ipv6.h>
23 #include <linux/netfilter/xt_LOG.h>
24 #include <net/netfilter/nf_log.h>
26 static const struct nf_loginfo default_loginfo = {
27 .type = NF_LOG_TYPE_LOG,
30 .level = LOGLEVEL_NOTICE,
31 .logflags = NF_LOG_DEFAULT_MASK,
37 unsigned char mac_src[ETH_ALEN];
38 unsigned char ip_src[4];
39 unsigned char mac_dst[ETH_ALEN];
40 unsigned char ip_dst[4];
43 /* Guard against containers flooding syslog. */
44 static bool nf_log_allowed(const struct net *net)
46 return net_eq(net, &init_net) || sysctl_nf_log_all_netns;
49 static void nf_log_dump_vlan(struct nf_log_buf *m, const struct sk_buff *skb)
53 if (!skb_vlan_tag_present(skb))
56 vid = skb_vlan_tag_get(skb);
57 nf_log_buf_add(m, "VPROTO=%04x VID=%u ", ntohs(skb->vlan_proto), vid);
59 static void noinline_for_stack
60 dump_arp_packet(struct nf_log_buf *m,
61 const struct nf_loginfo *info,
62 const struct sk_buff *skb, unsigned int nhoff)
64 const struct arppayload *ap;
65 struct arppayload _arpp;
66 const struct arphdr *ah;
67 unsigned int logflags;
70 ah = skb_header_pointer(skb, nhoff, sizeof(_arph), &_arph);
72 nf_log_buf_add(m, "TRUNCATED");
76 if (info->type == NF_LOG_TYPE_LOG)
77 logflags = info->u.log.logflags;
79 logflags = NF_LOG_DEFAULT_MASK;
81 if (logflags & NF_LOG_MACDECODE) {
82 nf_log_buf_add(m, "MACSRC=%pM MACDST=%pM ",
83 eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest);
84 nf_log_dump_vlan(m, skb);
85 nf_log_buf_add(m, "MACPROTO=%04x ",
86 ntohs(eth_hdr(skb)->h_proto));
89 nf_log_buf_add(m, "ARP HTYPE=%d PTYPE=0x%04x OPCODE=%d",
90 ntohs(ah->ar_hrd), ntohs(ah->ar_pro), ntohs(ah->ar_op));
91 /* If it's for Ethernet and the lengths are OK, then log the ARP
94 if (ah->ar_hrd != htons(ARPHRD_ETHER) ||
95 ah->ar_hln != ETH_ALEN ||
96 ah->ar_pln != sizeof(__be32))
99 ap = skb_header_pointer(skb, nhoff + sizeof(_arph), sizeof(_arpp), &_arpp);
101 nf_log_buf_add(m, " INCOMPLETE [%zu bytes]",
102 skb->len - sizeof(_arph));
105 nf_log_buf_add(m, " MACSRC=%pM IPSRC=%pI4 MACDST=%pM IPDST=%pI4",
106 ap->mac_src, ap->ip_src, ap->mac_dst, ap->ip_dst);
110 nf_log_dump_packet_common(struct nf_log_buf *m, u8 pf,
111 unsigned int hooknum, const struct sk_buff *skb,
112 const struct net_device *in,
113 const struct net_device *out,
114 const struct nf_loginfo *loginfo, const char *prefix)
116 const struct net_device *physoutdev __maybe_unused;
117 const struct net_device *physindev __maybe_unused;
119 nf_log_buf_add(m, KERN_SOH "%c%sIN=%s OUT=%s ",
120 '0' + loginfo->u.log.level, prefix,
122 out ? out->name : "");
123 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
124 physindev = nf_bridge_get_physindev(skb);
125 if (physindev && in != physindev)
126 nf_log_buf_add(m, "PHYSIN=%s ", physindev->name);
127 physoutdev = nf_bridge_get_physoutdev(skb);
128 if (physoutdev && out != physoutdev)
129 nf_log_buf_add(m, "PHYSOUT=%s ", physoutdev->name);
133 static void nf_log_arp_packet(struct net *net, u_int8_t pf,
134 unsigned int hooknum, const struct sk_buff *skb,
135 const struct net_device *in,
136 const struct net_device *out,
137 const struct nf_loginfo *loginfo,
140 struct nf_log_buf *m;
142 if (!nf_log_allowed(net))
145 m = nf_log_buf_open();
148 loginfo = &default_loginfo;
150 nf_log_dump_packet_common(m, pf, hooknum, skb, in, out, loginfo,
152 dump_arp_packet(m, loginfo, skb, skb_network_offset(skb));
157 static struct nf_logger nf_arp_logger __read_mostly = {
158 .name = "nf_log_arp",
159 .type = NF_LOG_TYPE_LOG,
160 .logfn = nf_log_arp_packet,
164 static void nf_log_dump_sk_uid_gid(struct net *net, struct nf_log_buf *m,
167 if (!sk || !sk_fullsock(sk) || !net_eq(net, sock_net(sk)))
170 read_lock_bh(&sk->sk_callback_lock);
171 if (sk->sk_socket && sk->sk_socket->file) {
172 const struct cred *cred = sk->sk_socket->file->f_cred;
174 nf_log_buf_add(m, "UID=%u GID=%u ",
175 from_kuid_munged(&init_user_ns, cred->fsuid),
176 from_kgid_munged(&init_user_ns, cred->fsgid));
178 read_unlock_bh(&sk->sk_callback_lock);
181 static noinline_for_stack int
182 nf_log_dump_tcp_header(struct nf_log_buf *m,
183 const struct sk_buff *skb,
184 u8 proto, int fragment,
186 unsigned int logflags)
189 const struct tcphdr *th;
191 /* Max length: 10 "PROTO=TCP " */
192 nf_log_buf_add(m, "PROTO=TCP ");
197 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
198 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
200 nf_log_buf_add(m, "INCOMPLETE [%u bytes] ", skb->len - offset);
204 /* Max length: 20 "SPT=65535 DPT=65535 " */
205 nf_log_buf_add(m, "SPT=%u DPT=%u ",
206 ntohs(th->source), ntohs(th->dest));
207 /* Max length: 30 "SEQ=4294967295 ACK=4294967295 " */
208 if (logflags & NF_LOG_TCPSEQ) {
209 nf_log_buf_add(m, "SEQ=%u ACK=%u ",
210 ntohl(th->seq), ntohl(th->ack_seq));
213 /* Max length: 13 "WINDOW=65535 " */
214 nf_log_buf_add(m, "WINDOW=%u ", ntohs(th->window));
215 /* Max length: 9 "RES=0x3C " */
216 nf_log_buf_add(m, "RES=0x%02x ", (u_int8_t)(ntohl(tcp_flag_word(th) &
217 TCP_RESERVED_BITS) >> 22));
218 /* Max length: 32 "CWR ECE URG ACK PSH RST SYN FIN " */
220 nf_log_buf_add(m, "CWR ");
222 nf_log_buf_add(m, "ECE ");
224 nf_log_buf_add(m, "URG ");
226 nf_log_buf_add(m, "ACK ");
228 nf_log_buf_add(m, "PSH ");
230 nf_log_buf_add(m, "RST ");
232 nf_log_buf_add(m, "SYN ");
234 nf_log_buf_add(m, "FIN ");
235 /* Max length: 11 "URGP=65535 " */
236 nf_log_buf_add(m, "URGP=%u ", ntohs(th->urg_ptr));
238 if ((logflags & NF_LOG_TCPOPT) && th->doff * 4 > sizeof(struct tcphdr)) {
239 unsigned int optsize = th->doff * 4 - sizeof(struct tcphdr);
240 u8 _opt[60 - sizeof(struct tcphdr)];
244 op = skb_header_pointer(skb, offset + sizeof(struct tcphdr),
247 nf_log_buf_add(m, "OPT (TRUNCATED)");
251 /* Max length: 127 "OPT (" 15*4*2chars ") " */
252 nf_log_buf_add(m, "OPT (");
253 for (i = 0; i < optsize; i++)
254 nf_log_buf_add(m, "%02X", op[i]);
256 nf_log_buf_add(m, ") ");
262 static noinline_for_stack int
263 nf_log_dump_udp_header(struct nf_log_buf *m,
264 const struct sk_buff *skb,
265 u8 proto, int fragment,
269 const struct udphdr *uh;
271 if (proto == IPPROTO_UDP)
272 /* Max length: 10 "PROTO=UDP " */
273 nf_log_buf_add(m, "PROTO=UDP ");
274 else /* Max length: 14 "PROTO=UDPLITE " */
275 nf_log_buf_add(m, "PROTO=UDPLITE ");
280 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
281 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
283 nf_log_buf_add(m, "INCOMPLETE [%u bytes] ", skb->len - offset);
288 /* Max length: 20 "SPT=65535 DPT=65535 " */
289 nf_log_buf_add(m, "SPT=%u DPT=%u LEN=%u ",
290 ntohs(uh->source), ntohs(uh->dest), ntohs(uh->len));
296 /* One level of recursion won't kill us */
297 static noinline_for_stack void
298 dump_ipv4_packet(struct net *net, struct nf_log_buf *m,
299 const struct nf_loginfo *info,
300 const struct sk_buff *skb, unsigned int iphoff)
302 const struct iphdr *ih;
303 unsigned int logflags;
306 if (info->type == NF_LOG_TYPE_LOG)
307 logflags = info->u.log.logflags;
309 logflags = NF_LOG_DEFAULT_MASK;
311 ih = skb_header_pointer(skb, iphoff, sizeof(_iph), &_iph);
313 nf_log_buf_add(m, "TRUNCATED");
318 * TOS, len, DF/MF, fragment offset, TTL, src, dst, options.
319 * Max length: 40 "SRC=255.255.255.255 DST=255.255.255.255 "
321 nf_log_buf_add(m, "SRC=%pI4 DST=%pI4 ", &ih->saddr, &ih->daddr);
323 /* Max length: 46 "LEN=65535 TOS=0xFF PREC=0xFF TTL=255 ID=65535 " */
324 nf_log_buf_add(m, "LEN=%u TOS=0x%02X PREC=0x%02X TTL=%u ID=%u ",
325 ntohs(ih->tot_len), ih->tos & IPTOS_TOS_MASK,
326 ih->tos & IPTOS_PREC_MASK, ih->ttl, ntohs(ih->id));
328 /* Max length: 6 "CE DF MF " */
329 if (ntohs(ih->frag_off) & IP_CE)
330 nf_log_buf_add(m, "CE ");
331 if (ntohs(ih->frag_off) & IP_DF)
332 nf_log_buf_add(m, "DF ");
333 if (ntohs(ih->frag_off) & IP_MF)
334 nf_log_buf_add(m, "MF ");
336 /* Max length: 11 "FRAG:65535 " */
337 if (ntohs(ih->frag_off) & IP_OFFSET)
338 nf_log_buf_add(m, "FRAG:%u ", ntohs(ih->frag_off) & IP_OFFSET);
340 if ((logflags & NF_LOG_IPOPT) &&
341 ih->ihl * 4 > sizeof(struct iphdr)) {
342 unsigned char _opt[4 * 15 - sizeof(struct iphdr)];
343 const unsigned char *op;
344 unsigned int i, optsize;
346 optsize = ih->ihl * 4 - sizeof(struct iphdr);
347 op = skb_header_pointer(skb, iphoff + sizeof(_iph),
350 nf_log_buf_add(m, "TRUNCATED");
354 /* Max length: 127 "OPT (" 15*4*2chars ") " */
355 nf_log_buf_add(m, "OPT (");
356 for (i = 0; i < optsize; i++)
357 nf_log_buf_add(m, "%02X", op[i]);
358 nf_log_buf_add(m, ") ");
361 switch (ih->protocol) {
363 if (nf_log_dump_tcp_header(m, skb, ih->protocol,
364 ntohs(ih->frag_off) & IP_OFFSET,
365 iphoff + ih->ihl * 4, logflags))
369 case IPPROTO_UDPLITE:
370 if (nf_log_dump_udp_header(m, skb, ih->protocol,
371 ntohs(ih->frag_off) & IP_OFFSET,
372 iphoff + ih->ihl * 4))
376 static const size_t required_len[NR_ICMP_TYPES + 1] = {
377 [ICMP_ECHOREPLY] = 4,
378 [ICMP_DEST_UNREACH] = 8 + sizeof(struct iphdr),
379 [ICMP_SOURCE_QUENCH] = 8 + sizeof(struct iphdr),
380 [ICMP_REDIRECT] = 8 + sizeof(struct iphdr),
382 [ICMP_TIME_EXCEEDED] = 8 + sizeof(struct iphdr),
383 [ICMP_PARAMETERPROB] = 8 + sizeof(struct iphdr),
384 [ICMP_TIMESTAMP] = 20,
385 [ICMP_TIMESTAMPREPLY] = 20,
387 [ICMP_ADDRESSREPLY] = 12 };
388 const struct icmphdr *ich;
389 struct icmphdr _icmph;
391 /* Max length: 11 "PROTO=ICMP " */
392 nf_log_buf_add(m, "PROTO=ICMP ");
394 if (ntohs(ih->frag_off) & IP_OFFSET)
397 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
398 ich = skb_header_pointer(skb, iphoff + ih->ihl * 4,
399 sizeof(_icmph), &_icmph);
401 nf_log_buf_add(m, "INCOMPLETE [%u bytes] ",
402 skb->len - iphoff - ih->ihl * 4);
406 /* Max length: 18 "TYPE=255 CODE=255 " */
407 nf_log_buf_add(m, "TYPE=%u CODE=%u ", ich->type, ich->code);
409 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
410 if (ich->type <= NR_ICMP_TYPES &&
411 required_len[ich->type] &&
412 skb->len - iphoff - ih->ihl * 4 < required_len[ich->type]) {
413 nf_log_buf_add(m, "INCOMPLETE [%u bytes] ",
414 skb->len - iphoff - ih->ihl * 4);
421 /* Max length: 19 "ID=65535 SEQ=65535 " */
422 nf_log_buf_add(m, "ID=%u SEQ=%u ",
423 ntohs(ich->un.echo.id),
424 ntohs(ich->un.echo.sequence));
427 case ICMP_PARAMETERPROB:
428 /* Max length: 14 "PARAMETER=255 " */
429 nf_log_buf_add(m, "PARAMETER=%u ",
430 ntohl(ich->un.gateway) >> 24);
433 /* Max length: 24 "GATEWAY=255.255.255.255 " */
434 nf_log_buf_add(m, "GATEWAY=%pI4 ", &ich->un.gateway);
436 case ICMP_DEST_UNREACH:
437 case ICMP_SOURCE_QUENCH:
438 case ICMP_TIME_EXCEEDED:
439 /* Max length: 3+maxlen */
440 if (!iphoff) { /* Only recurse once. */
441 nf_log_buf_add(m, "[");
442 dump_ipv4_packet(net, m, info, skb,
443 iphoff + ih->ihl * 4 + sizeof(_icmph));
444 nf_log_buf_add(m, "] ");
447 /* Max length: 10 "MTU=65535 " */
448 if (ich->type == ICMP_DEST_UNREACH &&
449 ich->code == ICMP_FRAG_NEEDED) {
450 nf_log_buf_add(m, "MTU=%u ",
451 ntohs(ich->un.frag.mtu));
458 const struct ip_auth_hdr *ah;
459 struct ip_auth_hdr _ahdr;
461 if (ntohs(ih->frag_off) & IP_OFFSET)
464 /* Max length: 9 "PROTO=AH " */
465 nf_log_buf_add(m, "PROTO=AH ");
467 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
468 ah = skb_header_pointer(skb, iphoff + ih->ihl * 4,
469 sizeof(_ahdr), &_ahdr);
471 nf_log_buf_add(m, "INCOMPLETE [%u bytes] ",
472 skb->len - iphoff - ih->ihl * 4);
476 /* Length: 15 "SPI=0xF1234567 " */
477 nf_log_buf_add(m, "SPI=0x%x ", ntohl(ah->spi));
481 const struct ip_esp_hdr *eh;
482 struct ip_esp_hdr _esph;
484 /* Max length: 10 "PROTO=ESP " */
485 nf_log_buf_add(m, "PROTO=ESP ");
487 if (ntohs(ih->frag_off) & IP_OFFSET)
490 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
491 eh = skb_header_pointer(skb, iphoff + ih->ihl * 4,
492 sizeof(_esph), &_esph);
494 nf_log_buf_add(m, "INCOMPLETE [%u bytes] ",
495 skb->len - iphoff - ih->ihl * 4);
499 /* Length: 15 "SPI=0xF1234567 " */
500 nf_log_buf_add(m, "SPI=0x%x ", ntohl(eh->spi));
503 /* Max length: 10 "PROTO 255 " */
505 nf_log_buf_add(m, "PROTO=%u ", ih->protocol);
508 /* Max length: 15 "UID=4294967295 " */
509 if ((logflags & NF_LOG_UID) && !iphoff)
510 nf_log_dump_sk_uid_gid(net, m, skb->sk);
512 /* Max length: 16 "MARK=0xFFFFFFFF " */
513 if (!iphoff && skb->mark)
514 nf_log_buf_add(m, "MARK=0x%x ", skb->mark);
516 /* Proto Max log string length */
517 /* IP: 40+46+6+11+127 = 230 */
518 /* TCP: 10+max(25,20+30+13+9+32+11+127) = 252 */
519 /* UDP: 10+max(25,20) = 35 */
520 /* UDPLITE: 14+max(25,20) = 39 */
521 /* ICMP: 11+max(25, 18+25+max(19,14,24+3+n+10,3+n+10)) = 91+n */
522 /* ESP: 10+max(25)+15 = 50 */
523 /* AH: 9+max(25)+15 = 49 */
526 /* (ICMP allows recursion one level deep) */
527 /* maxlen = IP + ICMP + IP + max(TCP,UDP,ICMP,unknown) */
528 /* maxlen = 230+ 91 + 230 + 252 = 803 */
531 static noinline_for_stack void
532 dump_ipv6_packet(struct net *net, struct nf_log_buf *m,
533 const struct nf_loginfo *info,
534 const struct sk_buff *skb, unsigned int ip6hoff,
537 const struct ipv6hdr *ih;
538 unsigned int hdrlen = 0;
539 unsigned int logflags;
540 struct ipv6hdr _ip6h;
545 if (info->type == NF_LOG_TYPE_LOG)
546 logflags = info->u.log.logflags;
548 logflags = NF_LOG_DEFAULT_MASK;
550 ih = skb_header_pointer(skb, ip6hoff, sizeof(_ip6h), &_ip6h);
552 nf_log_buf_add(m, "TRUNCATED");
556 /* Max length: 88 "SRC=0000.0000.0000.0000.0000.0000.0000.0000 DST=0000.0000.0000.0000.0000.0000.0000.0000 " */
557 nf_log_buf_add(m, "SRC=%pI6 DST=%pI6 ", &ih->saddr, &ih->daddr);
559 /* Max length: 44 "LEN=65535 TC=255 HOPLIMIT=255 FLOWLBL=FFFFF " */
560 nf_log_buf_add(m, "LEN=%zu TC=%u HOPLIMIT=%u FLOWLBL=%u ",
561 ntohs(ih->payload_len) + sizeof(struct ipv6hdr),
562 (ntohl(*(__be32 *)ih) & 0x0ff00000) >> 20,
564 (ntohl(*(__be32 *)ih) & 0x000fffff));
567 ptr = ip6hoff + sizeof(struct ipv6hdr);
568 currenthdr = ih->nexthdr;
569 while (currenthdr != NEXTHDR_NONE && nf_ip6_ext_hdr(currenthdr)) {
570 struct ipv6_opt_hdr _hdr;
571 const struct ipv6_opt_hdr *hp;
573 hp = skb_header_pointer(skb, ptr, sizeof(_hdr), &_hdr);
575 nf_log_buf_add(m, "TRUNCATED");
579 /* Max length: 48 "OPT (...) " */
580 if (logflags & NF_LOG_IPOPT)
581 nf_log_buf_add(m, "OPT ( ");
583 switch (currenthdr) {
584 case IPPROTO_FRAGMENT: {
585 struct frag_hdr _fhdr;
586 const struct frag_hdr *fh;
588 nf_log_buf_add(m, "FRAG:");
589 fh = skb_header_pointer(skb, ptr, sizeof(_fhdr),
592 nf_log_buf_add(m, "TRUNCATED ");
596 /* Max length: 6 "65535 " */
597 nf_log_buf_add(m, "%u ", ntohs(fh->frag_off) & 0xFFF8);
599 /* Max length: 11 "INCOMPLETE " */
600 if (fh->frag_off & htons(0x0001))
601 nf_log_buf_add(m, "INCOMPLETE ");
603 nf_log_buf_add(m, "ID:%08x ",
604 ntohl(fh->identification));
606 if (ntohs(fh->frag_off) & 0xFFF8)
612 case IPPROTO_DSTOPTS:
613 case IPPROTO_ROUTING:
614 case IPPROTO_HOPOPTS:
616 if (logflags & NF_LOG_IPOPT)
617 nf_log_buf_add(m, ")");
620 hdrlen = ipv6_optlen(hp);
624 if (logflags & NF_LOG_IPOPT) {
625 struct ip_auth_hdr _ahdr;
626 const struct ip_auth_hdr *ah;
628 /* Max length: 3 "AH " */
629 nf_log_buf_add(m, "AH ");
632 nf_log_buf_add(m, ")");
636 ah = skb_header_pointer(skb, ptr, sizeof(_ahdr),
639 /* Max length: 26 "INCOMPLETE [65535 bytes] )" */
640 nf_log_buf_add(m, "INCOMPLETE [%u bytes] )",
645 /* Length: 15 "SPI=0xF1234567 */
646 nf_log_buf_add(m, "SPI=0x%x ", ntohl(ah->spi));
649 hdrlen = ipv6_authlen(hp);
652 if (logflags & NF_LOG_IPOPT) {
653 struct ip_esp_hdr _esph;
654 const struct ip_esp_hdr *eh;
656 /* Max length: 4 "ESP " */
657 nf_log_buf_add(m, "ESP ");
660 nf_log_buf_add(m, ")");
664 /* Max length: 26 "INCOMPLETE [65535 bytes] )" */
665 eh = skb_header_pointer(skb, ptr, sizeof(_esph),
668 nf_log_buf_add(m, "INCOMPLETE [%u bytes] )",
673 /* Length: 16 "SPI=0xF1234567 )" */
674 nf_log_buf_add(m, "SPI=0x%x )",
679 /* Max length: 20 "Unknown Ext Hdr 255" */
680 nf_log_buf_add(m, "Unknown Ext Hdr %u", currenthdr);
683 if (logflags & NF_LOG_IPOPT)
684 nf_log_buf_add(m, ") ");
686 currenthdr = hp->nexthdr;
690 switch (currenthdr) {
692 if (nf_log_dump_tcp_header(m, skb, currenthdr, fragment,
697 case IPPROTO_UDPLITE:
698 if (nf_log_dump_udp_header(m, skb, currenthdr, fragment, ptr))
701 case IPPROTO_ICMPV6: {
702 struct icmp6hdr _icmp6h;
703 const struct icmp6hdr *ic;
705 /* Max length: 13 "PROTO=ICMPv6 " */
706 nf_log_buf_add(m, "PROTO=ICMPv6 ");
711 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
712 ic = skb_header_pointer(skb, ptr, sizeof(_icmp6h), &_icmp6h);
714 nf_log_buf_add(m, "INCOMPLETE [%u bytes] ",
719 /* Max length: 18 "TYPE=255 CODE=255 " */
720 nf_log_buf_add(m, "TYPE=%u CODE=%u ",
721 ic->icmp6_type, ic->icmp6_code);
723 switch (ic->icmp6_type) {
724 case ICMPV6_ECHO_REQUEST:
725 case ICMPV6_ECHO_REPLY:
726 /* Max length: 19 "ID=65535 SEQ=65535 " */
727 nf_log_buf_add(m, "ID=%u SEQ=%u ",
728 ntohs(ic->icmp6_identifier),
729 ntohs(ic->icmp6_sequence));
731 case ICMPV6_MGM_QUERY:
732 case ICMPV6_MGM_REPORT:
733 case ICMPV6_MGM_REDUCTION:
736 case ICMPV6_PARAMPROB:
737 /* Max length: 17 "POINTER=ffffffff " */
738 nf_log_buf_add(m, "POINTER=%08x ",
739 ntohl(ic->icmp6_pointer));
741 case ICMPV6_DEST_UNREACH:
742 case ICMPV6_PKT_TOOBIG:
743 case ICMPV6_TIME_EXCEED:
744 /* Max length: 3+maxlen */
746 nf_log_buf_add(m, "[");
747 dump_ipv6_packet(net, m, info, skb,
748 ptr + sizeof(_icmp6h), 0);
749 nf_log_buf_add(m, "] ");
752 /* Max length: 10 "MTU=65535 " */
753 if (ic->icmp6_type == ICMPV6_PKT_TOOBIG) {
754 nf_log_buf_add(m, "MTU=%u ",
755 ntohl(ic->icmp6_mtu));
760 /* Max length: 10 "PROTO=255 " */
762 nf_log_buf_add(m, "PROTO=%u ", currenthdr);
765 /* Max length: 15 "UID=4294967295 " */
766 if ((logflags & NF_LOG_UID) && recurse)
767 nf_log_dump_sk_uid_gid(net, m, skb->sk);
769 /* Max length: 16 "MARK=0xFFFFFFFF " */
770 if (recurse && skb->mark)
771 nf_log_buf_add(m, "MARK=0x%x ", skb->mark);
774 static void dump_mac_header(struct nf_log_buf *m,
775 const struct nf_loginfo *info,
776 const struct sk_buff *skb)
778 struct net_device *dev = skb->dev;
779 unsigned int logflags = 0;
781 if (info->type == NF_LOG_TYPE_LOG)
782 logflags = info->u.log.logflags;
784 if (!(logflags & NF_LOG_MACDECODE))
789 nf_log_buf_add(m, "MACSRC=%pM MACDST=%pM ",
790 eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest);
791 nf_log_dump_vlan(m, skb);
792 nf_log_buf_add(m, "MACPROTO=%04x ",
793 ntohs(eth_hdr(skb)->h_proto));
800 nf_log_buf_add(m, "MAC=");
801 if (dev->hard_header_len &&
802 skb->mac_header != skb->network_header) {
803 const unsigned char *p = skb_mac_header(skb);
806 if (dev->type == ARPHRD_SIT) {
814 nf_log_buf_add(m, "%02x", *p++);
815 for (i = 1; i < dev->hard_header_len; i++)
816 nf_log_buf_add(m, ":%02x", *p++);
819 if (dev->type == ARPHRD_SIT) {
820 const struct iphdr *iph =
821 (struct iphdr *)skb_mac_header(skb);
823 nf_log_buf_add(m, " TUNNEL=%pI4->%pI4", &iph->saddr,
827 nf_log_buf_add(m, " ");
830 static void nf_log_ip_packet(struct net *net, u_int8_t pf,
831 unsigned int hooknum, const struct sk_buff *skb,
832 const struct net_device *in,
833 const struct net_device *out,
834 const struct nf_loginfo *loginfo,
837 struct nf_log_buf *m;
839 if (!nf_log_allowed(net))
842 m = nf_log_buf_open();
845 loginfo = &default_loginfo;
847 nf_log_dump_packet_common(m, pf, hooknum, skb, in,
848 out, loginfo, prefix);
851 dump_mac_header(m, loginfo, skb);
853 dump_ipv4_packet(net, m, loginfo, skb, skb_network_offset(skb));
858 static struct nf_logger nf_ip_logger __read_mostly = {
859 .name = "nf_log_ipv4",
860 .type = NF_LOG_TYPE_LOG,
861 .logfn = nf_log_ip_packet,
865 static void nf_log_ip6_packet(struct net *net, u_int8_t pf,
866 unsigned int hooknum, const struct sk_buff *skb,
867 const struct net_device *in,
868 const struct net_device *out,
869 const struct nf_loginfo *loginfo,
872 struct nf_log_buf *m;
874 if (!nf_log_allowed(net))
877 m = nf_log_buf_open();
880 loginfo = &default_loginfo;
882 nf_log_dump_packet_common(m, pf, hooknum, skb, in, out,
886 dump_mac_header(m, loginfo, skb);
888 dump_ipv6_packet(net, m, loginfo, skb, skb_network_offset(skb), 1);
893 static struct nf_logger nf_ip6_logger __read_mostly = {
894 .name = "nf_log_ipv6",
895 .type = NF_LOG_TYPE_LOG,
896 .logfn = nf_log_ip6_packet,
900 static void nf_log_unknown_packet(struct net *net, u_int8_t pf,
901 unsigned int hooknum,
902 const struct sk_buff *skb,
903 const struct net_device *in,
904 const struct net_device *out,
905 const struct nf_loginfo *loginfo,
908 struct nf_log_buf *m;
910 if (!nf_log_allowed(net))
913 m = nf_log_buf_open();
916 loginfo = &default_loginfo;
918 nf_log_dump_packet_common(m, pf, hooknum, skb, in, out, loginfo,
921 dump_mac_header(m, loginfo, skb);
926 static void nf_log_netdev_packet(struct net *net, u_int8_t pf,
927 unsigned int hooknum,
928 const struct sk_buff *skb,
929 const struct net_device *in,
930 const struct net_device *out,
931 const struct nf_loginfo *loginfo,
934 switch (skb->protocol) {
935 case htons(ETH_P_IP):
936 nf_log_ip_packet(net, pf, hooknum, skb, in, out, loginfo, prefix);
938 case htons(ETH_P_IPV6):
939 nf_log_ip6_packet(net, pf, hooknum, skb, in, out, loginfo, prefix);
941 case htons(ETH_P_ARP):
942 case htons(ETH_P_RARP):
943 nf_log_arp_packet(net, pf, hooknum, skb, in, out, loginfo, prefix);
946 nf_log_unknown_packet(net, pf, hooknum, skb,
947 in, out, loginfo, prefix);
952 static struct nf_logger nf_netdev_logger __read_mostly = {
953 .name = "nf_log_netdev",
954 .type = NF_LOG_TYPE_LOG,
955 .logfn = nf_log_netdev_packet,
959 static struct nf_logger nf_bridge_logger __read_mostly = {
960 .name = "nf_log_bridge",
961 .type = NF_LOG_TYPE_LOG,
962 .logfn = nf_log_netdev_packet,
966 static int __net_init nf_log_syslog_net_init(struct net *net)
968 int ret = nf_log_set(net, NFPROTO_IPV4, &nf_ip_logger);
973 ret = nf_log_set(net, NFPROTO_ARP, &nf_arp_logger);
977 ret = nf_log_set(net, NFPROTO_IPV6, &nf_ip6_logger);
981 ret = nf_log_set(net, NFPROTO_NETDEV, &nf_netdev_logger);
985 ret = nf_log_set(net, NFPROTO_BRIDGE, &nf_bridge_logger);
990 nf_log_unset(net, &nf_netdev_logger);
992 nf_log_unset(net, &nf_ip6_logger);
994 nf_log_unset(net, &nf_arp_logger);
996 nf_log_unset(net, &nf_ip_logger);
1000 static void __net_exit nf_log_syslog_net_exit(struct net *net)
1002 nf_log_unset(net, &nf_ip_logger);
1003 nf_log_unset(net, &nf_arp_logger);
1004 nf_log_unset(net, &nf_ip6_logger);
1005 nf_log_unset(net, &nf_netdev_logger);
1006 nf_log_unset(net, &nf_bridge_logger);
1009 static struct pernet_operations nf_log_syslog_net_ops = {
1010 .init = nf_log_syslog_net_init,
1011 .exit = nf_log_syslog_net_exit,
1014 static int __init nf_log_syslog_init(void)
1018 ret = register_pernet_subsys(&nf_log_syslog_net_ops);
1022 ret = nf_log_register(NFPROTO_IPV4, &nf_ip_logger);
1026 ret = nf_log_register(NFPROTO_ARP, &nf_arp_logger);
1030 ret = nf_log_register(NFPROTO_IPV6, &nf_ip6_logger);
1034 ret = nf_log_register(NFPROTO_NETDEV, &nf_netdev_logger);
1038 ret = nf_log_register(NFPROTO_BRIDGE, &nf_bridge_logger);
1044 nf_log_unregister(&nf_netdev_logger);
1046 nf_log_unregister(&nf_ip6_logger);
1048 nf_log_unregister(&nf_arp_logger);
1050 nf_log_unregister(&nf_ip_logger);
1052 pr_err("failed to register logger\n");
1053 unregister_pernet_subsys(&nf_log_syslog_net_ops);
1057 static void __exit nf_log_syslog_exit(void)
1059 unregister_pernet_subsys(&nf_log_syslog_net_ops);
1060 nf_log_unregister(&nf_ip_logger);
1061 nf_log_unregister(&nf_arp_logger);
1062 nf_log_unregister(&nf_ip6_logger);
1063 nf_log_unregister(&nf_netdev_logger);
1064 nf_log_unregister(&nf_bridge_logger);
1067 module_init(nf_log_syslog_init);
1068 module_exit(nf_log_syslog_exit);
1071 MODULE_DESCRIPTION("Netfilter syslog packet logging");
1072 MODULE_LICENSE("GPL");
1073 MODULE_ALIAS("nf_log_arp");
1074 MODULE_ALIAS("nf_log_bridge");
1075 MODULE_ALIAS("nf_log_ipv4");
1076 MODULE_ALIAS("nf_log_ipv6");
1077 MODULE_ALIAS("nf_log_netdev");
1078 MODULE_ALIAS_NF_LOGGER(AF_BRIDGE, 0);
1079 MODULE_ALIAS_NF_LOGGER(AF_INET, 0);
1080 MODULE_ALIAS_NF_LOGGER(3, 0);
1081 MODULE_ALIAS_NF_LOGGER(5, 0); /* NFPROTO_NETDEV */
1082 MODULE_ALIAS_NF_LOGGER(AF_INET6, 0);