dm thin: wake worker when discard is prepared
[linux-2.6.git] / net / netfilter / xt_LOG.c
blobfa40096940a1712be7ac0c517ea5f1d7af4dfd56
1 /*
2 * This is a module which is used for logging packets.
3 */
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>
18 #include <linux/ip.h>
19 #include <net/ipv6.h>
20 #include <net/icmp.h>
21 #include <net/udp.h>
22 #include <net/tcp.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,
34 .u = {
35 .log = {
36 .level = 5,
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)
45 struct udphdr _udph;
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 ");
54 if (fragment)
55 goto out;
57 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
58 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
59 if (uh == NULL) {
60 sb_add(m, "INCOMPLETE [%u bytes] ", skb->len - offset);
62 return 1;
65 /* Max length: 20 "SPT=65535 DPT=65535 " */
66 sb_add(m, "SPT=%u DPT=%u LEN=%u ", ntohs(uh->source), ntohs(uh->dest),
67 ntohs(uh->len));
69 out:
70 return 0;
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)
77 struct tcphdr _tcph;
78 const struct tcphdr *th;
80 /* Max length: 10 "PROTO=TCP " */
81 sb_add(m, "PROTO=TCP ");
83 if (fragment)
84 return 0;
86 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
87 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
88 if (th == NULL) {
89 sb_add(m, "INCOMPLETE [%u bytes] ", skb->len - offset);
90 return 1;
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 " */
105 if (th->cwr)
106 sb_add(m, "CWR ");
107 if (th->ece)
108 sb_add(m, "ECE ");
109 if (th->urg)
110 sb_add(m, "URG ");
111 if (th->ack)
112 sb_add(m, "ACK ");
113 if (th->psh)
114 sb_add(m, "PSH ");
115 if (th->rst)
116 sb_add(m, "RST ");
117 if (th->syn)
118 sb_add(m, "SYN ");
119 if (th->fin)
120 sb_add(m, "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)];
126 const u_int8_t *op;
127 unsigned int i;
128 unsigned int optsize = th->doff*4 - sizeof(struct tcphdr);
130 op = skb_header_pointer(skb, offset + sizeof(struct tcphdr),
131 optsize, _opt);
132 if (op == NULL) {
133 sb_add(m, "OPT (TRUNCATED)");
134 return 1;
137 /* Max length: 127 "OPT (" 15*4*2chars ") " */
138 sb_add(m, "OPT (");
139 for (i = 0; i < optsize; i++)
140 sb_add(m, "%02X", op[i]);
142 sb_add(m, ") ");
145 return 0;
148 static void dump_sk_uid_gid(struct sbuff *m, struct sock *sk)
150 if (!sk || sk->sk_state == TCP_TIME_WAIT)
151 return;
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,
167 unsigned int iphoff)
169 struct iphdr _iph;
170 const struct iphdr *ih;
171 unsigned int logflags;
173 if (info->type == NF_LOG_TYPE_LOG)
174 logflags = info->u.log.logflags;
175 else
176 logflags = NF_LOG_MASK;
178 ih = skb_header_pointer(skb, iphoff, sizeof(_iph), &_iph);
179 if (ih == NULL) {
180 sb_add(m, "TRUNCATED");
181 return;
184 /* Important fields:
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)
197 sb_add(m, "CE ");
198 if (ntohs(ih->frag_off) & IP_DF)
199 sb_add(m, "DF ");
200 if (ntohs(ih->frag_off) & IP_MF)
201 sb_add(m, "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),
215 optsize, _opt);
216 if (op == NULL) {
217 sb_add(m, "TRUNCATED");
218 return;
221 /* Max length: 127 "OPT (" 15*4*2chars ") " */
222 sb_add(m, "OPT (");
223 for (i = 0; i < optsize; i++)
224 sb_add(m, "%02X", op[i]);
225 sb_add(m, ") ");
228 switch (ih->protocol) {
229 case IPPROTO_TCP:
230 if (dump_tcp_header(m, skb, ih->protocol,
231 ntohs(ih->frag_off) & IP_OFFSET,
232 iphoff+ih->ihl*4, logflags))
233 return;
234 break;
235 case IPPROTO_UDP:
236 case IPPROTO_UDPLITE:
237 if (dump_udp_header(m, skb, ih->protocol,
238 ntohs(ih->frag_off) & IP_OFFSET,
239 iphoff+ih->ihl*4))
240 return;
241 break;
242 case IPPROTO_ICMP: {
243 struct icmphdr _icmph;
244 const struct icmphdr *ich;
245 static const size_t required_len[NR_ICMP_TYPES+1]
246 = { [ICMP_ECHOREPLY] = 4,
247 [ICMP_DEST_UNREACH]
248 = 8 + sizeof(struct iphdr),
249 [ICMP_SOURCE_QUENCH]
250 = 8 + sizeof(struct iphdr),
251 [ICMP_REDIRECT]
252 = 8 + sizeof(struct iphdr),
253 [ICMP_ECHO] = 4,
254 [ICMP_TIME_EXCEEDED]
255 = 8 + sizeof(struct iphdr),
256 [ICMP_PARAMETERPROB]
257 = 8 + sizeof(struct iphdr),
258 [ICMP_TIMESTAMP] = 20,
259 [ICMP_TIMESTAMPREPLY] = 20,
260 [ICMP_ADDRESS] = 12,
261 [ICMP_ADDRESSREPLY] = 12 };
263 /* Max length: 11 "PROTO=ICMP " */
264 sb_add(m, "PROTO=ICMP ");
266 if (ntohs(ih->frag_off) & IP_OFFSET)
267 break;
269 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
270 ich = skb_header_pointer(skb, iphoff + ih->ihl * 4,
271 sizeof(_icmph), &_icmph);
272 if (ich == NULL) {
273 sb_add(m, "INCOMPLETE [%u bytes] ",
274 skb->len - iphoff - ih->ihl*4);
275 break;
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);
287 break;
290 switch (ich->type) {
291 case ICMP_ECHOREPLY:
292 case ICMP_ECHO:
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));
297 break;
299 case ICMP_PARAMETERPROB:
300 /* Max length: 14 "PARAMETER=255 " */
301 sb_add(m, "PARAMETER=%u ",
302 ntohl(ich->un.gateway) >> 24);
303 break;
304 case ICMP_REDIRECT:
305 /* Max length: 24 "GATEWAY=255.255.255.255 " */
306 sb_add(m, "GATEWAY=%pI4 ", &ich->un.gateway);
307 /* Fall through */
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. */
313 sb_add(m, "[");
314 dump_ipv4_packet(m, info, skb,
315 iphoff + ih->ihl*4+sizeof(_icmph));
316 sb_add(m, "] ");
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));
324 break;
326 /* Max Length */
327 case IPPROTO_AH: {
328 struct ip_auth_hdr _ahdr;
329 const struct ip_auth_hdr *ah;
331 if (ntohs(ih->frag_off) & IP_OFFSET)
332 break;
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);
340 if (ah == NULL) {
341 sb_add(m, "INCOMPLETE [%u bytes] ",
342 skb->len - iphoff - ih->ihl*4);
343 break;
346 /* Length: 15 "SPI=0xF1234567 " */
347 sb_add(m, "SPI=0x%x ", ntohl(ah->spi));
348 break;
350 case IPPROTO_ESP: {
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)
358 break;
360 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
361 eh = skb_header_pointer(skb, iphoff+ih->ihl*4,
362 sizeof(_esph), &_esph);
363 if (eh == NULL) {
364 sb_add(m, "INCOMPLETE [%u bytes] ",
365 skb->len - iphoff - ih->ihl*4);
366 break;
369 /* Length: 15 "SPI=0xF1234567 " */
370 sb_add(m, "SPI=0x%x ", ntohl(eh->spi));
371 break;
373 /* Max length: 10 "PROTO 255 " */
374 default:
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 */
394 /* unknown: 10 */
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))
412 goto fallback;
414 switch (dev->type) {
415 case ARPHRD_ETHER:
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));
419 return;
420 default:
421 break;
424 fallback:
425 sb_add(m, "MAC=");
426 if (dev->hard_header_len &&
427 skb->mac_header != skb->network_header) {
428 const unsigned char *p = skb_mac_header(skb);
429 unsigned int i;
431 sb_add(m, "%02x", *p++);
432 for (i = 1; i < dev->hard_header_len; i++, p++)
433 sb_add(m, ":%02x", *p);
435 sb_add(m, " ");
438 static void
439 log_packet_common(struct sbuff *m,
440 u_int8_t pf,
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,
446 const char *prefix)
448 sb_add(m, KERN_SOH "%c%sIN=%s OUT=%s ",
449 '0' + loginfo->u.log.level, prefix,
450 in ? in->name : "",
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);
464 #endif
468 static void
469 ipt_log_packet(u_int8_t pf,
470 unsigned int hooknum,
471 const struct sk_buff *skb,
472 const struct net_device *in,
473 const struct net_device *out,
474 const struct nf_loginfo *loginfo,
475 const char *prefix)
477 struct sbuff *m = sb_open();
479 if (!loginfo)
480 loginfo = &default_loginfo;
482 log_packet_common(m, pf, hooknum, skb, in, out, loginfo, prefix);
484 if (in != NULL)
485 dump_ipv4_mac_header(m, loginfo, skb);
487 dump_ipv4_packet(m, loginfo, skb, 0);
489 sb_close(m);
492 #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
493 /* One level of recursion won't kill us */
494 static void dump_ipv6_packet(struct sbuff *m,
495 const struct nf_loginfo *info,
496 const struct sk_buff *skb, unsigned int ip6hoff,
497 int recurse)
499 u_int8_t currenthdr;
500 int fragment;
501 struct ipv6hdr _ip6h;
502 const struct ipv6hdr *ih;
503 unsigned int ptr;
504 unsigned int hdrlen = 0;
505 unsigned int logflags;
507 if (info->type == NF_LOG_TYPE_LOG)
508 logflags = info->u.log.logflags;
509 else
510 logflags = NF_LOG_MASK;
512 ih = skb_header_pointer(skb, ip6hoff, sizeof(_ip6h), &_ip6h);
513 if (ih == NULL) {
514 sb_add(m, "TRUNCATED");
515 return;
518 /* Max length: 88 "SRC=0000.0000.0000.0000.0000.0000.0000.0000 DST=0000.0000.0000.0000.0000.0000.0000.0000 " */
519 sb_add(m, "SRC=%pI6 DST=%pI6 ", &ih->saddr, &ih->daddr);
521 /* Max length: 44 "LEN=65535 TC=255 HOPLIMIT=255 FLOWLBL=FFFFF " */
522 sb_add(m, "LEN=%Zu TC=%u HOPLIMIT=%u FLOWLBL=%u ",
523 ntohs(ih->payload_len) + sizeof(struct ipv6hdr),
524 (ntohl(*(__be32 *)ih) & 0x0ff00000) >> 20,
525 ih->hop_limit,
526 (ntohl(*(__be32 *)ih) & 0x000fffff));
528 fragment = 0;
529 ptr = ip6hoff + sizeof(struct ipv6hdr);
530 currenthdr = ih->nexthdr;
531 while (currenthdr != NEXTHDR_NONE && ip6t_ext_hdr(currenthdr)) {
532 struct ipv6_opt_hdr _hdr;
533 const struct ipv6_opt_hdr *hp;
535 hp = skb_header_pointer(skb, ptr, sizeof(_hdr), &_hdr);
536 if (hp == NULL) {
537 sb_add(m, "TRUNCATED");
538 return;
541 /* Max length: 48 "OPT (...) " */
542 if (logflags & XT_LOG_IPOPT)
543 sb_add(m, "OPT ( ");
545 switch (currenthdr) {
546 case IPPROTO_FRAGMENT: {
547 struct frag_hdr _fhdr;
548 const struct frag_hdr *fh;
550 sb_add(m, "FRAG:");
551 fh = skb_header_pointer(skb, ptr, sizeof(_fhdr),
552 &_fhdr);
553 if (fh == NULL) {
554 sb_add(m, "TRUNCATED ");
555 return;
558 /* Max length: 6 "65535 " */
559 sb_add(m, "%u ", ntohs(fh->frag_off) & 0xFFF8);
561 /* Max length: 11 "INCOMPLETE " */
562 if (fh->frag_off & htons(0x0001))
563 sb_add(m, "INCOMPLETE ");
565 sb_add(m, "ID:%08x ", ntohl(fh->identification));
567 if (ntohs(fh->frag_off) & 0xFFF8)
568 fragment = 1;
570 hdrlen = 8;
572 break;
574 case IPPROTO_DSTOPTS:
575 case IPPROTO_ROUTING:
576 case IPPROTO_HOPOPTS:
577 if (fragment) {
578 if (logflags & XT_LOG_IPOPT)
579 sb_add(m, ")");
580 return;
582 hdrlen = ipv6_optlen(hp);
583 break;
584 /* Max Length */
585 case IPPROTO_AH:
586 if (logflags & XT_LOG_IPOPT) {
587 struct ip_auth_hdr _ahdr;
588 const struct ip_auth_hdr *ah;
590 /* Max length: 3 "AH " */
591 sb_add(m, "AH ");
593 if (fragment) {
594 sb_add(m, ")");
595 return;
598 ah = skb_header_pointer(skb, ptr, sizeof(_ahdr),
599 &_ahdr);
600 if (ah == NULL) {
602 * Max length: 26 "INCOMPLETE [65535
603 * bytes] )"
605 sb_add(m, "INCOMPLETE [%u bytes] )",
606 skb->len - ptr);
607 return;
610 /* Length: 15 "SPI=0xF1234567 */
611 sb_add(m, "SPI=0x%x ", ntohl(ah->spi));
615 hdrlen = (hp->hdrlen+2)<<2;
616 break;
617 case IPPROTO_ESP:
618 if (logflags & XT_LOG_IPOPT) {
619 struct ip_esp_hdr _esph;
620 const struct ip_esp_hdr *eh;
622 /* Max length: 4 "ESP " */
623 sb_add(m, "ESP ");
625 if (fragment) {
626 sb_add(m, ")");
627 return;
631 * Max length: 26 "INCOMPLETE [65535 bytes] )"
633 eh = skb_header_pointer(skb, ptr, sizeof(_esph),
634 &_esph);
635 if (eh == NULL) {
636 sb_add(m, "INCOMPLETE [%u bytes] )",
637 skb->len - ptr);
638 return;
641 /* Length: 16 "SPI=0xF1234567 )" */
642 sb_add(m, "SPI=0x%x )", ntohl(eh->spi));
645 return;
646 default:
647 /* Max length: 20 "Unknown Ext Hdr 255" */
648 sb_add(m, "Unknown Ext Hdr %u", currenthdr);
649 return;
651 if (logflags & XT_LOG_IPOPT)
652 sb_add(m, ") ");
654 currenthdr = hp->nexthdr;
655 ptr += hdrlen;
658 switch (currenthdr) {
659 case IPPROTO_TCP:
660 if (dump_tcp_header(m, skb, currenthdr, fragment, ptr,
661 logflags))
662 return;
663 break;
664 case IPPROTO_UDP:
665 case IPPROTO_UDPLITE:
666 if (dump_udp_header(m, skb, currenthdr, fragment, ptr))
667 return;
668 break;
669 case IPPROTO_ICMPV6: {
670 struct icmp6hdr _icmp6h;
671 const struct icmp6hdr *ic;
673 /* Max length: 13 "PROTO=ICMPv6 " */
674 sb_add(m, "PROTO=ICMPv6 ");
676 if (fragment)
677 break;
679 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
680 ic = skb_header_pointer(skb, ptr, sizeof(_icmp6h), &_icmp6h);
681 if (ic == NULL) {
682 sb_add(m, "INCOMPLETE [%u bytes] ", skb->len - ptr);
683 return;
686 /* Max length: 18 "TYPE=255 CODE=255 " */
687 sb_add(m, "TYPE=%u CODE=%u ", ic->icmp6_type, ic->icmp6_code);
689 switch (ic->icmp6_type) {
690 case ICMPV6_ECHO_REQUEST:
691 case ICMPV6_ECHO_REPLY:
692 /* Max length: 19 "ID=65535 SEQ=65535 " */
693 sb_add(m, "ID=%u SEQ=%u ",
694 ntohs(ic->icmp6_identifier),
695 ntohs(ic->icmp6_sequence));
696 break;
697 case ICMPV6_MGM_QUERY:
698 case ICMPV6_MGM_REPORT:
699 case ICMPV6_MGM_REDUCTION:
700 break;
702 case ICMPV6_PARAMPROB:
703 /* Max length: 17 "POINTER=ffffffff " */
704 sb_add(m, "POINTER=%08x ", ntohl(ic->icmp6_pointer));
705 /* Fall through */
706 case ICMPV6_DEST_UNREACH:
707 case ICMPV6_PKT_TOOBIG:
708 case ICMPV6_TIME_EXCEED:
709 /* Max length: 3+maxlen */
710 if (recurse) {
711 sb_add(m, "[");
712 dump_ipv6_packet(m, info, skb,
713 ptr + sizeof(_icmp6h), 0);
714 sb_add(m, "] ");
717 /* Max length: 10 "MTU=65535 " */
718 if (ic->icmp6_type == ICMPV6_PKT_TOOBIG)
719 sb_add(m, "MTU=%u ", ntohl(ic->icmp6_mtu));
721 break;
723 /* Max length: 10 "PROTO=255 " */
724 default:
725 sb_add(m, "PROTO=%u ", currenthdr);
728 /* Max length: 15 "UID=4294967295 " */
729 if ((logflags & XT_LOG_UID) && recurse)
730 dump_sk_uid_gid(m, skb->sk);
732 /* Max length: 16 "MARK=0xFFFFFFFF " */
733 if (!recurse && skb->mark)
734 sb_add(m, "MARK=0x%x ", skb->mark);
737 static void dump_ipv6_mac_header(struct sbuff *m,
738 const struct nf_loginfo *info,
739 const struct sk_buff *skb)
741 struct net_device *dev = skb->dev;
742 unsigned int logflags = 0;
744 if (info->type == NF_LOG_TYPE_LOG)
745 logflags = info->u.log.logflags;
747 if (!(logflags & XT_LOG_MACDECODE))
748 goto fallback;
750 switch (dev->type) {
751 case ARPHRD_ETHER:
752 sb_add(m, "MACSRC=%pM MACDST=%pM MACPROTO=%04x ",
753 eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
754 ntohs(eth_hdr(skb)->h_proto));
755 return;
756 default:
757 break;
760 fallback:
761 sb_add(m, "MAC=");
762 if (dev->hard_header_len &&
763 skb->mac_header != skb->network_header) {
764 const unsigned char *p = skb_mac_header(skb);
765 unsigned int len = dev->hard_header_len;
766 unsigned int i;
768 if (dev->type == ARPHRD_SIT) {
769 p -= ETH_HLEN;
771 if (p < skb->head)
772 p = NULL;
775 if (p != NULL) {
776 sb_add(m, "%02x", *p++);
777 for (i = 1; i < len; i++)
778 sb_add(m, ":%02x", *p++);
780 sb_add(m, " ");
782 if (dev->type == ARPHRD_SIT) {
783 const struct iphdr *iph =
784 (struct iphdr *)skb_mac_header(skb);
785 sb_add(m, "TUNNEL=%pI4->%pI4 ", &iph->saddr,
786 &iph->daddr);
788 } else
789 sb_add(m, " ");
792 static void
793 ip6t_log_packet(u_int8_t pf,
794 unsigned int hooknum,
795 const struct sk_buff *skb,
796 const struct net_device *in,
797 const struct net_device *out,
798 const struct nf_loginfo *loginfo,
799 const char *prefix)
801 struct sbuff *m = sb_open();
803 if (!loginfo)
804 loginfo = &default_loginfo;
806 log_packet_common(m, pf, hooknum, skb, in, out, loginfo, prefix);
808 if (in != NULL)
809 dump_ipv6_mac_header(m, loginfo, skb);
811 dump_ipv6_packet(m, loginfo, skb, skb_network_offset(skb), 1);
813 sb_close(m);
815 #endif
817 static unsigned int
818 log_tg(struct sk_buff *skb, const struct xt_action_param *par)
820 const struct xt_log_info *loginfo = par->targinfo;
821 struct nf_loginfo li;
823 li.type = NF_LOG_TYPE_LOG;
824 li.u.log.level = loginfo->level;
825 li.u.log.logflags = loginfo->logflags;
827 if (par->family == NFPROTO_IPV4)
828 ipt_log_packet(NFPROTO_IPV4, par->hooknum, skb, par->in,
829 par->out, &li, loginfo->prefix);
830 #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
831 else if (par->family == NFPROTO_IPV6)
832 ip6t_log_packet(NFPROTO_IPV6, par->hooknum, skb, par->in,
833 par->out, &li, loginfo->prefix);
834 #endif
835 else
836 WARN_ON_ONCE(1);
838 return XT_CONTINUE;
841 static int log_tg_check(const struct xt_tgchk_param *par)
843 const struct xt_log_info *loginfo = par->targinfo;
845 if (par->family != NFPROTO_IPV4 && par->family != NFPROTO_IPV6)
846 return -EINVAL;
848 if (loginfo->level >= 8) {
849 pr_debug("level %u >= 8\n", loginfo->level);
850 return -EINVAL;
853 if (loginfo->prefix[sizeof(loginfo->prefix)-1] != '\0') {
854 pr_debug("prefix is not null-terminated\n");
855 return -EINVAL;
858 return 0;
861 static struct xt_target log_tg_regs[] __read_mostly = {
863 .name = "LOG",
864 .family = NFPROTO_IPV4,
865 .target = log_tg,
866 .targetsize = sizeof(struct xt_log_info),
867 .checkentry = log_tg_check,
868 .me = THIS_MODULE,
870 #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
872 .name = "LOG",
873 .family = NFPROTO_IPV6,
874 .target = log_tg,
875 .targetsize = sizeof(struct xt_log_info),
876 .checkentry = log_tg_check,
877 .me = THIS_MODULE,
879 #endif
882 static struct nf_logger ipt_log_logger __read_mostly = {
883 .name = "ipt_LOG",
884 .logfn = &ipt_log_packet,
885 .me = THIS_MODULE,
888 #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
889 static struct nf_logger ip6t_log_logger __read_mostly = {
890 .name = "ip6t_LOG",
891 .logfn = &ip6t_log_packet,
892 .me = THIS_MODULE,
894 #endif
896 static int __init log_tg_init(void)
898 int ret;
900 ret = xt_register_targets(log_tg_regs, ARRAY_SIZE(log_tg_regs));
901 if (ret < 0)
902 return ret;
904 nf_log_register(NFPROTO_IPV4, &ipt_log_logger);
905 #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
906 nf_log_register(NFPROTO_IPV6, &ip6t_log_logger);
907 #endif
908 return 0;
911 static void __exit log_tg_exit(void)
913 nf_log_unregister(&ipt_log_logger);
914 #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
915 nf_log_unregister(&ip6t_log_logger);
916 #endif
917 xt_unregister_targets(log_tg_regs, ARRAY_SIZE(log_tg_regs));
920 module_init(log_tg_init);
921 module_exit(log_tg_exit);
923 MODULE_LICENSE("GPL");
924 MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
925 MODULE_AUTHOR("Jan Rekorajski <baggins@pld.org.pl>");
926 MODULE_DESCRIPTION("Xtables: IPv4/IPv6 packet logging");
927 MODULE_ALIAS("ipt_LOG");
928 MODULE_ALIAS("ip6t_LOG");