netfilter: nf_log: move log buffering to core logging
[cascardo/linux.git] / net / netfilter / xt_LOG.c
1 /*
2  * This is a module which is used for logging packets.
3  */
4
5 /* (C) 1999-2001 Paul `Rusty' Russell
6  * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
7  *
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.
11  */
12
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>
24
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
31 static struct nf_loginfo default_loginfo = {
32         .type   = NF_LOG_TYPE_LOG,
33         .u = {
34                 .log = {
35                         .level    = 5,
36                         .logflags = NF_LOG_MASK,
37                 },
38         },
39 };
40
41 static int dump_udp_header(struct nf_log_buf *m, const struct sk_buff *skb,
42                            u8 proto, int fragment, unsigned int offset)
43 {
44         struct udphdr _udph;
45         const struct udphdr *uh;
46
47         if (proto == IPPROTO_UDP)
48                 /* Max length: 10 "PROTO=UDP "     */
49                 nf_log_buf_add(m, "PROTO=UDP ");
50         else    /* Max length: 14 "PROTO=UDPLITE " */
51                 nf_log_buf_add(m, "PROTO=UDPLITE ");
52
53         if (fragment)
54                 goto out;
55
56         /* Max length: 25 "INCOMPLETE [65535 bytes] " */
57         uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph);
58         if (uh == NULL) {
59                 nf_log_buf_add(m, "INCOMPLETE [%u bytes] ", skb->len - offset);
60
61                 return 1;
62         }
63
64         /* Max length: 20 "SPT=65535 DPT=65535 " */
65         nf_log_buf_add(m, "SPT=%u DPT=%u LEN=%u ",
66                        ntohs(uh->source), ntohs(uh->dest), ntohs(uh->len));
67
68 out:
69         return 0;
70 }
71
72 static int dump_tcp_header(struct nf_log_buf *m, const struct sk_buff *skb,
73                            u8 proto, int fragment, unsigned int offset,
74                            unsigned int logflags)
75 {
76         struct tcphdr _tcph;
77         const struct tcphdr *th;
78
79         /* Max length: 10 "PROTO=TCP " */
80         nf_log_buf_add(m, "PROTO=TCP ");
81
82         if (fragment)
83                 return 0;
84
85         /* Max length: 25 "INCOMPLETE [65535 bytes] " */
86         th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph);
87         if (th == NULL) {
88                 nf_log_buf_add(m, "INCOMPLETE [%u bytes] ", skb->len - offset);
89                 return 1;
90         }
91
92         /* Max length: 20 "SPT=65535 DPT=65535 " */
93         nf_log_buf_add(m, "SPT=%u DPT=%u ",
94                        ntohs(th->source), ntohs(th->dest));
95         /* Max length: 30 "SEQ=4294967295 ACK=4294967295 " */
96         if (logflags & XT_LOG_TCPSEQ) {
97                 nf_log_buf_add(m, "SEQ=%u ACK=%u ",
98                                ntohl(th->seq), ntohl(th->ack_seq));
99         }
100
101         /* Max length: 13 "WINDOW=65535 " */
102         nf_log_buf_add(m, "WINDOW=%u ", ntohs(th->window));
103         /* Max length: 9 "RES=0x3C " */
104         nf_log_buf_add(m, "RES=0x%02x ", (u_int8_t)(ntohl(tcp_flag_word(th) &
105                                             TCP_RESERVED_BITS) >> 22));
106         /* Max length: 32 "CWR ECE URG ACK PSH RST SYN FIN " */
107         if (th->cwr)
108                 nf_log_buf_add(m, "CWR ");
109         if (th->ece)
110                 nf_log_buf_add(m, "ECE ");
111         if (th->urg)
112                 nf_log_buf_add(m, "URG ");
113         if (th->ack)
114                 nf_log_buf_add(m, "ACK ");
115         if (th->psh)
116                 nf_log_buf_add(m, "PSH ");
117         if (th->rst)
118                 nf_log_buf_add(m, "RST ");
119         if (th->syn)
120                 nf_log_buf_add(m, "SYN ");
121         if (th->fin)
122                 nf_log_buf_add(m, "FIN ");
123         /* Max length: 11 "URGP=65535 " */
124         nf_log_buf_add(m, "URGP=%u ", ntohs(th->urg_ptr));
125
126         if ((logflags & XT_LOG_TCPOPT) && th->doff*4 > sizeof(struct tcphdr)) {
127                 u_int8_t _opt[60 - sizeof(struct tcphdr)];
128                 const u_int8_t *op;
129                 unsigned int i;
130                 unsigned int optsize = th->doff*4 - sizeof(struct tcphdr);
131
132                 op = skb_header_pointer(skb, offset + sizeof(struct tcphdr),
133                                         optsize, _opt);
134                 if (op == NULL) {
135                         nf_log_buf_add(m, "OPT (TRUNCATED)");
136                         return 1;
137                 }
138
139                 /* Max length: 127 "OPT (" 15*4*2chars ") " */
140                 nf_log_buf_add(m, "OPT (");
141                 for (i = 0; i < optsize; i++)
142                         nf_log_buf_add(m, "%02X", op[i]);
143
144                 nf_log_buf_add(m, ") ");
145         }
146
147         return 0;
148 }
149
150 static void dump_sk_uid_gid(struct nf_log_buf *m, struct sock *sk)
151 {
152         if (!sk || sk->sk_state == TCP_TIME_WAIT)
153                 return;
154
155         read_lock_bh(&sk->sk_callback_lock);
156         if (sk->sk_socket && sk->sk_socket->file) {
157                 const struct cred *cred = sk->sk_socket->file->f_cred;
158                 nf_log_buf_add(m, "UID=%u GID=%u ",
159                         from_kuid_munged(&init_user_ns, cred->fsuid),
160                         from_kgid_munged(&init_user_ns, cred->fsgid));
161         }
162         read_unlock_bh(&sk->sk_callback_lock);
163 }
164
165 /* One level of recursion won't kill us */
166 static void dump_ipv4_packet(struct nf_log_buf *m,
167                              const struct nf_loginfo *info,
168                              const struct sk_buff *skb, unsigned int iphoff)
169 {
170         struct iphdr _iph;
171         const struct iphdr *ih;
172         unsigned int logflags;
173
174         if (info->type == NF_LOG_TYPE_LOG)
175                 logflags = info->u.log.logflags;
176         else
177                 logflags = NF_LOG_MASK;
178
179         ih = skb_header_pointer(skb, iphoff, sizeof(_iph), &_iph);
180         if (ih == NULL) {
181                 nf_log_buf_add(m, "TRUNCATED");
182                 return;
183         }
184
185         /* Important fields:
186          * TOS, len, DF/MF, fragment offset, TTL, src, dst, options. */
187         /* Max length: 40 "SRC=255.255.255.255 DST=255.255.255.255 " */
188         nf_log_buf_add(m, "SRC=%pI4 DST=%pI4 ",
189                &ih->saddr, &ih->daddr);
190
191         /* Max length: 46 "LEN=65535 TOS=0xFF PREC=0xFF TTL=255 ID=65535 " */
192         nf_log_buf_add(m, "LEN=%u TOS=0x%02X PREC=0x%02X TTL=%u ID=%u ",
193                ntohs(ih->tot_len), ih->tos & IPTOS_TOS_MASK,
194                ih->tos & IPTOS_PREC_MASK, ih->ttl, ntohs(ih->id));
195
196         /* Max length: 6 "CE DF MF " */
197         if (ntohs(ih->frag_off) & IP_CE)
198                 nf_log_buf_add(m, "CE ");
199         if (ntohs(ih->frag_off) & IP_DF)
200                 nf_log_buf_add(m, "DF ");
201         if (ntohs(ih->frag_off) & IP_MF)
202                 nf_log_buf_add(m, "MF ");
203
204         /* Max length: 11 "FRAG:65535 " */
205         if (ntohs(ih->frag_off) & IP_OFFSET)
206                 nf_log_buf_add(m, "FRAG:%u ", ntohs(ih->frag_off) & IP_OFFSET);
207
208         if ((logflags & XT_LOG_IPOPT) &&
209             ih->ihl * 4 > sizeof(struct iphdr)) {
210                 const unsigned char *op;
211                 unsigned char _opt[4 * 15 - sizeof(struct iphdr)];
212                 unsigned int i, optsize;
213
214                 optsize = ih->ihl * 4 - sizeof(struct iphdr);
215                 op = skb_header_pointer(skb, iphoff+sizeof(_iph),
216                                         optsize, _opt);
217                 if (op == NULL) {
218                         nf_log_buf_add(m, "TRUNCATED");
219                         return;
220                 }
221
222                 /* Max length: 127 "OPT (" 15*4*2chars ") " */
223                 nf_log_buf_add(m, "OPT (");
224                 for (i = 0; i < optsize; i++)
225                         nf_log_buf_add(m, "%02X", op[i]);
226                 nf_log_buf_add(m, ") ");
227         }
228
229         switch (ih->protocol) {
230         case IPPROTO_TCP:
231                 if (dump_tcp_header(m, skb, ih->protocol,
232                                     ntohs(ih->frag_off) & IP_OFFSET,
233                                     iphoff+ih->ihl*4, logflags))
234                         return;
235                 break;
236         case IPPROTO_UDP:
237         case IPPROTO_UDPLITE:
238                 if (dump_udp_header(m, skb, ih->protocol,
239                                     ntohs(ih->frag_off) & IP_OFFSET,
240                                     iphoff+ih->ihl*4))
241                         return;
242                 break;
243         case IPPROTO_ICMP: {
244                 struct icmphdr _icmph;
245                 const struct icmphdr *ich;
246                 static const size_t required_len[NR_ICMP_TYPES+1]
247                         = { [ICMP_ECHOREPLY] = 4,
248                             [ICMP_DEST_UNREACH]
249                             = 8 + sizeof(struct iphdr),
250                             [ICMP_SOURCE_QUENCH]
251                             = 8 + sizeof(struct iphdr),
252                             [ICMP_REDIRECT]
253                             = 8 + sizeof(struct iphdr),
254                             [ICMP_ECHO] = 4,
255                             [ICMP_TIME_EXCEEDED]
256                             = 8 + sizeof(struct iphdr),
257                             [ICMP_PARAMETERPROB]
258                             = 8 + sizeof(struct iphdr),
259                             [ICMP_TIMESTAMP] = 20,
260                             [ICMP_TIMESTAMPREPLY] = 20,
261                             [ICMP_ADDRESS] = 12,
262                             [ICMP_ADDRESSREPLY] = 12 };
263
264                 /* Max length: 11 "PROTO=ICMP " */
265                 nf_log_buf_add(m, "PROTO=ICMP ");
266
267                 if (ntohs(ih->frag_off) & IP_OFFSET)
268                         break;
269
270                 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
271                 ich = skb_header_pointer(skb, iphoff + ih->ihl * 4,
272                                          sizeof(_icmph), &_icmph);
273                 if (ich == NULL) {
274                         nf_log_buf_add(m, "INCOMPLETE [%u bytes] ",
275                                        skb->len - iphoff - ih->ihl*4);
276                         break;
277                 }
278
279                 /* Max length: 18 "TYPE=255 CODE=255 " */
280                 nf_log_buf_add(m, "TYPE=%u CODE=%u ", ich->type, ich->code);
281
282                 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
283                 if (ich->type <= NR_ICMP_TYPES &&
284                     required_len[ich->type] &&
285                     skb->len-iphoff-ih->ihl*4 < required_len[ich->type]) {
286                         nf_log_buf_add(m, "INCOMPLETE [%u bytes] ",
287                                skb->len - iphoff - ih->ihl*4);
288                         break;
289                 }
290
291                 switch (ich->type) {
292                 case ICMP_ECHOREPLY:
293                 case ICMP_ECHO:
294                         /* Max length: 19 "ID=65535 SEQ=65535 " */
295                         nf_log_buf_add(m, "ID=%u SEQ=%u ",
296                                ntohs(ich->un.echo.id),
297                                ntohs(ich->un.echo.sequence));
298                         break;
299
300                 case ICMP_PARAMETERPROB:
301                         /* Max length: 14 "PARAMETER=255 " */
302                         nf_log_buf_add(m, "PARAMETER=%u ",
303                                ntohl(ich->un.gateway) >> 24);
304                         break;
305                 case ICMP_REDIRECT:
306                         /* Max length: 24 "GATEWAY=255.255.255.255 " */
307                         nf_log_buf_add(m, "GATEWAY=%pI4 ", &ich->un.gateway);
308                         /* Fall through */
309                 case ICMP_DEST_UNREACH:
310                 case ICMP_SOURCE_QUENCH:
311                 case ICMP_TIME_EXCEEDED:
312                         /* Max length: 3+maxlen */
313                         if (!iphoff) { /* Only recurse once. */
314                                 nf_log_buf_add(m, "[");
315                                 dump_ipv4_packet(m, info, skb,
316                                             iphoff + ih->ihl*4+sizeof(_icmph));
317                                 nf_log_buf_add(m, "] ");
318                         }
319
320                         /* Max length: 10 "MTU=65535 " */
321                         if (ich->type == ICMP_DEST_UNREACH &&
322                             ich->code == ICMP_FRAG_NEEDED) {
323                                 nf_log_buf_add(m, "MTU=%u ",
324                                                ntohs(ich->un.frag.mtu));
325                         }
326                 }
327                 break;
328         }
329         /* Max Length */
330         case IPPROTO_AH: {
331                 struct ip_auth_hdr _ahdr;
332                 const struct ip_auth_hdr *ah;
333
334                 if (ntohs(ih->frag_off) & IP_OFFSET)
335                         break;
336
337                 /* Max length: 9 "PROTO=AH " */
338                 nf_log_buf_add(m, "PROTO=AH ");
339
340                 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
341                 ah = skb_header_pointer(skb, iphoff+ih->ihl*4,
342                                         sizeof(_ahdr), &_ahdr);
343                 if (ah == NULL) {
344                         nf_log_buf_add(m, "INCOMPLETE [%u bytes] ",
345                                        skb->len - iphoff - ih->ihl*4);
346                         break;
347                 }
348
349                 /* Length: 15 "SPI=0xF1234567 " */
350                 nf_log_buf_add(m, "SPI=0x%x ", ntohl(ah->spi));
351                 break;
352         }
353         case IPPROTO_ESP: {
354                 struct ip_esp_hdr _esph;
355                 const struct ip_esp_hdr *eh;
356
357                 /* Max length: 10 "PROTO=ESP " */
358                 nf_log_buf_add(m, "PROTO=ESP ");
359
360                 if (ntohs(ih->frag_off) & IP_OFFSET)
361                         break;
362
363                 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
364                 eh = skb_header_pointer(skb, iphoff+ih->ihl*4,
365                                         sizeof(_esph), &_esph);
366                 if (eh == NULL) {
367                         nf_log_buf_add(m, "INCOMPLETE [%u bytes] ",
368                                        skb->len - iphoff - ih->ihl*4);
369                         break;
370                 }
371
372                 /* Length: 15 "SPI=0xF1234567 " */
373                 nf_log_buf_add(m, "SPI=0x%x ", ntohl(eh->spi));
374                 break;
375         }
376         /* Max length: 10 "PROTO 255 " */
377         default:
378                 nf_log_buf_add(m, "PROTO=%u ", ih->protocol);
379         }
380
381         /* Max length: 15 "UID=4294967295 " */
382         if ((logflags & XT_LOG_UID) && !iphoff)
383                 dump_sk_uid_gid(m, skb->sk);
384
385         /* Max length: 16 "MARK=0xFFFFFFFF " */
386         if (!iphoff && skb->mark)
387                 nf_log_buf_add(m, "MARK=0x%x ", skb->mark);
388
389         /* Proto    Max log string length */
390         /* IP:      40+46+6+11+127 = 230 */
391         /* TCP:     10+max(25,20+30+13+9+32+11+127) = 252 */
392         /* UDP:     10+max(25,20) = 35 */
393         /* UDPLITE: 14+max(25,20) = 39 */
394         /* ICMP:    11+max(25, 18+25+max(19,14,24+3+n+10,3+n+10)) = 91+n */
395         /* ESP:     10+max(25)+15 = 50 */
396         /* AH:      9+max(25)+15 = 49 */
397         /* unknown: 10 */
398
399         /* (ICMP allows recursion one level deep) */
400         /* maxlen =  IP + ICMP +  IP + max(TCP,UDP,ICMP,unknown) */
401         /* maxlen = 230+   91  + 230 + 252 = 803 */
402 }
403
404 static void dump_ipv4_mac_header(struct nf_log_buf *m,
405                                  const struct nf_loginfo *info,
406                                  const struct sk_buff *skb)
407 {
408         struct net_device *dev = skb->dev;
409         unsigned int logflags = 0;
410
411         if (info->type == NF_LOG_TYPE_LOG)
412                 logflags = info->u.log.logflags;
413
414         if (!(logflags & XT_LOG_MACDECODE))
415                 goto fallback;
416
417         switch (dev->type) {
418         case ARPHRD_ETHER:
419                 nf_log_buf_add(m, "MACSRC=%pM MACDST=%pM MACPROTO=%04x ",
420                                eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
421                                ntohs(eth_hdr(skb)->h_proto));
422                 return;
423         default:
424                 break;
425         }
426
427 fallback:
428         nf_log_buf_add(m, "MAC=");
429         if (dev->hard_header_len &&
430             skb->mac_header != skb->network_header) {
431                 const unsigned char *p = skb_mac_header(skb);
432                 unsigned int i;
433
434                 nf_log_buf_add(m, "%02x", *p++);
435                 for (i = 1; i < dev->hard_header_len; i++, p++)
436                         nf_log_buf_add(m, ":%02x", *p);
437         }
438         nf_log_buf_add(m, " ");
439 }
440
441 static void
442 log_packet_common(struct nf_log_buf *m,
443                   u_int8_t pf,
444                   unsigned int hooknum,
445                   const struct sk_buff *skb,
446                   const struct net_device *in,
447                   const struct net_device *out,
448                   const struct nf_loginfo *loginfo,
449                   const char *prefix)
450 {
451         nf_log_buf_add(m, KERN_SOH "%c%sIN=%s OUT=%s ",
452                        '0' + loginfo->u.log.level, prefix,
453                        in ? in->name : "",
454                        out ? out->name : "");
455 #ifdef CONFIG_BRIDGE_NETFILTER
456         if (skb->nf_bridge) {
457                 const struct net_device *physindev;
458                 const struct net_device *physoutdev;
459
460                 physindev = skb->nf_bridge->physindev;
461                 if (physindev && in != physindev)
462                         nf_log_buf_add(m, "PHYSIN=%s ", physindev->name);
463                 physoutdev = skb->nf_bridge->physoutdev;
464                 if (physoutdev && out != physoutdev)
465                         nf_log_buf_add(m, "PHYSOUT=%s ", physoutdev->name);
466         }
467 #endif
468 }
469
470
471 static void
472 ipt_log_packet(struct net *net,
473                u_int8_t pf,
474                unsigned int hooknum,
475                const struct sk_buff *skb,
476                const struct net_device *in,
477                const struct net_device *out,
478                const struct nf_loginfo *loginfo,
479                const char *prefix)
480 {
481         struct nf_log_buf *m;
482
483         /* FIXME: Disabled from containers until syslog ns is supported */
484         if (!net_eq(net, &init_net))
485                 return;
486
487         m = nf_log_buf_open();
488
489         if (!loginfo)
490                 loginfo = &default_loginfo;
491
492         log_packet_common(m, pf, hooknum, skb, in, out, loginfo, prefix);
493
494         if (in != NULL)
495                 dump_ipv4_mac_header(m, loginfo, skb);
496
497         dump_ipv4_packet(m, loginfo, skb, 0);
498
499         nf_log_buf_close(m);
500 }
501
502 #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
503 /* One level of recursion won't kill us */
504 static void dump_ipv6_packet(struct nf_log_buf *m,
505                              const struct nf_loginfo *info,
506                              const struct sk_buff *skb, unsigned int ip6hoff,
507                              int recurse)
508 {
509         u_int8_t currenthdr;
510         int fragment;
511         struct ipv6hdr _ip6h;
512         const struct ipv6hdr *ih;
513         unsigned int ptr;
514         unsigned int hdrlen = 0;
515         unsigned int logflags;
516
517         if (info->type == NF_LOG_TYPE_LOG)
518                 logflags = info->u.log.logflags;
519         else
520                 logflags = NF_LOG_MASK;
521
522         ih = skb_header_pointer(skb, ip6hoff, sizeof(_ip6h), &_ip6h);
523         if (ih == NULL) {
524                 nf_log_buf_add(m, "TRUNCATED");
525                 return;
526         }
527
528         /* Max length: 88 "SRC=0000.0000.0000.0000.0000.0000.0000.0000 DST=0000.0000.0000.0000.0000.0000.0000.0000 " */
529         nf_log_buf_add(m, "SRC=%pI6 DST=%pI6 ", &ih->saddr, &ih->daddr);
530
531         /* Max length: 44 "LEN=65535 TC=255 HOPLIMIT=255 FLOWLBL=FFFFF " */
532         nf_log_buf_add(m, "LEN=%Zu TC=%u HOPLIMIT=%u FLOWLBL=%u ",
533                        ntohs(ih->payload_len) + sizeof(struct ipv6hdr),
534                        (ntohl(*(__be32 *)ih) & 0x0ff00000) >> 20,
535                        ih->hop_limit, (ntohl(*(__be32 *)ih) & 0x000fffff));
536
537         fragment = 0;
538         ptr = ip6hoff + sizeof(struct ipv6hdr);
539         currenthdr = ih->nexthdr;
540         while (currenthdr != NEXTHDR_NONE && ip6t_ext_hdr(currenthdr)) {
541                 struct ipv6_opt_hdr _hdr;
542                 const struct ipv6_opt_hdr *hp;
543
544                 hp = skb_header_pointer(skb, ptr, sizeof(_hdr), &_hdr);
545                 if (hp == NULL) {
546                         nf_log_buf_add(m, "TRUNCATED");
547                         return;
548                 }
549
550                 /* Max length: 48 "OPT (...) " */
551                 if (logflags & XT_LOG_IPOPT)
552                         nf_log_buf_add(m, "OPT ( ");
553
554                 switch (currenthdr) {
555                 case IPPROTO_FRAGMENT: {
556                         struct frag_hdr _fhdr;
557                         const struct frag_hdr *fh;
558
559                         nf_log_buf_add(m, "FRAG:");
560                         fh = skb_header_pointer(skb, ptr, sizeof(_fhdr),
561                                                 &_fhdr);
562                         if (fh == NULL) {
563                                 nf_log_buf_add(m, "TRUNCATED ");
564                                 return;
565                         }
566
567                         /* Max length: 6 "65535 " */
568                         nf_log_buf_add(m, "%u ", ntohs(fh->frag_off) & 0xFFF8);
569
570                         /* Max length: 11 "INCOMPLETE " */
571                         if (fh->frag_off & htons(0x0001))
572                                 nf_log_buf_add(m, "INCOMPLETE ");
573
574                         nf_log_buf_add(m, "ID:%08x ", ntohl(fh->identification));
575
576                         if (ntohs(fh->frag_off) & 0xFFF8)
577                                 fragment = 1;
578
579                         hdrlen = 8;
580
581                         break;
582                 }
583                 case IPPROTO_DSTOPTS:
584                 case IPPROTO_ROUTING:
585                 case IPPROTO_HOPOPTS:
586                         if (fragment) {
587                                 if (logflags & XT_LOG_IPOPT)
588                                         nf_log_buf_add(m, ")");
589                                 return;
590                         }
591                         hdrlen = ipv6_optlen(hp);
592                         break;
593                 /* Max Length */
594                 case IPPROTO_AH:
595                         if (logflags & XT_LOG_IPOPT) {
596                                 struct ip_auth_hdr _ahdr;
597                                 const struct ip_auth_hdr *ah;
598
599                                 /* Max length: 3 "AH " */
600                                 nf_log_buf_add(m, "AH ");
601
602                                 if (fragment) {
603                                         nf_log_buf_add(m, ")");
604                                         return;
605                                 }
606
607                                 ah = skb_header_pointer(skb, ptr, sizeof(_ahdr),
608                                                         &_ahdr);
609                                 if (ah == NULL) {
610                                         /*
611                                          * Max length: 26 "INCOMPLETE [65535
612                                          *  bytes] )"
613                                          */
614                                         nf_log_buf_add(m, "INCOMPLETE [%u bytes] )",
615                                                        skb->len - ptr);
616                                         return;
617                                 }
618
619                                 /* Length: 15 "SPI=0xF1234567 */
620                                 nf_log_buf_add(m, "SPI=0x%x ", ntohl(ah->spi));
621
622                         }
623
624                         hdrlen = (hp->hdrlen+2)<<2;
625                         break;
626                 case IPPROTO_ESP:
627                         if (logflags & XT_LOG_IPOPT) {
628                                 struct ip_esp_hdr _esph;
629                                 const struct ip_esp_hdr *eh;
630
631                                 /* Max length: 4 "ESP " */
632                                 nf_log_buf_add(m, "ESP ");
633
634                                 if (fragment) {
635                                         nf_log_buf_add(m, ")");
636                                         return;
637                                 }
638
639                                 /*
640                                  * Max length: 26 "INCOMPLETE [65535 bytes] )"
641                                  */
642                                 eh = skb_header_pointer(skb, ptr, sizeof(_esph),
643                                                         &_esph);
644                                 if (eh == NULL) {
645                                         nf_log_buf_add(m, "INCOMPLETE [%u bytes] )",
646                                                        skb->len - ptr);
647                                         return;
648                                 }
649
650                                 /* Length: 16 "SPI=0xF1234567 )" */
651                                 nf_log_buf_add(m, "SPI=0x%x )", ntohl(eh->spi));
652
653                         }
654                         return;
655                 default:
656                         /* Max length: 20 "Unknown Ext Hdr 255" */
657                         nf_log_buf_add(m, "Unknown Ext Hdr %u", currenthdr);
658                         return;
659                 }
660                 if (logflags & XT_LOG_IPOPT)
661                         nf_log_buf_add(m, ") ");
662
663                 currenthdr = hp->nexthdr;
664                 ptr += hdrlen;
665         }
666
667         switch (currenthdr) {
668         case IPPROTO_TCP:
669                 if (dump_tcp_header(m, skb, currenthdr, fragment, ptr,
670                     logflags))
671                         return;
672                 break;
673         case IPPROTO_UDP:
674         case IPPROTO_UDPLITE:
675                 if (dump_udp_header(m, skb, currenthdr, fragment, ptr))
676                         return;
677                 break;
678         case IPPROTO_ICMPV6: {
679                 struct icmp6hdr _icmp6h;
680                 const struct icmp6hdr *ic;
681
682                 /* Max length: 13 "PROTO=ICMPv6 " */
683                 nf_log_buf_add(m, "PROTO=ICMPv6 ");
684
685                 if (fragment)
686                         break;
687
688                 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
689                 ic = skb_header_pointer(skb, ptr, sizeof(_icmp6h), &_icmp6h);
690                 if (ic == NULL) {
691                         nf_log_buf_add(m, "INCOMPLETE [%u bytes] ",
692                                        skb->len - ptr);
693                         return;
694                 }
695
696                 /* Max length: 18 "TYPE=255 CODE=255 " */
697                 nf_log_buf_add(m, "TYPE=%u CODE=%u ",
698                                ic->icmp6_type, ic->icmp6_code);
699
700                 switch (ic->icmp6_type) {
701                 case ICMPV6_ECHO_REQUEST:
702                 case ICMPV6_ECHO_REPLY:
703                         /* Max length: 19 "ID=65535 SEQ=65535 " */
704                         nf_log_buf_add(m, "ID=%u SEQ=%u ",
705                                        ntohs(ic->icmp6_identifier),
706                                        ntohs(ic->icmp6_sequence));
707                         break;
708                 case ICMPV6_MGM_QUERY:
709                 case ICMPV6_MGM_REPORT:
710                 case ICMPV6_MGM_REDUCTION:
711                         break;
712
713                 case ICMPV6_PARAMPROB:
714                         /* Max length: 17 "POINTER=ffffffff " */
715                         nf_log_buf_add(m, "POINTER=%08x ",
716                                        ntohl(ic->icmp6_pointer));
717                         /* Fall through */
718                 case ICMPV6_DEST_UNREACH:
719                 case ICMPV6_PKT_TOOBIG:
720                 case ICMPV6_TIME_EXCEED:
721                         /* Max length: 3+maxlen */
722                         if (recurse) {
723                                 nf_log_buf_add(m, "[");
724                                 dump_ipv6_packet(m, info, skb,
725                                             ptr + sizeof(_icmp6h), 0);
726                                 nf_log_buf_add(m, "] ");
727                         }
728
729                         /* Max length: 10 "MTU=65535 " */
730                         if (ic->icmp6_type == ICMPV6_PKT_TOOBIG)
731                                 nf_log_buf_add(m, "MTU=%u ",
732                                                ntohl(ic->icmp6_mtu));
733                 }
734                 break;
735         }
736         /* Max length: 10 "PROTO=255 " */
737         default:
738                 nf_log_buf_add(m, "PROTO=%u ", currenthdr);
739         }
740
741         /* Max length: 15 "UID=4294967295 " */
742         if ((logflags & XT_LOG_UID) && recurse)
743                 dump_sk_uid_gid(m, skb->sk);
744
745         /* Max length: 16 "MARK=0xFFFFFFFF " */
746         if (recurse && skb->mark)
747                 nf_log_buf_add(m, "MARK=0x%x ", skb->mark);
748 }
749
750 static void dump_ipv6_mac_header(struct nf_log_buf *m,
751                                  const struct nf_loginfo *info,
752                                  const struct sk_buff *skb)
753 {
754         struct net_device *dev = skb->dev;
755         unsigned int logflags = 0;
756
757         if (info->type == NF_LOG_TYPE_LOG)
758                 logflags = info->u.log.logflags;
759
760         if (!(logflags & XT_LOG_MACDECODE))
761                 goto fallback;
762
763         switch (dev->type) {
764         case ARPHRD_ETHER:
765                 nf_log_buf_add(m, "MACSRC=%pM MACDST=%pM MACPROTO=%04x ",
766                                eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
767                                ntohs(eth_hdr(skb)->h_proto));
768                 return;
769         default:
770                 break;
771         }
772
773 fallback:
774         nf_log_buf_add(m, "MAC=");
775         if (dev->hard_header_len &&
776             skb->mac_header != skb->network_header) {
777                 const unsigned char *p = skb_mac_header(skb);
778                 unsigned int len = dev->hard_header_len;
779                 unsigned int i;
780
781                 if (dev->type == ARPHRD_SIT) {
782                         p -= ETH_HLEN;
783
784                         if (p < skb->head)
785                                 p = NULL;
786                 }
787
788                 if (p != NULL) {
789                         nf_log_buf_add(m, "%02x", *p++);
790                         for (i = 1; i < len; i++)
791                                 nf_log_buf_add(m, ":%02x", *p++);
792                 }
793                 nf_log_buf_add(m, " ");
794
795                 if (dev->type == ARPHRD_SIT) {
796                         const struct iphdr *iph =
797                                 (struct iphdr *)skb_mac_header(skb);
798                         nf_log_buf_add(m, "TUNNEL=%pI4->%pI4 ",
799                                        &iph->saddr, &iph->daddr);
800                 }
801         } else {
802                 nf_log_buf_add(m, " ");
803         }
804 }
805
806 static void
807 ip6t_log_packet(struct net *net,
808                 u_int8_t pf,
809                 unsigned int hooknum,
810                 const struct sk_buff *skb,
811                 const struct net_device *in,
812                 const struct net_device *out,
813                 const struct nf_loginfo *loginfo,
814                 const char *prefix)
815 {
816         struct nf_log_buf *m;
817
818         /* FIXME: Disabled from containers until syslog ns is supported */
819         if (!net_eq(net, &init_net))
820                 return;
821
822         m = nf_log_buf_open();
823
824         if (!loginfo)
825                 loginfo = &default_loginfo;
826
827         log_packet_common(m, pf, hooknum, skb, in, out, loginfo, prefix);
828
829         if (in != NULL)
830                 dump_ipv6_mac_header(m, loginfo, skb);
831
832         dump_ipv6_packet(m, loginfo, skb, skb_network_offset(skb), 1);
833
834         nf_log_buf_close(m);
835 }
836 #endif
837
838 static unsigned int
839 log_tg(struct sk_buff *skb, const struct xt_action_param *par)
840 {
841         const struct xt_log_info *loginfo = par->targinfo;
842         struct nf_loginfo li;
843         struct net *net = dev_net(par->in ? par->in : par->out);
844
845         li.type = NF_LOG_TYPE_LOG;
846         li.u.log.level = loginfo->level;
847         li.u.log.logflags = loginfo->logflags;
848
849         if (par->family == NFPROTO_IPV4)
850                 ipt_log_packet(net, NFPROTO_IPV4, par->hooknum, skb, par->in,
851                                par->out, &li, loginfo->prefix);
852 #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
853         else if (par->family == NFPROTO_IPV6)
854                 ip6t_log_packet(net, NFPROTO_IPV6, par->hooknum, skb, par->in,
855                                 par->out, &li, loginfo->prefix);
856 #endif
857         else
858                 WARN_ON_ONCE(1);
859
860         return XT_CONTINUE;
861 }
862
863 static int log_tg_check(const struct xt_tgchk_param *par)
864 {
865         const struct xt_log_info *loginfo = par->targinfo;
866
867         if (par->family != NFPROTO_IPV4 && par->family != NFPROTO_IPV6)
868                 return -EINVAL;
869
870         if (loginfo->level >= 8) {
871                 pr_debug("level %u >= 8\n", loginfo->level);
872                 return -EINVAL;
873         }
874
875         if (loginfo->prefix[sizeof(loginfo->prefix)-1] != '\0') {
876                 pr_debug("prefix is not null-terminated\n");
877                 return -EINVAL;
878         }
879
880         return 0;
881 }
882
883 static struct xt_target log_tg_regs[] __read_mostly = {
884         {
885                 .name           = "LOG",
886                 .family         = NFPROTO_IPV4,
887                 .target         = log_tg,
888                 .targetsize     = sizeof(struct xt_log_info),
889                 .checkentry     = log_tg_check,
890                 .me             = THIS_MODULE,
891         },
892 #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
893         {
894                 .name           = "LOG",
895                 .family         = NFPROTO_IPV6,
896                 .target         = log_tg,
897                 .targetsize     = sizeof(struct xt_log_info),
898                 .checkentry     = log_tg_check,
899                 .me             = THIS_MODULE,
900         },
901 #endif
902 };
903
904 static struct nf_logger ipt_log_logger __read_mostly = {
905         .name           = "ipt_LOG",
906         .type           = NF_LOG_TYPE_LOG,
907         .logfn          = &ipt_log_packet,
908         .me             = THIS_MODULE,
909 };
910
911 #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
912 static struct nf_logger ip6t_log_logger __read_mostly = {
913         .name           = "ip6t_LOG",
914         .type           = NF_LOG_TYPE_LOG,
915         .logfn          = &ip6t_log_packet,
916         .me             = THIS_MODULE,
917 };
918 #endif
919
920 static int __net_init log_net_init(struct net *net)
921 {
922         nf_log_set(net, NFPROTO_IPV4, &ipt_log_logger);
923 #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
924         nf_log_set(net, NFPROTO_IPV6, &ip6t_log_logger);
925 #endif
926         return 0;
927 }
928
929 static void __net_exit log_net_exit(struct net *net)
930 {
931         nf_log_unset(net, &ipt_log_logger);
932 #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
933         nf_log_unset(net, &ip6t_log_logger);
934 #endif
935 }
936
937 static struct pernet_operations log_net_ops = {
938         .init = log_net_init,
939         .exit = log_net_exit,
940 };
941
942 static int __init log_tg_init(void)
943 {
944         int ret;
945
946         ret = register_pernet_subsys(&log_net_ops);
947         if (ret < 0)
948                 goto err_pernet;
949
950         ret = xt_register_targets(log_tg_regs, ARRAY_SIZE(log_tg_regs));
951         if (ret < 0)
952                 goto err_target;
953
954         nf_log_register(NFPROTO_IPV4, &ipt_log_logger);
955 #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
956         nf_log_register(NFPROTO_IPV6, &ip6t_log_logger);
957 #endif
958         return 0;
959
960 err_target:
961         unregister_pernet_subsys(&log_net_ops);
962 err_pernet:
963         return ret;
964 }
965
966 static void __exit log_tg_exit(void)
967 {
968         unregister_pernet_subsys(&log_net_ops);
969         nf_log_unregister(&ipt_log_logger);
970 #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
971         nf_log_unregister(&ip6t_log_logger);
972 #endif
973         xt_unregister_targets(log_tg_regs, ARRAY_SIZE(log_tg_regs));
974 }
975
976 module_init(log_tg_init);
977 module_exit(log_tg_exit);
978
979 MODULE_LICENSE("GPL");
980 MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
981 MODULE_AUTHOR("Jan Rekorajski <baggins@pld.org.pl>");
982 MODULE_DESCRIPTION("Xtables: IPv4/IPv6 packet logging");
983 MODULE_ALIAS("ipt_LOG");
984 MODULE_ALIAS("ip6t_LOG");