]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/ipv4/netfilter/ipt_LOG.c
netfilter: xtables: substitute temporary defines by final name
[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 */
ff67e4e4 12#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
1da177e4
LT
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>
6709dbbb 23#include <linux/netfilter/x_tables.h>
1da177e4 24#include <linux/netfilter_ipv4/ipt_LOG.h>
f01ffbd6 25#include <net/netfilter/nf_log.h>
1da177e4
LT
26
27MODULE_LICENSE("GPL");
28MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
2ae15b64 29MODULE_DESCRIPTION("Xtables: IPv4 packet logging to syslog");
1da177e4 30
1da177e4
LT
31/* Use lock to serialize, so printks don't overlap */
32static DEFINE_SPINLOCK(log_lock);
33
34/* One level of recursion won't kill us */
608c8e4f 35static void dump_packet(const struct nf_loginfo *info,
1da177e4
LT
36 const struct sk_buff *skb,
37 unsigned int iphoff)
38{
a47362a2
JE
39 struct iphdr _iph;
40 const struct iphdr *ih;
608c8e4f
HW
41 unsigned int logflags;
42
43 if (info->type == NF_LOG_TYPE_LOG)
44 logflags = info->u.log.logflags;
45 else
46 logflags = NF_LOG_MASK;
1da177e4
LT
47
48 ih = skb_header_pointer(skb, iphoff, sizeof(_iph), &_iph);
49 if (ih == NULL) {
50 printk("TRUNCATED");
51 return;
52 }
53
54 /* Important fields:
55 * TOS, len, DF/MF, fragment offset, TTL, src, dst, options. */
56 /* Max length: 40 "SRC=255.255.255.255 DST=255.255.255.255 " */
cffee385
HH
57 printk("SRC=%pI4 DST=%pI4 ",
58 &ih->saddr, &ih->daddr);
1da177e4
LT
59
60 /* Max length: 46 "LEN=65535 TOS=0xFF PREC=0xFF TTL=255 ID=65535 " */
61 printk("LEN=%u TOS=0x%02X PREC=0x%02X TTL=%u ID=%u ",
62 ntohs(ih->tot_len), ih->tos & IPTOS_TOS_MASK,
63 ih->tos & IPTOS_PREC_MASK, ih->ttl, ntohs(ih->id));
64
65 /* Max length: 6 "CE DF MF " */
66 if (ntohs(ih->frag_off) & IP_CE)
67 printk("CE ");
68 if (ntohs(ih->frag_off) & IP_DF)
69 printk("DF ");
70 if (ntohs(ih->frag_off) & IP_MF)
71 printk("MF ");
72
73 /* Max length: 11 "FRAG:65535 " */
74 if (ntohs(ih->frag_off) & IP_OFFSET)
75 printk("FRAG:%u ", ntohs(ih->frag_off) & IP_OFFSET);
76
3666ed1c
JP
77 if ((logflags & IPT_LOG_IPOPT) &&
78 ih->ihl * 4 > sizeof(struct iphdr)) {
3cf93c96
JE
79 const unsigned char *op;
80 unsigned char _opt[4 * 15 - sizeof(struct iphdr)];
1da177e4
LT
81 unsigned int i, optsize;
82
83 optsize = ih->ihl * 4 - sizeof(struct iphdr);
84 op = skb_header_pointer(skb, iphoff+sizeof(_iph),
85 optsize, _opt);
86 if (op == NULL) {
87 printk("TRUNCATED");
88 return;
89 }
90
91 /* Max length: 127 "OPT (" 15*4*2chars ") " */
92 printk("OPT (");
93 for (i = 0; i < optsize; i++)
94 printk("%02X", op[i]);
95 printk(") ");
96 }
97
98 switch (ih->protocol) {
99 case IPPROTO_TCP: {
a47362a2
JE
100 struct tcphdr _tcph;
101 const struct tcphdr *th;
1da177e4
LT
102
103 /* Max length: 10 "PROTO=TCP " */
104 printk("PROTO=TCP ");
105
106 if (ntohs(ih->frag_off) & IP_OFFSET)
107 break;
108
109 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
110 th = skb_header_pointer(skb, iphoff + ih->ihl * 4,
111 sizeof(_tcph), &_tcph);
112 if (th == NULL) {
113 printk("INCOMPLETE [%u bytes] ",
114 skb->len - iphoff - ih->ihl*4);
115 break;
116 }
117
118 /* Max length: 20 "SPT=65535 DPT=65535 " */
119 printk("SPT=%u DPT=%u ",
120 ntohs(th->source), ntohs(th->dest));
121 /* Max length: 30 "SEQ=4294967295 ACK=4294967295 " */
608c8e4f 122 if (logflags & IPT_LOG_TCPSEQ)
1da177e4
LT
123 printk("SEQ=%u ACK=%u ",
124 ntohl(th->seq), ntohl(th->ack_seq));
125 /* Max length: 13 "WINDOW=65535 " */
126 printk("WINDOW=%u ", ntohs(th->window));
127 /* Max length: 9 "RES=0x3F " */
128 printk("RES=0x%02x ", (u8)(ntohl(tcp_flag_word(th) & TCP_RESERVED_BITS) >> 22));
129 /* Max length: 32 "CWR ECE URG ACK PSH RST SYN FIN " */
130 if (th->cwr)
131 printk("CWR ");
132 if (th->ece)
133 printk("ECE ");
134 if (th->urg)
135 printk("URG ");
136 if (th->ack)
137 printk("ACK ");
138 if (th->psh)
139 printk("PSH ");
140 if (th->rst)
141 printk("RST ");
142 if (th->syn)
143 printk("SYN ");
144 if (th->fin)
145 printk("FIN ");
146 /* Max length: 11 "URGP=65535 " */
147 printk("URGP=%u ", ntohs(th->urg_ptr));
148
3666ed1c
JP
149 if ((logflags & IPT_LOG_TCPOPT) &&
150 th->doff * 4 > sizeof(struct tcphdr)) {
1da177e4 151 unsigned char _opt[4 * 15 - sizeof(struct tcphdr)];
a47362a2 152 const unsigned char *op;
1da177e4
LT
153 unsigned int i, optsize;
154
155 optsize = th->doff * 4 - sizeof(struct tcphdr);
156 op = skb_header_pointer(skb,
157 iphoff+ih->ihl*4+sizeof(_tcph),
158 optsize, _opt);
159 if (op == NULL) {
160 printk("TRUNCATED");
161 return;
162 }
163
164 /* Max length: 127 "OPT (" 15*4*2chars ") " */
165 printk("OPT (");
166 for (i = 0; i < optsize; i++)
167 printk("%02X", op[i]);
168 printk(") ");
169 }
170 break;
171 }
ba4e58ec
GR
172 case IPPROTO_UDP:
173 case IPPROTO_UDPLITE: {
a47362a2
JE
174 struct udphdr _udph;
175 const struct udphdr *uh;
1da177e4 176
ba4e58ec
GR
177 if (ih->protocol == IPPROTO_UDP)
178 /* Max length: 10 "PROTO=UDP " */
179 printk("PROTO=UDP " );
180 else /* Max length: 14 "PROTO=UDPLITE " */
181 printk("PROTO=UDPLITE ");
1da177e4
LT
182
183 if (ntohs(ih->frag_off) & IP_OFFSET)
184 break;
185
186 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
187 uh = skb_header_pointer(skb, iphoff+ih->ihl*4,
188 sizeof(_udph), &_udph);
189 if (uh == NULL) {
190 printk("INCOMPLETE [%u bytes] ",
191 skb->len - iphoff - ih->ihl*4);
192 break;
193 }
194
195 /* Max length: 20 "SPT=65535 DPT=65535 " */
196 printk("SPT=%u DPT=%u LEN=%u ",
197 ntohs(uh->source), ntohs(uh->dest),
198 ntohs(uh->len));
199 break;
200 }
201 case IPPROTO_ICMP: {
a47362a2
JE
202 struct icmphdr _icmph;
203 const struct icmphdr *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] " */
3666ed1c
JP
241 if (ich->type <= NR_ICMP_TYPES &&
242 required_len[ich->type] &&
243 skb->len-iphoff-ih->ihl*4 < required_len[ich->type]) {
1da177e4
LT
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 " */
cffee385 265 printk("GATEWAY=%pI4 ", &ich->un.gateway);
1da177e4
LT
266 /* Fall through */
267 case ICMP_DEST_UNREACH:
268 case ICMP_SOURCE_QUENCH:
269 case ICMP_TIME_EXCEEDED:
270 /* Max length: 3+maxlen */
271 if (!iphoff) { /* Only recurse once. */
272 printk("[");
273 dump_packet(info, skb,
274 iphoff + ih->ihl*4+sizeof(_icmph));
275 printk("] ");
276 }
277
278 /* Max length: 10 "MTU=65535 " */
3666ed1c
JP
279 if (ich->type == ICMP_DEST_UNREACH &&
280 ich->code == ICMP_FRAG_NEEDED)
1da177e4
LT
281 printk("MTU=%u ", ntohs(ich->un.frag.mtu));
282 }
283 break;
284 }
285 /* Max Length */
286 case IPPROTO_AH: {
a47362a2
JE
287 struct ip_auth_hdr _ahdr;
288 const struct ip_auth_hdr *ah;
1da177e4
LT
289
290 if (ntohs(ih->frag_off) & IP_OFFSET)
291 break;
e905a9ed 292
1da177e4
LT
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: {
a47362a2
JE
310 struct ip_esp_hdr _esph;
311 const struct ip_esp_hdr *eh;
1da177e4
LT
312
313 /* Max length: 10 "PROTO=ESP " */
314 printk("PROTO=ESP ");
315
316 if (ntohs(ih->frag_off) & IP_OFFSET)
317 break;
318
319 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
320 eh = skb_header_pointer(skb, iphoff+ih->ihl*4,
321 sizeof(_esph), &_esph);
322 if (eh == NULL) {
323 printk("INCOMPLETE [%u bytes] ",
324 skb->len - iphoff - ih->ihl*4);
325 break;
326 }
327
328 /* Length: 15 "SPI=0xF1234567 " */
329 printk("SPI=0x%x ", ntohl(eh->spi));
330 break;
331 }
332 /* Max length: 10 "PROTO 255 " */
333 default:
334 printk("PROTO=%u ", ih->protocol);
335 }
336
337 /* Max length: 15 "UID=4294967295 " */
e905a9ed 338 if ((logflags & IPT_LOG_UID) && !iphoff && skb->sk) {
1da177e4
LT
339 read_lock_bh(&skb->sk->sk_callback_lock);
340 if (skb->sk->sk_socket && skb->sk->sk_socket->file)
36e2a1b0 341 printk("UID=%u GID=%u ",
d76b0d9b
DH
342 skb->sk->sk_socket->file->f_cred->fsuid,
343 skb->sk->sk_socket->file->f_cred->fsgid);
1da177e4
LT
344 read_unlock_bh(&skb->sk->sk_callback_lock);
345 }
346
36e2a1b0
PM
347 /* Max length: 16 "MARK=0xFFFFFFFF " */
348 if (!iphoff && skb->mark)
349 printk("MARK=0x%x ", skb->mark);
350
1da177e4
LT
351 /* Proto Max log string length */
352 /* IP: 40+46+6+11+127 = 230 */
353 /* TCP: 10+max(25,20+30+13+9+32+11+127) = 252 */
354 /* UDP: 10+max(25,20) = 35 */
ba4e58ec 355 /* UDPLITE: 14+max(25,20) = 39 */
1da177e4
LT
356 /* ICMP: 11+max(25, 18+25+max(19,14,24+3+n+10,3+n+10)) = 91+n */
357 /* ESP: 10+max(25)+15 = 50 */
358 /* AH: 9+max(25)+15 = 49 */
359 /* unknown: 10 */
360
361 /* (ICMP allows recursion one level deep) */
362 /* maxlen = IP + ICMP + IP + max(TCP,UDP,ICMP,unknown) */
363 /* maxlen = 230+ 91 + 230 + 252 = 803 */
364}
365
d127e94a 366static struct nf_loginfo default_loginfo = {
608c8e4f
HW
367 .type = NF_LOG_TYPE_LOG,
368 .u = {
369 .log = {
f0d57a54 370 .level = 5,
608c8e4f
HW
371 .logflags = NF_LOG_MASK,
372 },
373 },
374};
375
1da177e4 376static void
76108cea 377ipt_log_packet(u_int8_t pf,
608c8e4f 378 unsigned int hooknum,
1da177e4
LT
379 const struct sk_buff *skb,
380 const struct net_device *in,
381 const struct net_device *out,
608c8e4f 382 const struct nf_loginfo *loginfo,
1da177e4
LT
383 const char *prefix)
384{
608c8e4f
HW
385 if (!loginfo)
386 loginfo = &default_loginfo;
387
1da177e4 388 spin_lock_bh(&log_lock);
608c8e4f
HW
389 printk("<%d>%sIN=%s OUT=%s ", loginfo->u.log.level,
390 prefix,
1da177e4
LT
391 in ? in->name : "",
392 out ? out->name : "");
393#ifdef CONFIG_BRIDGE_NETFILTER
394 if (skb->nf_bridge) {
a47362a2
JE
395 const struct net_device *physindev;
396 const struct net_device *physoutdev;
1da177e4 397
a47362a2 398 physindev = skb->nf_bridge->physindev;
1da177e4
LT
399 if (physindev && in != physindev)
400 printk("PHYSIN=%s ", physindev->name);
a47362a2 401 physoutdev = skb->nf_bridge->physoutdev;
1da177e4
LT
402 if (physoutdev && out != physoutdev)
403 printk("PHYSOUT=%s ", physoutdev->name);
404 }
405#endif
406
407 if (in && !out) {
408 /* MAC logging for input chain only. */
409 printk("MAC=");
3666ed1c
JP
410 if (skb->dev && skb->dev->hard_header_len &&
411 skb->mac_header != skb->network_header) {
1da177e4 412 int i;
98e399f8 413 const unsigned char *p = skb_mac_header(skb);
1da177e4
LT
414 for (i = 0; i < skb->dev->hard_header_len; i++,p++)
415 printk("%02x%c", *p,
416 i==skb->dev->hard_header_len - 1
417 ? ' ':':');
418 } else
419 printk(" ");
420 }
421
422 dump_packet(loginfo, skb, 0);
423 printk("\n");
424 spin_unlock_bh(&log_lock);
425}
426
427static unsigned int
4b560b44 428log_tg(struct sk_buff *skb, const struct xt_action_param *par)
1da177e4 429{
7eb35586 430 const struct ipt_log_info *loginfo = par->targinfo;
608c8e4f 431 struct nf_loginfo li;
1da177e4 432
608c8e4f
HW
433 li.type = NF_LOG_TYPE_LOG;
434 li.u.log.level = loginfo->level;
435 li.u.log.logflags = loginfo->logflags;
1da177e4 436
7eb35586 437 ipt_log_packet(NFPROTO_IPV4, par->hooknum, skb, par->in, par->out, &li,
e905a9ed 438 loginfo->prefix);
6709dbbb 439 return XT_CONTINUE;
1da177e4
LT
440}
441
135367b8 442static int log_tg_check(const struct xt_tgchk_param *par)
1da177e4 443{
af5d6dc2 444 const struct ipt_log_info *loginfo = par->targinfo;
1da177e4 445
1da177e4 446 if (loginfo->level >= 8) {
ff67e4e4 447 pr_debug("level %u >= 8\n", loginfo->level);
d6b00a53 448 return -EINVAL;
1da177e4 449 }
1da177e4 450 if (loginfo->prefix[sizeof(loginfo->prefix)-1] != '\0') {
ff67e4e4 451 pr_debug("prefix is not null-terminated\n");
d6b00a53 452 return -EINVAL;
1da177e4 453 }
d6b00a53 454 return 0;
1da177e4
LT
455}
456
d3c5ee6d 457static struct xt_target log_tg_reg __read_mostly = {
1da177e4 458 .name = "LOG",
ee999d8b 459 .family = NFPROTO_IPV4,
d3c5ee6d 460 .target = log_tg,
1d5cd909 461 .targetsize = sizeof(struct ipt_log_info),
d3c5ee6d 462 .checkentry = log_tg_check,
1da177e4
LT
463 .me = THIS_MODULE,
464};
465
ca735b3a 466static struct nf_logger ipt_log_logger __read_mostly = {
608c8e4f
HW
467 .name = "ipt_LOG",
468 .logfn = &ipt_log_packet,
469 .me = THIS_MODULE,
470};
471
d3c5ee6d 472static int __init log_tg_init(void)
1da177e4 473{
e1fd0586
JE
474 int ret;
475
d3c5ee6d 476 ret = xt_register_target(&log_tg_reg);
e1fd0586
JE
477 if (ret < 0)
478 return ret;
ee999d8b 479 nf_log_register(NFPROTO_IPV4, &ipt_log_logger);
7e2acc7e 480 return 0;
1da177e4
LT
481}
482
d3c5ee6d 483static void __exit log_tg_exit(void)
1da177e4 484{
e92ad99c 485 nf_log_unregister(&ipt_log_logger);
d3c5ee6d 486 xt_unregister_target(&log_tg_reg);
1da177e4
LT
487}
488
d3c5ee6d
JE
489module_init(log_tg_init);
490module_exit(log_tg_exit);