Add changes from the Linux-2.6 tree.
[linux-2.6.git] / net / ipv4 / netfilter / ipt_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 #include <linux/module.h>
14 #include <linux/spinlock.h>
15 #include <linux/skbuff.h>
16 #include <linux/ip.h>
17 #include <net/icmp.h>
18 #include <net/udp.h>
19 #include <net/tcp.h>
20 #include <net/route.h>
21
22 #include <linux/netfilter.h>
23 #include <linux/netfilter_ipv4/ip_tables.h>
24 #include <linux/netfilter_ipv4/ipt_LOG.h>
25
26 MODULE_LICENSE("GPL");
27 MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
28 MODULE_DESCRIPTION("iptables syslog logging module");
29
30 #if 0
31 #define DEBUGP printk
32 #else
33 #define DEBUGP(format, args...)
34 #endif
35
36 /* Use lock to serialize, so printks don't overlap */
37 static DEFINE_SPINLOCK(log_lock);
38
39 /* One level of recursion won't kill us */
40 static void dump_packet(const struct nf_loginfo *info,
41                         const struct sk_buff *skb,
42                         unsigned int iphoff)
43 {
44         struct iphdr _iph, *ih;
45         unsigned int logflags;
46
47         if (info->type == NF_LOG_TYPE_LOG)
48                 logflags = info->u.log.logflags;
49         else
50                 logflags = NF_LOG_MASK;
51
52         printk("TAG=%d ", skb->skb_tag);
53
54         ih = skb_header_pointer(skb, iphoff, sizeof(_iph), &_iph);
55         if (ih == NULL) {
56                 printk("TRUNCATED");
57                 return;
58         }
59
60         /* Important fields:
61          * TOS, len, DF/MF, fragment offset, TTL, src, dst, options. */
62         /* Max length: 40 "SRC=255.255.255.255 DST=255.255.255.255 " */
63         printk("SRC=%u.%u.%u.%u DST=%u.%u.%u.%u ",
64                NIPQUAD(ih->saddr), NIPQUAD(ih->daddr));
65
66         /* Max length: 46 "LEN=65535 TOS=0xFF PREC=0xFF TTL=255 ID=65535 " */
67         printk("LEN=%u TOS=0x%02X PREC=0x%02X TTL=%u ID=%u ",
68                ntohs(ih->tot_len), ih->tos & IPTOS_TOS_MASK,
69                ih->tos & IPTOS_PREC_MASK, ih->ttl, ntohs(ih->id));
70
71         /* Max length: 6 "CE DF MF " */
72         if (ntohs(ih->frag_off) & IP_CE)
73                 printk("CE ");
74         if (ntohs(ih->frag_off) & IP_DF)
75                 printk("DF ");
76         if (ntohs(ih->frag_off) & IP_MF)
77                 printk("MF ");
78
79         /* Max length: 11 "FRAG:65535 " */
80         if (ntohs(ih->frag_off) & IP_OFFSET)
81                 printk("FRAG:%u ", ntohs(ih->frag_off) & IP_OFFSET);
82
83         if ((logflags & IPT_LOG_IPOPT)
84             && ih->ihl * 4 > sizeof(struct iphdr)) {
85                 unsigned char _opt[4 * 15 - sizeof(struct iphdr)], *op;
86                 unsigned int i, optsize;
87
88                 optsize = ih->ihl * 4 - sizeof(struct iphdr);
89                 op = skb_header_pointer(skb, iphoff+sizeof(_iph),
90                                         optsize, _opt);
91                 if (op == NULL) {
92                         printk("TRUNCATED");
93                         return;
94                 }
95
96                 /* Max length: 127 "OPT (" 15*4*2chars ") " */
97                 printk("OPT (");
98                 for (i = 0; i < optsize; i++)
99                         printk("%02X", op[i]);
100                 printk(") ");
101         }
102
103         switch (ih->protocol) {
104         case IPPROTO_TCP: {
105                 struct tcphdr _tcph, *th;
106
107                 /* Max length: 10 "PROTO=TCP " */
108                 printk("PROTO=TCP ");
109
110                 if (ntohs(ih->frag_off) & IP_OFFSET)
111                         break;
112
113                 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
114                 th = skb_header_pointer(skb, iphoff + ih->ihl * 4,
115                                         sizeof(_tcph), &_tcph);
116                 if (th == NULL) {
117                         printk("INCOMPLETE [%u bytes] ",
118                                skb->len - iphoff - ih->ihl*4);
119                         break;
120                 }
121
122                 /* Max length: 20 "SPT=65535 DPT=65535 " */
123                 printk("SPT=%u DPT=%u ",
124                        ntohs(th->source), ntohs(th->dest));
125                 /* Max length: 30 "SEQ=4294967295 ACK=4294967295 " */
126                 if (logflags & IPT_LOG_TCPSEQ)
127                         printk("SEQ=%u ACK=%u ",
128                                ntohl(th->seq), ntohl(th->ack_seq));
129                 /* Max length: 13 "WINDOW=65535 " */
130                 printk("WINDOW=%u ", ntohs(th->window));
131                 /* Max length: 9 "RES=0x3F " */
132                 printk("RES=0x%02x ", (u8)(ntohl(tcp_flag_word(th) & TCP_RESERVED_BITS) >> 22));
133                 /* Max length: 32 "CWR ECE URG ACK PSH RST SYN FIN " */
134                 if (th->cwr)
135                         printk("CWR ");
136                 if (th->ece)
137                         printk("ECE ");
138                 if (th->urg)
139                         printk("URG ");
140                 if (th->ack)
141                         printk("ACK ");
142                 if (th->psh)
143                         printk("PSH ");
144                 if (th->rst)
145                         printk("RST ");
146                 if (th->syn)
147                         printk("SYN ");
148                 if (th->fin)
149                         printk("FIN ");
150                 /* Max length: 11 "URGP=65535 " */
151                 printk("URGP=%u ", ntohs(th->urg_ptr));
152
153                 if ((logflags & IPT_LOG_TCPOPT)
154                     && th->doff * 4 > sizeof(struct tcphdr)) {
155                         unsigned char _opt[4 * 15 - sizeof(struct tcphdr)];
156                         unsigned char *op;
157                         unsigned int i, optsize;
158
159                         optsize = th->doff * 4 - sizeof(struct tcphdr);
160                         op = skb_header_pointer(skb,
161                                                 iphoff+ih->ihl*4+sizeof(_tcph),
162                                                 optsize, _opt);
163                         if (op == NULL) {
164                                 printk("TRUNCATED");
165                                 return;
166                         }
167
168                         /* Max length: 127 "OPT (" 15*4*2chars ") " */
169                         printk("OPT (");
170                         for (i = 0; i < optsize; i++)
171                                 printk("%02X", op[i]);
172                         printk(") ");
173                 }
174                 break;
175         }
176         case IPPROTO_UDP:
177         case IPPROTO_UDPLITE: {
178                 struct udphdr _udph, *uh;
179
180                 if (ih->protocol == IPPROTO_UDP)
181                         /* Max length: 10 "PROTO=UDP "     */
182                         printk("PROTO=UDP " );
183                 else    /* Max length: 14 "PROTO=UDPLITE " */
184                         printk("PROTO=UDPLITE ");
185
186                 if (ntohs(ih->frag_off) & IP_OFFSET)
187                         break;
188
189                 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
190                 uh = skb_header_pointer(skb, iphoff+ih->ihl*4,
191                                         sizeof(_udph), &_udph);
192                 if (uh == NULL) {
193                         printk("INCOMPLETE [%u bytes] ",
194                                skb->len - iphoff - ih->ihl*4);
195                         break;
196                 }
197
198                 /* Max length: 20 "SPT=65535 DPT=65535 " */
199                 printk("SPT=%u DPT=%u LEN=%u ",
200                        ntohs(uh->source), ntohs(uh->dest),
201                        ntohs(uh->len));
202                 break;
203         }
204         case IPPROTO_ICMP: {
205                 struct icmphdr _icmph, *ich;
206                 static const size_t required_len[NR_ICMP_TYPES+1]
207                         = { [ICMP_ECHOREPLY] = 4,
208                             [ICMP_DEST_UNREACH]
209                             = 8 + sizeof(struct iphdr),
210                             [ICMP_SOURCE_QUENCH]
211                             = 8 + sizeof(struct iphdr),
212                             [ICMP_REDIRECT]
213                             = 8 + sizeof(struct iphdr),
214                             [ICMP_ECHO] = 4,
215                             [ICMP_TIME_EXCEEDED]
216                             = 8 + sizeof(struct iphdr),
217                             [ICMP_PARAMETERPROB]
218                             = 8 + sizeof(struct iphdr),
219                             [ICMP_TIMESTAMP] = 20,
220                             [ICMP_TIMESTAMPREPLY] = 20,
221                             [ICMP_ADDRESS] = 12,
222                             [ICMP_ADDRESSREPLY] = 12 };
223
224                 /* Max length: 11 "PROTO=ICMP " */
225                 printk("PROTO=ICMP ");
226
227                 if (ntohs(ih->frag_off) & IP_OFFSET)
228                         break;
229
230                 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
231                 ich = skb_header_pointer(skb, iphoff + ih->ihl * 4,
232                                          sizeof(_icmph), &_icmph);
233                 if (ich == NULL) {
234                         printk("INCOMPLETE [%u bytes] ",
235                                skb->len - iphoff - ih->ihl*4);
236                         break;
237                 }
238
239                 /* Max length: 18 "TYPE=255 CODE=255 " */
240                 printk("TYPE=%u CODE=%u ", ich->type, ich->code);
241
242                 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
243                 if (ich->type <= NR_ICMP_TYPES
244                     && required_len[ich->type]
245                     && skb->len-iphoff-ih->ihl*4 < required_len[ich->type]) {
246                         printk("INCOMPLETE [%u bytes] ",
247                                skb->len - iphoff - ih->ihl*4);
248                         break;
249                 }
250
251                 switch (ich->type) {
252                 case ICMP_ECHOREPLY:
253                 case ICMP_ECHO:
254                         /* Max length: 19 "ID=65535 SEQ=65535 " */
255                         printk("ID=%u SEQ=%u ",
256                                ntohs(ich->un.echo.id),
257                                ntohs(ich->un.echo.sequence));
258                         break;
259
260                 case ICMP_PARAMETERPROB:
261                         /* Max length: 14 "PARAMETER=255 " */
262                         printk("PARAMETER=%u ",
263                                ntohl(ich->un.gateway) >> 24);
264                         break;
265                 case ICMP_REDIRECT:
266                         /* Max length: 24 "GATEWAY=255.255.255.255 " */
267                         printk("GATEWAY=%u.%u.%u.%u ",
268                                NIPQUAD(ich->un.gateway));
269                         /* Fall through */
270                 case ICMP_DEST_UNREACH:
271                 case ICMP_SOURCE_QUENCH:
272                 case ICMP_TIME_EXCEEDED:
273                         /* Max length: 3+maxlen */
274                         if (!iphoff) { /* Only recurse once. */
275                                 printk("[");
276                                 dump_packet(info, skb,
277                                             iphoff + ih->ihl*4+sizeof(_icmph));
278                                 printk("] ");
279                         }
280
281                         /* Max length: 10 "MTU=65535 " */
282                         if (ich->type == ICMP_DEST_UNREACH
283                             && ich->code == ICMP_FRAG_NEEDED)
284                                 printk("MTU=%u ", ntohs(ich->un.frag.mtu));
285                 }
286                 break;
287         }
288         /* Max Length */
289         case IPPROTO_AH: {
290                 struct ip_auth_hdr _ahdr, *ah;
291
292                 if (ntohs(ih->frag_off) & IP_OFFSET)
293                         break;
294                 
295                 /* Max length: 9 "PROTO=AH " */
296                 printk("PROTO=AH ");
297
298                 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
299                 ah = skb_header_pointer(skb, iphoff+ih->ihl*4,
300                                         sizeof(_ahdr), &_ahdr);
301                 if (ah == NULL) {
302                         printk("INCOMPLETE [%u bytes] ",
303                                skb->len - iphoff - ih->ihl*4);
304                         break;
305                 }
306
307                 /* Length: 15 "SPI=0xF1234567 " */
308                 printk("SPI=0x%x ", ntohl(ah->spi));
309                 break;
310         }
311         case IPPROTO_ESP: {
312                 struct ip_esp_hdr _esph, *eh;
313
314                 /* Max length: 10 "PROTO=ESP " */
315                 printk("PROTO=ESP ");
316
317                 if (ntohs(ih->frag_off) & IP_OFFSET)
318                         break;
319
320                 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
321                 eh = skb_header_pointer(skb, iphoff+ih->ihl*4,
322                                         sizeof(_esph), &_esph);
323                 if (eh == NULL) {
324                         printk("INCOMPLETE [%u bytes] ",
325                                skb->len - iphoff - ih->ihl*4);
326                         break;
327                 }
328
329                 /* Length: 15 "SPI=0xF1234567 " */
330                 printk("SPI=0x%x ", ntohl(eh->spi));
331                 break;
332         }
333         /* Max length: 10 "PROTO 255 " */
334         default:
335                 printk("PROTO=%u ", ih->protocol);
336         }
337
338         /* Max length: 15 "UID=4294967295 " */
339         if ((logflags & IPT_LOG_UID) && !iphoff && skb->sk) {
340                 read_lock_bh(&skb->sk->sk_callback_lock);
341                 if (skb->sk->sk_socket && skb->sk->sk_socket->file)
342                         printk("UID=%u ", skb->sk->sk_socket->file->f_uid);
343                 read_unlock_bh(&skb->sk->sk_callback_lock);
344         }
345
346         /* Proto    Max log string length */
347         /* IP:      40+46+6+11+127 = 230 */
348         /* TCP:     10+max(25,20+30+13+9+32+11+127) = 252 */
349         /* UDP:     10+max(25,20) = 35 */
350         /* UDPLITE: 14+max(25,20) = 39 */
351         /* ICMP:    11+max(25, 18+25+max(19,14,24+3+n+10,3+n+10)) = 91+n */
352         /* ESP:     10+max(25)+15 = 50 */
353         /* AH:      9+max(25)+15 = 49 */
354         /* unknown: 10 */
355
356         /* (ICMP allows recursion one level deep) */
357         /* maxlen =  IP + ICMP +  IP + max(TCP,UDP,ICMP,unknown) */
358         /* maxlen = 230+   91  + 230 + 252 = 803 */
359 }
360
361 static struct nf_loginfo default_loginfo = {
362         .type   = NF_LOG_TYPE_LOG,
363         .u = {
364                 .log = {
365                         .level    = 0,
366                         .logflags = NF_LOG_MASK,
367                 },
368         },
369 };
370
371 static void
372 ipt_log_packet(unsigned int pf,
373                unsigned int hooknum,
374                const struct sk_buff *skb,
375                const struct net_device *in,
376                const struct net_device *out,
377                const struct nf_loginfo *loginfo,
378                const char *prefix)
379 {
380         if (!loginfo)
381                 loginfo = &default_loginfo;
382
383         spin_lock_bh(&log_lock);
384         printk("<%d>%sIN=%s OUT=%s ", loginfo->u.log.level,
385                prefix,
386                in ? in->name : "",
387                out ? out->name : "");
388 #ifdef CONFIG_BRIDGE_NETFILTER
389         if (skb->nf_bridge) {
390                 struct net_device *physindev = skb->nf_bridge->physindev;
391                 struct net_device *physoutdev = skb->nf_bridge->physoutdev;
392
393                 if (physindev && in != physindev)
394                         printk("PHYSIN=%s ", physindev->name);
395                 if (physoutdev && out != physoutdev)
396                         printk("PHYSOUT=%s ", physoutdev->name);
397         }
398 #endif
399
400         if (in && !out) {
401                 /* MAC logging for input chain only. */
402                 printk("MAC=");
403                 if (skb->dev && skb->dev->hard_header_len
404                     && skb->mac.raw != (void*)skb->nh.iph) {
405                         int i;
406                         unsigned char *p = skb->mac.raw;
407                         for (i = 0; i < skb->dev->hard_header_len; i++,p++)
408                                 printk("%02x%c", *p,
409                                        i==skb->dev->hard_header_len - 1
410                                        ? ' ':':');
411                 } else
412                         printk(" ");
413         }
414
415         dump_packet(loginfo, skb, 0);
416         printk("\n");
417         spin_unlock_bh(&log_lock);
418 }
419
420 static unsigned int
421 ipt_log_target(struct sk_buff **pskb,
422                const struct net_device *in,
423                const struct net_device *out,
424                unsigned int hooknum,
425                const struct xt_target *target,
426                const void *targinfo)
427 {
428         const struct ipt_log_info *loginfo = targinfo;
429         struct nf_loginfo li;
430
431         li.type = NF_LOG_TYPE_LOG;
432         li.u.log.level = loginfo->level;
433         li.u.log.logflags = loginfo->logflags;
434
435         ipt_log_packet(PF_INET, hooknum, *pskb, in, out, &li,
436                        loginfo->prefix);
437         return IPT_CONTINUE;
438 }
439
440 static int ipt_log_checkentry(const char *tablename,
441                               const void *e,
442                               const struct xt_target *target,
443                               void *targinfo,
444                               unsigned int hook_mask)
445 {
446         const struct ipt_log_info *loginfo = targinfo;
447
448         if (loginfo->level >= 8) {
449                 DEBUGP("LOG: level %u >= 8\n", loginfo->level);
450                 return 0;
451         }
452         if (loginfo->prefix[sizeof(loginfo->prefix)-1] != '\0') {
453                 DEBUGP("LOG: prefix term %i\n",
454                        loginfo->prefix[sizeof(loginfo->prefix)-1]);
455                 return 0;
456         }
457         return 1;
458 }
459
460 static struct ipt_target ipt_log_reg = {
461         .name           = "LOG",
462         .target         = ipt_log_target,
463         .targetsize     = sizeof(struct ipt_log_info),
464         .checkentry     = ipt_log_checkentry,
465         .me             = THIS_MODULE,
466 };
467
468 static struct nf_logger ipt_log_logger ={
469         .name           = "ipt_LOG",
470         .logfn          = &ipt_log_packet,
471         .me             = THIS_MODULE,
472 };
473
474 static int __init ipt_log_init(void)
475 {
476         if (ipt_register_target(&ipt_log_reg))
477                 return -EINVAL;
478         if (nf_log_register(PF_INET, &ipt_log_logger) < 0) {
479                 printk(KERN_WARNING "ipt_LOG: not logging via system console "
480                        "since somebody else already registered for PF_INET\n");
481                 /* we cannot make module load fail here, since otherwise
482                  * iptables userspace would abort */
483         }
484         
485         return 0;
486 }
487
488 static void __exit ipt_log_fini(void)
489 {
490         nf_log_unregister_logger(&ipt_log_logger);
491         ipt_unregister_target(&ipt_log_reg);
492 }
493
494 module_init(ipt_log_init);
495 module_exit(ipt_log_fini);