blob: 73c6300109d61d7243e89f5268d3d9fbe3ec3e30 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020016#include <linux/if_arp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/ip.h>
18#include <linux/spinlock.h>
19#include <linux/icmpv6.h>
20#include <net/udp.h>
21#include <net/tcp.h>
22#include <net/ipv6.h>
23#include <linux/netfilter.h>
24#include <linux/netfilter_ipv6/ip6_tables.h>
25
26MODULE_AUTHOR("Jan Rekorajski <baggins@pld.org.pl>");
27MODULE_DESCRIPTION("IP6 tables LOG target module");
28MODULE_LICENSE("GPL");
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030struct in_device;
31#include <net/route.h>
32#include <linux/netfilter_ipv6/ip6t_LOG.h>
33
34#if 0
35#define DEBUGP printk
36#else
37#define DEBUGP(format, args...)
38#endif
39
40/* Use lock to serialize, so printks don't overlap */
41static DEFINE_SPINLOCK(log_lock);
42
43/* One level of recursion won't kill us */
Harald Welte608c8e42005-08-09 19:58:27 -070044static void dump_packet(const struct nf_loginfo *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 const struct sk_buff *skb, unsigned int ip6hoff,
46 int recurse)
47{
48 u_int8_t currenthdr;
49 int fragment;
50 struct ipv6hdr _ip6h, *ih;
51 unsigned int ptr;
52 unsigned int hdrlen = 0;
Harald Welte608c8e42005-08-09 19:58:27 -070053 unsigned int logflags;
54
55 if (info->type == NF_LOG_TYPE_LOG)
56 logflags = info->u.log.logflags;
57 else
58 logflags = NF_LOG_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60 ih = skb_header_pointer(skb, ip6hoff, sizeof(_ip6h), &_ip6h);
61 if (ih == NULL) {
62 printk("TRUNCATED");
63 return;
64 }
65
Joe Perches46b86a22006-01-13 14:29:07 -080066 /* Max length: 88 "SRC=0000.0000.0000.0000.0000.0000.0000.0000 DST=0000.0000.0000.0000.0000.0000.0000.0000 " */
67 printk("SRC=" NIP6_FMT " DST=" NIP6_FMT " ", NIP6(ih->saddr), NIP6(ih->daddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69 /* Max length: 44 "LEN=65535 TC=255 HOPLIMIT=255 FLOWLBL=FFFFF " */
70 printk("LEN=%Zu TC=%u HOPLIMIT=%u FLOWLBL=%u ",
71 ntohs(ih->payload_len) + sizeof(struct ipv6hdr),
72 (ntohl(*(u_int32_t *)ih) & 0x0ff00000) >> 20,
73 ih->hop_limit,
74 (ntohl(*(u_int32_t *)ih) & 0x000fffff));
75
76 fragment = 0;
77 ptr = ip6hoff + sizeof(struct ipv6hdr);
78 currenthdr = ih->nexthdr;
79 while (currenthdr != NEXTHDR_NONE && ip6t_ext_hdr(currenthdr)) {
80 struct ipv6_opt_hdr _hdr, *hp;
81
82 hp = skb_header_pointer(skb, ptr, sizeof(_hdr), &_hdr);
83 if (hp == NULL) {
84 printk("TRUNCATED");
85 return;
86 }
87
88 /* Max length: 48 "OPT (...) " */
Harald Welte608c8e42005-08-09 19:58:27 -070089 if (logflags & IP6T_LOG_IPOPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 printk("OPT ( ");
91
92 switch (currenthdr) {
93 case IPPROTO_FRAGMENT: {
94 struct frag_hdr _fhdr, *fh;
95
96 printk("FRAG:");
97 fh = skb_header_pointer(skb, ptr, sizeof(_fhdr),
98 &_fhdr);
99 if (fh == NULL) {
100 printk("TRUNCATED ");
101 return;
102 }
103
104 /* Max length: 6 "65535 " */
105 printk("%u ", ntohs(fh->frag_off) & 0xFFF8);
106
107 /* Max length: 11 "INCOMPLETE " */
108 if (fh->frag_off & htons(0x0001))
109 printk("INCOMPLETE ");
110
111 printk("ID:%08x ", ntohl(fh->identification));
112
113 if (ntohs(fh->frag_off) & 0xFFF8)
114 fragment = 1;
115
116 hdrlen = 8;
117
118 break;
119 }
120 case IPPROTO_DSTOPTS:
121 case IPPROTO_ROUTING:
122 case IPPROTO_HOPOPTS:
123 if (fragment) {
Harald Welte608c8e42005-08-09 19:58:27 -0700124 if (logflags & IP6T_LOG_IPOPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 printk(")");
126 return;
127 }
128 hdrlen = ipv6_optlen(hp);
129 break;
130 /* Max Length */
131 case IPPROTO_AH:
Harald Welte608c8e42005-08-09 19:58:27 -0700132 if (logflags & IP6T_LOG_IPOPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 struct ip_auth_hdr _ahdr, *ah;
134
135 /* Max length: 3 "AH " */
136 printk("AH ");
137
138 if (fragment) {
139 printk(")");
140 return;
141 }
142
143 ah = skb_header_pointer(skb, ptr, sizeof(_ahdr),
144 &_ahdr);
145 if (ah == NULL) {
146 /*
147 * Max length: 26 "INCOMPLETE [65535
148 * bytes] )"
149 */
150 printk("INCOMPLETE [%u bytes] )",
151 skb->len - ptr);
152 return;
153 }
154
155 /* Length: 15 "SPI=0xF1234567 */
156 printk("SPI=0x%x ", ntohl(ah->spi));
157
158 }
159
160 hdrlen = (hp->hdrlen+2)<<2;
161 break;
162 case IPPROTO_ESP:
Harald Welte608c8e42005-08-09 19:58:27 -0700163 if (logflags & IP6T_LOG_IPOPT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 struct ip_esp_hdr _esph, *eh;
165
166 /* Max length: 4 "ESP " */
167 printk("ESP ");
168
169 if (fragment) {
170 printk(")");
171 return;
172 }
173
174 /*
175 * Max length: 26 "INCOMPLETE [65535 bytes] )"
176 */
177 eh = skb_header_pointer(skb, ptr, sizeof(_esph),
178 &_esph);
179 if (eh == NULL) {
180 printk("INCOMPLETE [%u bytes] )",
181 skb->len - ptr);
182 return;
183 }
184
185 /* Length: 16 "SPI=0xF1234567 )" */
186 printk("SPI=0x%x )", ntohl(eh->spi) );
187
188 }
189 return;
190 default:
191 /* Max length: 20 "Unknown Ext Hdr 255" */
192 printk("Unknown Ext Hdr %u", currenthdr);
193 return;
194 }
Harald Welte608c8e42005-08-09 19:58:27 -0700195 if (logflags & IP6T_LOG_IPOPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 printk(") ");
197
198 currenthdr = hp->nexthdr;
199 ptr += hdrlen;
200 }
201
202 switch (currenthdr) {
203 case IPPROTO_TCP: {
204 struct tcphdr _tcph, *th;
205
206 /* Max length: 10 "PROTO=TCP " */
207 printk("PROTO=TCP ");
208
209 if (fragment)
210 break;
211
212 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
213 th = skb_header_pointer(skb, ptr, sizeof(_tcph), &_tcph);
214 if (th == NULL) {
215 printk("INCOMPLETE [%u bytes] ", skb->len - ptr);
216 return;
217 }
218
219 /* Max length: 20 "SPT=65535 DPT=65535 " */
220 printk("SPT=%u DPT=%u ",
221 ntohs(th->source), ntohs(th->dest));
222 /* Max length: 30 "SEQ=4294967295 ACK=4294967295 " */
Harald Welte608c8e42005-08-09 19:58:27 -0700223 if (logflags & IP6T_LOG_TCPSEQ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 printk("SEQ=%u ACK=%u ",
225 ntohl(th->seq), ntohl(th->ack_seq));
226 /* Max length: 13 "WINDOW=65535 " */
227 printk("WINDOW=%u ", ntohs(th->window));
228 /* Max length: 9 "RES=0x3C " */
229 printk("RES=0x%02x ", (u_int8_t)(ntohl(tcp_flag_word(th) & TCP_RESERVED_BITS) >> 22));
230 /* Max length: 32 "CWR ECE URG ACK PSH RST SYN FIN " */
231 if (th->cwr)
232 printk("CWR ");
233 if (th->ece)
234 printk("ECE ");
235 if (th->urg)
236 printk("URG ");
237 if (th->ack)
238 printk("ACK ");
239 if (th->psh)
240 printk("PSH ");
241 if (th->rst)
242 printk("RST ");
243 if (th->syn)
244 printk("SYN ");
245 if (th->fin)
246 printk("FIN ");
247 /* Max length: 11 "URGP=65535 " */
248 printk("URGP=%u ", ntohs(th->urg_ptr));
249
Harald Welte608c8e42005-08-09 19:58:27 -0700250 if ((logflags & IP6T_LOG_TCPOPT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 && th->doff * 4 > sizeof(struct tcphdr)) {
252 u_int8_t _opt[60 - sizeof(struct tcphdr)], *op;
253 unsigned int i;
254 unsigned int optsize = th->doff * 4
255 - sizeof(struct tcphdr);
256
257 op = skb_header_pointer(skb,
258 ptr + sizeof(struct tcphdr),
259 optsize, _opt);
260 if (op == NULL) {
261 printk("OPT (TRUNCATED)");
262 return;
263 }
264
265 /* Max length: 127 "OPT (" 15*4*2chars ") " */
266 printk("OPT (");
267 for (i =0; i < optsize; i++)
268 printk("%02X", op[i]);
269 printk(") ");
270 }
271 break;
272 }
273 case IPPROTO_UDP: {
274 struct udphdr _udph, *uh;
275
276 /* Max length: 10 "PROTO=UDP " */
277 printk("PROTO=UDP ");
278
279 if (fragment)
280 break;
281
282 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
283 uh = skb_header_pointer(skb, ptr, sizeof(_udph), &_udph);
284 if (uh == NULL) {
285 printk("INCOMPLETE [%u bytes] ", skb->len - ptr);
286 return;
287 }
288
289 /* Max length: 20 "SPT=65535 DPT=65535 " */
290 printk("SPT=%u DPT=%u LEN=%u ",
291 ntohs(uh->source), ntohs(uh->dest),
292 ntohs(uh->len));
293 break;
294 }
295 case IPPROTO_ICMPV6: {
296 struct icmp6hdr _icmp6h, *ic;
297
298 /* Max length: 13 "PROTO=ICMPv6 " */
299 printk("PROTO=ICMPv6 ");
300
301 if (fragment)
302 break;
303
304 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
305 ic = skb_header_pointer(skb, ptr, sizeof(_icmp6h), &_icmp6h);
306 if (ic == NULL) {
307 printk("INCOMPLETE [%u bytes] ", skb->len - ptr);
308 return;
309 }
310
311 /* Max length: 18 "TYPE=255 CODE=255 " */
312 printk("TYPE=%u CODE=%u ", ic->icmp6_type, ic->icmp6_code);
313
314 switch (ic->icmp6_type) {
315 case ICMPV6_ECHO_REQUEST:
316 case ICMPV6_ECHO_REPLY:
317 /* Max length: 19 "ID=65535 SEQ=65535 " */
318 printk("ID=%u SEQ=%u ",
319 ntohs(ic->icmp6_identifier),
320 ntohs(ic->icmp6_sequence));
321 break;
322 case ICMPV6_MGM_QUERY:
323 case ICMPV6_MGM_REPORT:
324 case ICMPV6_MGM_REDUCTION:
325 break;
326
327 case ICMPV6_PARAMPROB:
328 /* Max length: 17 "POINTER=ffffffff " */
329 printk("POINTER=%08x ", ntohl(ic->icmp6_pointer));
330 /* Fall through */
331 case ICMPV6_DEST_UNREACH:
332 case ICMPV6_PKT_TOOBIG:
333 case ICMPV6_TIME_EXCEED:
334 /* Max length: 3+maxlen */
335 if (recurse) {
336 printk("[");
337 dump_packet(info, skb, ptr + sizeof(_icmp6h),
338 0);
339 printk("] ");
340 }
341
342 /* Max length: 10 "MTU=65535 " */
343 if (ic->icmp6_type == ICMPV6_PKT_TOOBIG)
344 printk("MTU=%u ", ntohl(ic->icmp6_mtu));
345 }
346 break;
347 }
348 /* Max length: 10 "PROTO=255 " */
349 default:
350 printk("PROTO=%u ", currenthdr);
351 }
352
353 /* Max length: 15 "UID=4294967295 " */
Harald Welte608c8e42005-08-09 19:58:27 -0700354 if ((logflags & IP6T_LOG_UID) && recurse && skb->sk) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 read_lock_bh(&skb->sk->sk_callback_lock);
356 if (skb->sk->sk_socket && skb->sk->sk_socket->file)
357 printk("UID=%u ", skb->sk->sk_socket->file->f_uid);
358 read_unlock_bh(&skb->sk->sk_callback_lock);
359 }
360}
361
Harald Welte608c8e42005-08-09 19:58:27 -0700362static struct nf_loginfo default_loginfo = {
363 .type = NF_LOG_TYPE_LOG,
364 .u = {
365 .log = {
366 .level = 0,
367 .logflags = NF_LOG_MASK,
368 },
369 },
370};
371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372static void
Harald Welte608c8e42005-08-09 19:58:27 -0700373ip6t_log_packet(unsigned int pf,
374 unsigned int hooknum,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 const struct sk_buff *skb,
376 const struct net_device *in,
377 const struct net_device *out,
Harald Welte608c8e42005-08-09 19:58:27 -0700378 const struct nf_loginfo *loginfo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 const char *prefix)
380{
Harald Welte608c8e42005-08-09 19:58:27 -0700381 if (!loginfo)
382 loginfo = &default_loginfo;
383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 spin_lock_bh(&log_lock);
Harald Welte608c8e42005-08-09 19:58:27 -0700385 printk("<%d>%sIN=%s OUT=%s ", loginfo->u.log.level,
386 prefix,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 in ? in->name : "",
388 out ? out->name : "");
389 if (in && !out) {
Patrick McHardyd3984a62005-07-22 12:52:47 -0700390 unsigned int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 /* MAC logging for input chain only. */
392 printk("MAC=");
Patrick McHardyd3984a62005-07-22 12:52:47 -0700393 if (skb->dev && (len = skb->dev->hard_header_len) &&
Patrick McHardy047601b2005-06-21 14:07:13 -0700394 skb->mac.raw != skb->nh.raw) {
395 unsigned char *p = skb->mac.raw;
396 int i;
397
398 if (skb->dev->type == ARPHRD_SIT &&
399 (p -= ETH_HLEN) < skb->head)
400 p = NULL;
401
Patrick McHardyd3984a62005-07-22 12:52:47 -0700402 if (p != NULL) {
403 for (i = 0; i < len; i++)
404 printk("%02x%s", p[i],
405 i == len - 1 ? "" : ":");
406 }
Patrick McHardy047601b2005-06-21 14:07:13 -0700407 printk(" ");
408
409 if (skb->dev->type == ARPHRD_SIT) {
410 struct iphdr *iph = (struct iphdr *)skb->mac.raw;
411 printk("TUNNEL=%u.%u.%u.%u->%u.%u.%u.%u ",
412 NIPQUAD(iph->saddr),
413 NIPQUAD(iph->daddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 }
415 } else
416 printk(" ");
417 }
418
419 dump_packet(loginfo, skb, (u8*)skb->nh.ipv6h - skb->data, 1);
420 printk("\n");
421 spin_unlock_bh(&log_lock);
422}
423
424static unsigned int
425ip6t_log_target(struct sk_buff **pskb,
426 const struct net_device *in,
427 const struct net_device *out,
428 unsigned int hooknum,
Patrick McHardyc4986732006-03-20 18:02:56 -0800429 const struct xt_target *target,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 const void *targinfo,
431 void *userinfo)
432{
433 const struct ip6t_log_info *loginfo = targinfo;
Harald Welte608c8e42005-08-09 19:58:27 -0700434 struct nf_loginfo li;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Harald Welte608c8e42005-08-09 19:58:27 -0700436 li.type = NF_LOG_TYPE_LOG;
437 li.u.log.level = loginfo->level;
438 li.u.log.logflags = loginfo->logflags;
439
Patrick McHardybafac2a2006-02-27 13:04:17 -0800440 if (loginfo->logflags & IP6T_LOG_NFLOG)
441 nf_log_packet(PF_INET6, hooknum, *pskb, in, out, &li,
Philip Craig5c170a02006-05-19 02:15:47 -0700442 "%s", loginfo->prefix);
Patrick McHardybafac2a2006-02-27 13:04:17 -0800443 else
444 ip6t_log_packet(PF_INET6, hooknum, *pskb, in, out, &li,
445 loginfo->prefix);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447 return IP6T_CONTINUE;
448}
449
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451static int ip6t_log_checkentry(const char *tablename,
Harald Welte2e4e6a12006-01-12 13:30:04 -0800452 const void *entry,
Patrick McHardyc4986732006-03-20 18:02:56 -0800453 const struct xt_target *target,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 void *targinfo,
455 unsigned int targinfosize,
456 unsigned int hook_mask)
457{
458 const struct ip6t_log_info *loginfo = targinfo;
459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 if (loginfo->level >= 8) {
461 DEBUGP("LOG: level %u >= 8\n", loginfo->level);
462 return 0;
463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 if (loginfo->prefix[sizeof(loginfo->prefix)-1] != '\0') {
465 DEBUGP("LOG: prefix term %i\n",
466 loginfo->prefix[sizeof(loginfo->prefix)-1]);
467 return 0;
468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 return 1;
470}
471
472static struct ip6t_target ip6t_log_reg = {
473 .name = "LOG",
474 .target = ip6t_log_target,
Patrick McHardy7f939712006-03-20 18:01:43 -0800475 .targetsize = sizeof(struct ip6t_log_info),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 .checkentry = ip6t_log_checkentry,
477 .me = THIS_MODULE,
478};
479
Harald Welte608c8e42005-08-09 19:58:27 -0700480static struct nf_logger ip6t_logger = {
481 .name = "ip6t_LOG",
482 .logfn = &ip6t_log_packet,
483 .me = THIS_MODULE,
484};
485
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800486static int __init ip6t_log_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
488 if (ip6t_register_target(&ip6t_log_reg))
489 return -EINVAL;
Harald Welte608c8e42005-08-09 19:58:27 -0700490 if (nf_log_register(PF_INET6, &ip6t_logger) < 0) {
491 printk(KERN_WARNING "ip6t_LOG: not logging via system console "
492 "since somebody else already registered for PF_INET6\n");
493 /* we cannot make module load fail here, since otherwise
494 * ip6tables userspace would abort */
495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497 return 0;
498}
499
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800500static void __exit ip6t_log_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
Harald Welte608c8e42005-08-09 19:58:27 -0700502 nf_log_unregister_logger(&ip6t_logger);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 ip6t_unregister_target(&ip6t_log_reg);
504}
505
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800506module_init(ip6t_log_init);
507module_exit(ip6t_log_fini);