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