bcc43a625e72988e5f8b6de63f3cec38ccae06d3
[safe/jmp/linux-2.6] / 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/x_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;
45         const struct iphdr *ih;
46         unsigned int logflags;
47
48         if (info->type == NF_LOG_TYPE_LOG)
49                 logflags = info->u.log.logflags;
50         else
51                 logflags = NF_LOG_MASK;
52
53         ih = skb_header_pointer(skb, iphoff, sizeof(_iph), &_iph);
54         if (ih == NULL) {
55                 printk("TRUNCATED");
56                 return;
57         }
58
59         /* Important fields:
60          * TOS, len, DF/MF, fragment offset, TTL, src, dst, options. */
61         /* Max length: 40 "SRC=255.255.255.255 DST=255.255.255.255 " */
62         printk("SRC=%u.%u.%u.%u DST=%u.%u.%u.%u ",
63                NIPQUAD(ih->saddr), NIPQUAD(ih->daddr));
64
65         /* Max length: 46 "LEN=65535 TOS=0xFF PREC=0xFF TTL=255 ID=65535 " */
66         printk("LEN=%u TOS=0x%02X PREC=0x%02X TTL=%u ID=%u ",
67                ntohs(ih->tot_len), ih->tos & IPTOS_TOS_MASK,
68                ih->tos & IPTOS_PREC_MASK, ih->ttl, ntohs(ih->id));
69
70         /* Max length: 6 "CE DF MF " */
71         if (ntohs(ih->frag_off) & IP_CE)
72                 printk("CE ");
73         if (ntohs(ih->frag_off) & IP_DF)
74                 printk("DF ");
75         if (ntohs(ih->frag_off) & IP_MF)
76                 printk("MF ");
77
78         /* Max length: 11 "FRAG:65535 " */
79         if (ntohs(ih->frag_off) & IP_OFFSET)
80                 printk("FRAG:%u ", ntohs(ih->frag_off) & IP_OFFSET);
81
82         if ((logflags & IPT_LOG_IPOPT)
83             && ih->ihl * 4 > sizeof(struct iphdr)) {
84                 unsigned char _opt[4 * 15 - sizeof(struct iphdr)], *op;
85                 unsigned int i, optsize;
86
87                 optsize = ih->ihl * 4 - sizeof(struct iphdr);
88                 op = skb_header_pointer(skb, iphoff+sizeof(_iph),
89                                         optsize, _opt);
90                 if (op == NULL) {
91                         printk("TRUNCATED");
92                         return;
93                 }
94
95                 /* Max length: 127 "OPT (" 15*4*2chars ") " */
96                 printk("OPT (");
97                 for (i = 0; i < optsize; i++)
98                         printk("%02X", op[i]);
99                 printk(") ");
100         }
101
102         switch (ih->protocol) {
103         case IPPROTO_TCP: {
104                 struct tcphdr _tcph;
105                 const struct tcphdr *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                         const 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;
179                 const struct udphdr *uh;
180
181                 if (ih->protocol == IPPROTO_UDP)
182                         /* Max length: 10 "PROTO=UDP "     */
183                         printk("PROTO=UDP " );
184                 else    /* Max length: 14 "PROTO=UDPLITE " */
185                         printk("PROTO=UDPLITE ");
186
187                 if (ntohs(ih->frag_off) & IP_OFFSET)
188                         break;
189
190                 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
191                 uh = skb_header_pointer(skb, iphoff+ih->ihl*4,
192                                         sizeof(_udph), &_udph);
193                 if (uh == NULL) {
194                         printk("INCOMPLETE [%u bytes] ",
195                                skb->len - iphoff - ih->ihl*4);
196                         break;
197                 }
198
199                 /* Max length: 20 "SPT=65535 DPT=65535 " */
200                 printk("SPT=%u DPT=%u LEN=%u ",
201                        ntohs(uh->source), ntohs(uh->dest),
202                        ntohs(uh->len));
203                 break;
204         }
205         case IPPROTO_ICMP: {
206                 struct icmphdr _icmph;
207                 const struct icmphdr *ich;
208                 static const size_t required_len[NR_ICMP_TYPES+1]
209                         = { [ICMP_ECHOREPLY] = 4,
210                             [ICMP_DEST_UNREACH]
211                             = 8 + sizeof(struct iphdr),
212                             [ICMP_SOURCE_QUENCH]
213                             = 8 + sizeof(struct iphdr),
214                             [ICMP_REDIRECT]
215                             = 8 + sizeof(struct iphdr),
216                             [ICMP_ECHO] = 4,
217                             [ICMP_TIME_EXCEEDED]
218                             = 8 + sizeof(struct iphdr),
219                             [ICMP_PARAMETERPROB]
220                             = 8 + sizeof(struct iphdr),
221                             [ICMP_TIMESTAMP] = 20,
222                             [ICMP_TIMESTAMPREPLY] = 20,
223                             [ICMP_ADDRESS] = 12,
224                             [ICMP_ADDRESSREPLY] = 12 };
225
226                 /* Max length: 11 "PROTO=ICMP " */
227                 printk("PROTO=ICMP ");
228
229                 if (ntohs(ih->frag_off) & IP_OFFSET)
230                         break;
231
232                 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
233                 ich = skb_header_pointer(skb, iphoff + ih->ihl * 4,
234                                          sizeof(_icmph), &_icmph);
235                 if (ich == NULL) {
236                         printk("INCOMPLETE [%u bytes] ",
237                                skb->len - iphoff - ih->ihl*4);
238                         break;
239                 }
240
241                 /* Max length: 18 "TYPE=255 CODE=255 " */
242                 printk("TYPE=%u CODE=%u ", ich->type, ich->code);
243
244                 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
245                 if (ich->type <= NR_ICMP_TYPES
246                     && required_len[ich->type]
247                     && skb->len-iphoff-ih->ihl*4 < required_len[ich->type]) {
248                         printk("INCOMPLETE [%u bytes] ",
249                                skb->len - iphoff - ih->ihl*4);
250                         break;
251                 }
252
253                 switch (ich->type) {
254                 case ICMP_ECHOREPLY:
255                 case ICMP_ECHO:
256                         /* Max length: 19 "ID=65535 SEQ=65535 " */
257                         printk("ID=%u SEQ=%u ",
258                                ntohs(ich->un.echo.id),
259                                ntohs(ich->un.echo.sequence));
260                         break;
261
262                 case ICMP_PARAMETERPROB:
263                         /* Max length: 14 "PARAMETER=255 " */
264                         printk("PARAMETER=%u ",
265                                ntohl(ich->un.gateway) >> 24);
266                         break;
267                 case ICMP_REDIRECT:
268                         /* Max length: 24 "GATEWAY=255.255.255.255 " */
269                         printk("GATEWAY=%u.%u.%u.%u ",
270                                NIPQUAD(ich->un.gateway));
271                         /* Fall through */
272                 case ICMP_DEST_UNREACH:
273                 case ICMP_SOURCE_QUENCH:
274                 case ICMP_TIME_EXCEEDED:
275                         /* Max length: 3+maxlen */
276                         if (!iphoff) { /* Only recurse once. */
277                                 printk("[");
278                                 dump_packet(info, skb,
279                                             iphoff + ih->ihl*4+sizeof(_icmph));
280                                 printk("] ");
281                         }
282
283                         /* Max length: 10 "MTU=65535 " */
284                         if (ich->type == ICMP_DEST_UNREACH
285                             && ich->code == ICMP_FRAG_NEEDED)
286                                 printk("MTU=%u ", ntohs(ich->un.frag.mtu));
287                 }
288                 break;
289         }
290         /* Max Length */
291         case IPPROTO_AH: {
292                 struct ip_auth_hdr _ahdr;
293                 const struct ip_auth_hdr *ah;
294
295                 if (ntohs(ih->frag_off) & IP_OFFSET)
296                         break;
297
298                 /* Max length: 9 "PROTO=AH " */
299                 printk("PROTO=AH ");
300
301                 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
302                 ah = skb_header_pointer(skb, iphoff+ih->ihl*4,
303                                         sizeof(_ahdr), &_ahdr);
304                 if (ah == NULL) {
305                         printk("INCOMPLETE [%u bytes] ",
306                                skb->len - iphoff - ih->ihl*4);
307                         break;
308                 }
309
310                 /* Length: 15 "SPI=0xF1234567 " */
311                 printk("SPI=0x%x ", ntohl(ah->spi));
312                 break;
313         }
314         case IPPROTO_ESP: {
315                 struct ip_esp_hdr _esph;
316                 const struct ip_esp_hdr *eh;
317
318                 /* Max length: 10 "PROTO=ESP " */
319                 printk("PROTO=ESP ");
320
321                 if (ntohs(ih->frag_off) & IP_OFFSET)
322                         break;
323
324                 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
325                 eh = skb_header_pointer(skb, iphoff+ih->ihl*4,
326                                         sizeof(_esph), &_esph);
327                 if (eh == NULL) {
328                         printk("INCOMPLETE [%u bytes] ",
329                                skb->len - iphoff - ih->ihl*4);
330                         break;
331                 }
332
333                 /* Length: 15 "SPI=0xF1234567 " */
334                 printk("SPI=0x%x ", ntohl(eh->spi));
335                 break;
336         }
337         /* Max length: 10 "PROTO 255 " */
338         default:
339                 printk("PROTO=%u ", ih->protocol);
340         }
341
342         /* Max length: 15 "UID=4294967295 " */
343         if ((logflags & IPT_LOG_UID) && !iphoff && skb->sk) {
344                 read_lock_bh(&skb->sk->sk_callback_lock);
345                 if (skb->sk->sk_socket && skb->sk->sk_socket->file)
346                         printk("UID=%u ", skb->sk->sk_socket->file->f_uid);
347                 read_unlock_bh(&skb->sk->sk_callback_lock);
348         }
349
350         /* Proto    Max log string length */
351         /* IP:      40+46+6+11+127 = 230 */
352         /* TCP:     10+max(25,20+30+13+9+32+11+127) = 252 */
353         /* UDP:     10+max(25,20) = 35 */
354         /* UDPLITE: 14+max(25,20) = 39 */
355         /* ICMP:    11+max(25, 18+25+max(19,14,24+3+n+10,3+n+10)) = 91+n */
356         /* ESP:     10+max(25)+15 = 50 */
357         /* AH:      9+max(25)+15 = 49 */
358         /* unknown: 10 */
359
360         /* (ICMP allows recursion one level deep) */
361         /* maxlen =  IP + ICMP +  IP + max(TCP,UDP,ICMP,unknown) */
362         /* maxlen = 230+   91  + 230 + 252 = 803 */
363 }
364
365 static struct nf_loginfo default_loginfo = {
366         .type   = NF_LOG_TYPE_LOG,
367         .u = {
368                 .log = {
369                         .level    = 0,
370                         .logflags = NF_LOG_MASK,
371                 },
372         },
373 };
374
375 static void
376 ipt_log_packet(unsigned int pf,
377                unsigned int hooknum,
378                const struct sk_buff *skb,
379                const struct net_device *in,
380                const struct net_device *out,
381                const struct nf_loginfo *loginfo,
382                const char *prefix)
383 {
384         if (!loginfo)
385                 loginfo = &default_loginfo;
386
387         spin_lock_bh(&log_lock);
388         printk("<%d>%sIN=%s OUT=%s ", loginfo->u.log.level,
389                prefix,
390                in ? in->name : "",
391                out ? out->name : "");
392 #ifdef CONFIG_BRIDGE_NETFILTER
393         if (skb->nf_bridge) {
394                 const struct net_device *physindev;
395                 const struct net_device *physoutdev;
396
397                 physindev = skb->nf_bridge->physindev;
398                 if (physindev && in != physindev)
399                         printk("PHYSIN=%s ", physindev->name);
400                 physoutdev = skb->nf_bridge->physoutdev;
401                 if (physoutdev && out != physoutdev)
402                         printk("PHYSOUT=%s ", physoutdev->name);
403         }
404 #endif
405
406         if (in && !out) {
407                 /* MAC logging for input chain only. */
408                 printk("MAC=");
409                 if (skb->dev && skb->dev->hard_header_len
410                     && skb->mac_header != skb->network_header) {
411                         int i;
412                         const unsigned char *p = skb_mac_header(skb);
413                         for (i = 0; i < skb->dev->hard_header_len; i++,p++)
414                                 printk("%02x%c", *p,
415                                        i==skb->dev->hard_header_len - 1
416                                        ? ' ':':');
417                 } else
418                         printk(" ");
419         }
420
421         dump_packet(loginfo, skb, 0);
422         printk("\n");
423         spin_unlock_bh(&log_lock);
424 }
425
426 static unsigned int
427 ipt_log_target(struct sk_buff **pskb,
428                const struct net_device *in,
429                const struct net_device *out,
430                unsigned int hooknum,
431                const struct xt_target *target,
432                const void *targinfo)
433 {
434         const struct ipt_log_info *loginfo = targinfo;
435         struct nf_loginfo li;
436
437         li.type = NF_LOG_TYPE_LOG;
438         li.u.log.level = loginfo->level;
439         li.u.log.logflags = loginfo->logflags;
440
441         ipt_log_packet(PF_INET, hooknum, *pskb, in, out, &li,
442                        loginfo->prefix);
443         return XT_CONTINUE;
444 }
445
446 static bool ipt_log_checkentry(const char *tablename,
447                                const void *e,
448                                const struct xt_target *target,
449                                void *targinfo,
450                                unsigned int hook_mask)
451 {
452         const struct ipt_log_info *loginfo = targinfo;
453
454         if (loginfo->level >= 8) {
455                 DEBUGP("LOG: level %u >= 8\n", loginfo->level);
456                 return false;
457         }
458         if (loginfo->prefix[sizeof(loginfo->prefix)-1] != '\0') {
459                 DEBUGP("LOG: prefix term %i\n",
460                        loginfo->prefix[sizeof(loginfo->prefix)-1]);
461                 return false;
462         }
463         return true;
464 }
465
466 static struct xt_target ipt_log_reg = {
467         .name           = "LOG",
468         .family         = AF_INET,
469         .target         = ipt_log_target,
470         .targetsize     = sizeof(struct ipt_log_info),
471         .checkentry     = ipt_log_checkentry,
472         .me             = THIS_MODULE,
473 };
474
475 static struct nf_logger ipt_log_logger ={
476         .name           = "ipt_LOG",
477         .logfn          = &ipt_log_packet,
478         .me             = THIS_MODULE,
479 };
480
481 static int __init ipt_log_init(void)
482 {
483         int ret;
484
485         ret = xt_register_target(&ipt_log_reg);
486         if (ret < 0)
487                 return ret;
488         ret = nf_log_register(PF_INET, &ipt_log_logger);
489         if (ret < 0 && ret != -EEXIST)
490                 xt_unregister_target(&ipt_log_reg);
491         return ret;
492 }
493
494 static void __exit ipt_log_fini(void)
495 {
496         nf_log_unregister(&ipt_log_logger);
497         xt_unregister_target(&ipt_log_reg);
498 }
499
500 module_init(ipt_log_init);
501 module_exit(ipt_log_fini);