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