Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[linux-drm-fsl-dcu.git] / net / ipv6 / netfilter / ip6t_LOG.c
1 /*
2  * This is a module which is used for logging packets.
3  */
4
5 /* (C) 2001 Jan Rekorajski <baggins@pld.org.pl>
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 #include <linux/module.h>
14 #include <linux/moduleparam.h>
15 #include <linux/skbuff.h>
16 #include <linux/if_arp.h>
17 #include <linux/ip.h>
18 #include <linux/spinlock.h>
19 #include <linux/icmpv6.h>
20 #include <net/udp.h>
21 #include <net/tcp.h>
22 #include <net/ipv6.h>
23 #include <linux/netfilter.h>
24 #include <linux/netfilter/x_tables.h>
25 #include <linux/netfilter_ipv6/ip6_tables.h>
26
27 MODULE_AUTHOR("Jan Rekorajski <baggins@pld.org.pl>");
28 MODULE_DESCRIPTION("IP6 tables LOG target module");
29 MODULE_LICENSE("GPL");
30
31 struct in_device;
32 #include <net/route.h>
33 #include <linux/netfilter_ipv6/ip6t_LOG.h>
34
35 #if 0
36 #define DEBUGP printk
37 #else
38 #define DEBUGP(format, args...)
39 #endif
40
41 /* Use lock to serialize, so printks don't overlap */
42 static DEFINE_SPINLOCK(log_lock);
43
44 /* One level of recursion won't kill us */
45 static void dump_packet(const struct nf_loginfo *info,
46                         const struct sk_buff *skb, unsigned int ip6hoff,
47                         int recurse)
48 {
49         u_int8_t currenthdr;
50         int fragment;
51         struct ipv6hdr _ip6h, *ih;
52         unsigned int ptr;
53         unsigned int hdrlen = 0;
54         unsigned int logflags;
55
56         if (info->type == NF_LOG_TYPE_LOG)
57                 logflags = info->u.log.logflags;
58         else
59                 logflags = NF_LOG_MASK;
60
61         ih = skb_header_pointer(skb, ip6hoff, sizeof(_ip6h), &_ip6h);
62         if (ih == NULL) {
63                 printk("TRUNCATED");
64                 return;
65         }
66
67         /* Max length: 88 "SRC=0000.0000.0000.0000.0000.0000.0000.0000 DST=0000.0000.0000.0000.0000.0000.0000.0000 " */
68         printk("SRC=" NIP6_FMT " DST=" NIP6_FMT " ", NIP6(ih->saddr), NIP6(ih->daddr));
69
70         /* Max length: 44 "LEN=65535 TC=255 HOPLIMIT=255 FLOWLBL=FFFFF " */
71         printk("LEN=%Zu TC=%u HOPLIMIT=%u FLOWLBL=%u ",
72                ntohs(ih->payload_len) + sizeof(struct ipv6hdr),
73                (ntohl(*(__be32 *)ih) & 0x0ff00000) >> 20,
74                ih->hop_limit,
75                (ntohl(*(__be32 *)ih) & 0x000fffff));
76
77         fragment = 0;
78         ptr = ip6hoff + sizeof(struct ipv6hdr);
79         currenthdr = ih->nexthdr;
80         while (currenthdr != NEXTHDR_NONE && ip6t_ext_hdr(currenthdr)) {
81                 struct ipv6_opt_hdr _hdr, *hp;
82
83                 hp = skb_header_pointer(skb, ptr, sizeof(_hdr), &_hdr);
84                 if (hp == NULL) {
85                         printk("TRUNCATED");
86                         return;
87                 }
88
89                 /* Max length: 48 "OPT (...) " */
90                 if (logflags & IP6T_LOG_IPOPT)
91                         printk("OPT ( ");
92
93                 switch (currenthdr) {
94                 case IPPROTO_FRAGMENT: {
95                         struct frag_hdr _fhdr, *fh;
96
97                         printk("FRAG:");
98                         fh = skb_header_pointer(skb, ptr, sizeof(_fhdr),
99                                                 &_fhdr);
100                         if (fh == NULL) {
101                                 printk("TRUNCATED ");
102                                 return;
103                         }
104
105                         /* Max length: 6 "65535 " */
106                         printk("%u ", ntohs(fh->frag_off) & 0xFFF8);
107
108                         /* Max length: 11 "INCOMPLETE " */
109                         if (fh->frag_off & htons(0x0001))
110                                 printk("INCOMPLETE ");
111
112                         printk("ID:%08x ", ntohl(fh->identification));
113
114                         if (ntohs(fh->frag_off) & 0xFFF8)
115                                 fragment = 1;
116
117                         hdrlen = 8;
118
119                         break;
120                 }
121                 case IPPROTO_DSTOPTS:
122                 case IPPROTO_ROUTING:
123                 case IPPROTO_HOPOPTS:
124                         if (fragment) {
125                                 if (logflags & IP6T_LOG_IPOPT)
126                                         printk(")");
127                                 return;
128                         }
129                         hdrlen = ipv6_optlen(hp);
130                         break;
131                 /* Max Length */
132                 case IPPROTO_AH:
133                         if (logflags & IP6T_LOG_IPOPT) {
134                                 struct ip_auth_hdr _ahdr, *ah;
135
136                                 /* Max length: 3 "AH " */
137                                 printk("AH ");
138
139                                 if (fragment) {
140                                         printk(")");
141                                         return;
142                                 }
143
144                                 ah = skb_header_pointer(skb, ptr, sizeof(_ahdr),
145                                                         &_ahdr);
146                                 if (ah == NULL) {
147                                         /*
148                                          * Max length: 26 "INCOMPLETE [65535
149                                          *  bytes] )"
150                                          */
151                                         printk("INCOMPLETE [%u bytes] )",
152                                                skb->len - ptr);
153                                         return;
154                                 }
155
156                                 /* Length: 15 "SPI=0xF1234567 */
157                                 printk("SPI=0x%x ", ntohl(ah->spi));
158
159                         }
160
161                         hdrlen = (hp->hdrlen+2)<<2;
162                         break;
163                 case IPPROTO_ESP:
164                         if (logflags & IP6T_LOG_IPOPT) {
165                                 struct ip_esp_hdr _esph, *eh;
166
167                                 /* Max length: 4 "ESP " */
168                                 printk("ESP ");
169
170                                 if (fragment) {
171                                         printk(")");
172                                         return;
173                                 }
174
175                                 /*
176                                  * Max length: 26 "INCOMPLETE [65535 bytes] )"
177                                  */
178                                 eh = skb_header_pointer(skb, ptr, sizeof(_esph),
179                                                         &_esph);
180                                 if (eh == NULL) {
181                                         printk("INCOMPLETE [%u bytes] )",
182                                                skb->len - ptr);
183                                         return;
184                                 }
185
186                                 /* Length: 16 "SPI=0xF1234567 )" */
187                                 printk("SPI=0x%x )", ntohl(eh->spi) );
188
189                         }
190                         return;
191                 default:
192                         /* Max length: 20 "Unknown Ext Hdr 255" */
193                         printk("Unknown Ext Hdr %u", currenthdr);
194                         return;
195                 }
196                 if (logflags & IP6T_LOG_IPOPT)
197                         printk(") ");
198
199                 currenthdr = hp->nexthdr;
200                 ptr += hdrlen;
201         }
202
203         switch (currenthdr) {
204         case IPPROTO_TCP: {
205                 struct tcphdr _tcph, *th;
206
207                 /* Max length: 10 "PROTO=TCP " */
208                 printk("PROTO=TCP ");
209
210                 if (fragment)
211                         break;
212
213                 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
214                 th = skb_header_pointer(skb, ptr, sizeof(_tcph), &_tcph);
215                 if (th == NULL) {
216                         printk("INCOMPLETE [%u bytes] ", skb->len - ptr);
217                         return;
218                 }
219
220                 /* Max length: 20 "SPT=65535 DPT=65535 " */
221                 printk("SPT=%u DPT=%u ",
222                        ntohs(th->source), ntohs(th->dest));
223                 /* Max length: 30 "SEQ=4294967295 ACK=4294967295 " */
224                 if (logflags & IP6T_LOG_TCPSEQ)
225                         printk("SEQ=%u ACK=%u ",
226                                ntohl(th->seq), ntohl(th->ack_seq));
227                 /* Max length: 13 "WINDOW=65535 " */
228                 printk("WINDOW=%u ", ntohs(th->window));
229                 /* Max length: 9 "RES=0x3C " */
230                 printk("RES=0x%02x ", (u_int8_t)(ntohl(tcp_flag_word(th) & TCP_RESERVED_BITS) >> 22));
231                 /* Max length: 32 "CWR ECE URG ACK PSH RST SYN FIN " */
232                 if (th->cwr)
233                         printk("CWR ");
234                 if (th->ece)
235                         printk("ECE ");
236                 if (th->urg)
237                         printk("URG ");
238                 if (th->ack)
239                         printk("ACK ");
240                 if (th->psh)
241                         printk("PSH ");
242                 if (th->rst)
243                         printk("RST ");
244                 if (th->syn)
245                         printk("SYN ");
246                 if (th->fin)
247                         printk("FIN ");
248                 /* Max length: 11 "URGP=65535 " */
249                 printk("URGP=%u ", ntohs(th->urg_ptr));
250
251                 if ((logflags & IP6T_LOG_TCPOPT)
252                     && th->doff * 4 > sizeof(struct tcphdr)) {
253                         u_int8_t _opt[60 - sizeof(struct tcphdr)], *op;
254                         unsigned int i;
255                         unsigned int optsize = th->doff * 4
256                                                - sizeof(struct tcphdr);
257
258                         op = skb_header_pointer(skb,
259                                                 ptr + sizeof(struct tcphdr),
260                                                 optsize, _opt);
261                         if (op == NULL) {
262                                 printk("OPT (TRUNCATED)");
263                                 return;
264                         }
265
266                         /* Max length: 127 "OPT (" 15*4*2chars ") " */
267                         printk("OPT (");
268                         for (i =0; i < optsize; i++)
269                                 printk("%02X", op[i]);
270                         printk(") ");
271                 }
272                 break;
273         }
274         case IPPROTO_UDP:
275         case IPPROTO_UDPLITE: {
276                 struct udphdr _udph, *uh;
277
278                 if (currenthdr == IPPROTO_UDP)
279                         /* Max length: 10 "PROTO=UDP "     */
280                         printk("PROTO=UDP " );
281                 else    /* Max length: 14 "PROTO=UDPLITE " */
282                         printk("PROTO=UDPLITE ");
283
284                 if (fragment)
285                         break;
286
287                 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
288                 uh = skb_header_pointer(skb, ptr, sizeof(_udph), &_udph);
289                 if (uh == NULL) {
290                         printk("INCOMPLETE [%u bytes] ", skb->len - ptr);
291                         return;
292                 }
293
294                 /* Max length: 20 "SPT=65535 DPT=65535 " */
295                 printk("SPT=%u DPT=%u LEN=%u ",
296                        ntohs(uh->source), ntohs(uh->dest),
297                        ntohs(uh->len));
298                 break;
299         }
300         case IPPROTO_ICMPV6: {
301                 struct icmp6hdr _icmp6h, *ic;
302
303                 /* Max length: 13 "PROTO=ICMPv6 " */
304                 printk("PROTO=ICMPv6 ");
305
306                 if (fragment)
307                         break;
308
309                 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
310                 ic = skb_header_pointer(skb, ptr, sizeof(_icmp6h), &_icmp6h);
311                 if (ic == NULL) {
312                         printk("INCOMPLETE [%u bytes] ", skb->len - ptr);
313                         return;
314                 }
315
316                 /* Max length: 18 "TYPE=255 CODE=255 " */
317                 printk("TYPE=%u CODE=%u ", ic->icmp6_type, ic->icmp6_code);
318
319                 switch (ic->icmp6_type) {
320                 case ICMPV6_ECHO_REQUEST:
321                 case ICMPV6_ECHO_REPLY:
322                         /* Max length: 19 "ID=65535 SEQ=65535 " */
323                         printk("ID=%u SEQ=%u ",
324                                 ntohs(ic->icmp6_identifier),
325                                 ntohs(ic->icmp6_sequence));
326                         break;
327                 case ICMPV6_MGM_QUERY:
328                 case ICMPV6_MGM_REPORT:
329                 case ICMPV6_MGM_REDUCTION:
330                         break;
331
332                 case ICMPV6_PARAMPROB:
333                         /* Max length: 17 "POINTER=ffffffff " */
334                         printk("POINTER=%08x ", ntohl(ic->icmp6_pointer));
335                         /* Fall through */
336                 case ICMPV6_DEST_UNREACH:
337                 case ICMPV6_PKT_TOOBIG:
338                 case ICMPV6_TIME_EXCEED:
339                         /* Max length: 3+maxlen */
340                         if (recurse) {
341                                 printk("[");
342                                 dump_packet(info, skb, ptr + sizeof(_icmp6h),
343                                             0);
344                                 printk("] ");
345                         }
346
347                         /* Max length: 10 "MTU=65535 " */
348                         if (ic->icmp6_type == ICMPV6_PKT_TOOBIG)
349                                 printk("MTU=%u ", ntohl(ic->icmp6_mtu));
350                 }
351                 break;
352         }
353         /* Max length: 10 "PROTO=255 " */
354         default:
355                 printk("PROTO=%u ", currenthdr);
356         }
357
358         /* Max length: 15 "UID=4294967295 " */
359         if ((logflags & IP6T_LOG_UID) && recurse && skb->sk) {
360                 read_lock_bh(&skb->sk->sk_callback_lock);
361                 if (skb->sk->sk_socket && skb->sk->sk_socket->file)
362                         printk("UID=%u ", skb->sk->sk_socket->file->f_uid);
363                 read_unlock_bh(&skb->sk->sk_callback_lock);
364         }
365 }
366
367 static struct nf_loginfo default_loginfo = {
368         .type   = NF_LOG_TYPE_LOG,
369         .u = {
370                 .log = {
371                         .level    = 0,
372                         .logflags = NF_LOG_MASK,
373                 },
374         },
375 };
376
377 static void
378 ip6t_log_packet(unsigned int pf,
379                 unsigned int hooknum,
380                 const struct sk_buff *skb,
381                 const struct net_device *in,
382                 const struct net_device *out,
383                 const struct nf_loginfo *loginfo,
384                 const char *prefix)
385 {
386         if (!loginfo)
387                 loginfo = &default_loginfo;
388
389         spin_lock_bh(&log_lock);
390         printk("<%d>%sIN=%s OUT=%s ", loginfo->u.log.level,
391                 prefix,
392                 in ? in->name : "",
393                 out ? out->name : "");
394         if (in && !out) {
395                 unsigned int len;
396                 /* MAC logging for input chain only. */
397                 printk("MAC=");
398                 if (skb->dev && (len = skb->dev->hard_header_len) &&
399                     skb->mac.raw != skb->nh.raw) {
400                         unsigned char *p = skb->mac.raw;
401                         int i;
402
403                         if (skb->dev->type == ARPHRD_SIT &&
404                             (p -= ETH_HLEN) < skb->head)
405                                 p = NULL;
406
407                         if (p != NULL) {
408                                 for (i = 0; i < len; i++)
409                                         printk("%02x%s", p[i],
410                                                i == len - 1 ? "" : ":");
411                         }
412                         printk(" ");
413
414                         if (skb->dev->type == ARPHRD_SIT) {
415                                 struct iphdr *iph = (struct iphdr *)skb->mac.raw;
416                                 printk("TUNNEL=%u.%u.%u.%u->%u.%u.%u.%u ",
417                                        NIPQUAD(iph->saddr),
418                                        NIPQUAD(iph->daddr));
419                         }
420                 } else
421                         printk(" ");
422         }
423
424         dump_packet(loginfo, skb, (u8*)skb->nh.ipv6h - skb->data, 1);
425         printk("\n");
426         spin_unlock_bh(&log_lock);
427 }
428
429 static unsigned int
430 ip6t_log_target(struct sk_buff **pskb,
431                 const struct net_device *in,
432                 const struct net_device *out,
433                 unsigned int hooknum,
434                 const struct xt_target *target,
435                 const void *targinfo)
436 {
437         const struct ip6t_log_info *loginfo = targinfo;
438         struct nf_loginfo li;
439
440         li.type = NF_LOG_TYPE_LOG;
441         li.u.log.level = loginfo->level;
442         li.u.log.logflags = loginfo->logflags;
443
444         ip6t_log_packet(PF_INET6, hooknum, *pskb, in, out, &li,
445                         loginfo->prefix);
446         return XT_CONTINUE;
447 }
448
449
450 static int ip6t_log_checkentry(const char *tablename,
451                                const void *entry,
452                                const struct xt_target *target,
453                                void *targinfo,
454                                unsigned int hook_mask)
455 {
456         const struct ip6t_log_info *loginfo = targinfo;
457
458         if (loginfo->level >= 8) {
459                 DEBUGP("LOG: level %u >= 8\n", loginfo->level);
460                 return 0;
461         }
462         if (loginfo->prefix[sizeof(loginfo->prefix)-1] != '\0') {
463                 DEBUGP("LOG: prefix term %i\n",
464                        loginfo->prefix[sizeof(loginfo->prefix)-1]);
465                 return 0;
466         }
467         return 1;
468 }
469
470 static struct xt_target ip6t_log_reg = {
471         .name           = "LOG",
472         .family         = AF_INET6,
473         .target         = ip6t_log_target,
474         .targetsize     = sizeof(struct ip6t_log_info),
475         .checkentry     = ip6t_log_checkentry,
476         .me             = THIS_MODULE,
477 };
478
479 static struct nf_logger ip6t_logger = {
480         .name           = "ip6t_LOG",
481         .logfn          = &ip6t_log_packet,
482         .me             = THIS_MODULE,
483 };
484
485 static int __init ip6t_log_init(void)
486 {
487         int ret;
488
489         ret = xt_register_target(&ip6t_log_reg);
490         if (ret < 0)
491                 return ret;
492         if (nf_log_register(PF_INET6, &ip6t_logger) < 0) {
493                 printk(KERN_WARNING "ip6t_LOG: not logging via system console "
494                        "since somebody else already registered for PF_INET6\n");
495                 /* we cannot make module load fail here, since otherwise
496                  * ip6tables userspace would abort */
497         }
498
499         return 0;
500 }
501
502 static void __exit ip6t_log_fini(void)
503 {
504         nf_log_unregister_logger(&ip6t_logger);
505         xt_unregister_target(&ip6t_log_reg);
506 }
507
508 module_init(ip6t_log_init);
509 module_exit(ip6t_log_fini);