2 * This is a module which is used for logging packets.
5 /* (C) 2001 Jan Rekorajski <baggins@pld.org.pl>
6 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
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.
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
15 #include <linux/skbuff.h>
17 #include <linux/spinlock.h>
18 #include <linux/icmpv6.h>
22 #include <linux/netfilter.h>
23 #include <linux/netfilter_ipv6/ip6_tables.h>
25 MODULE_AUTHOR("Jan Rekorajski <baggins@pld.org.pl>");
26 MODULE_DESCRIPTION("IP6 tables LOG target module");
27 MODULE_LICENSE("GPL");
29 static unsigned int nflog = 1;
30 module_param(nflog, int, 0400);
31 MODULE_PARM_DESC(nflog, "register as internal netfilter logging module");
34 #include <net/route.h>
35 #include <linux/netfilter_ipv6/ip6t_LOG.h>
40 #define DEBUGP(format, args...)
43 /* Use lock to serialize, so printks don't overlap */
44 static DEFINE_SPINLOCK(log_lock);
46 /* One level of recursion won't kill us */
47 static void dump_packet(const struct ip6t_log_info *info,
48 const struct sk_buff *skb, unsigned int ip6hoff,
53 struct ipv6hdr _ip6h, *ih;
55 unsigned int hdrlen = 0;
57 ih = skb_header_pointer(skb, ip6hoff, sizeof(_ip6h), &_ip6h);
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));
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,
72 (ntohl(*(u_int32_t *)ih) & 0x000fffff));
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;
80 hp = skb_header_pointer(skb, ptr, sizeof(_hdr), &_hdr);
86 /* Max length: 48 "OPT (...) " */
87 if (info->logflags & IP6T_LOG_IPOPT)
91 case IPPROTO_FRAGMENT: {
92 struct frag_hdr _fhdr, *fh;
95 fh = skb_header_pointer(skb, ptr, sizeof(_fhdr),
102 /* Max length: 6 "65535 " */
103 printk("%u ", ntohs(fh->frag_off) & 0xFFF8);
105 /* Max length: 11 "INCOMPLETE " */
106 if (fh->frag_off & htons(0x0001))
107 printk("INCOMPLETE ");
109 printk("ID:%08x ", ntohl(fh->identification));
111 if (ntohs(fh->frag_off) & 0xFFF8)
118 case IPPROTO_DSTOPTS:
119 case IPPROTO_ROUTING:
120 case IPPROTO_HOPOPTS:
122 if (info->logflags & IP6T_LOG_IPOPT)
126 hdrlen = ipv6_optlen(hp);
130 if (info->logflags & IP6T_LOG_IPOPT) {
131 struct ip_auth_hdr _ahdr, *ah;
133 /* Max length: 3 "AH " */
141 ah = skb_header_pointer(skb, ptr, sizeof(_ahdr),
145 * Max length: 26 "INCOMPLETE [65535
148 printk("INCOMPLETE [%u bytes] )",
153 /* Length: 15 "SPI=0xF1234567 */
154 printk("SPI=0x%x ", ntohl(ah->spi));
158 hdrlen = (hp->hdrlen+2)<<2;
161 if (info->logflags & IP6T_LOG_IPOPT) {
162 struct ip_esp_hdr _esph, *eh;
164 /* Max length: 4 "ESP " */
173 * Max length: 26 "INCOMPLETE [65535 bytes] )"
175 eh = skb_header_pointer(skb, ptr, sizeof(_esph),
178 printk("INCOMPLETE [%u bytes] )",
183 /* Length: 16 "SPI=0xF1234567 )" */
184 printk("SPI=0x%x )", ntohl(eh->spi) );
189 /* Max length: 20 "Unknown Ext Hdr 255" */
190 printk("Unknown Ext Hdr %u", currenthdr);
193 if (info->logflags & IP6T_LOG_IPOPT)
196 currenthdr = hp->nexthdr;
200 switch (currenthdr) {
202 struct tcphdr _tcph, *th;
204 /* Max length: 10 "PROTO=TCP " */
205 printk("PROTO=TCP ");
210 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
211 th = skb_header_pointer(skb, ptr, sizeof(_tcph), &_tcph);
213 printk("INCOMPLETE [%u bytes] ", skb->len - ptr);
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 " */
245 /* Max length: 11 "URGP=65535 " */
246 printk("URGP=%u ", ntohs(th->urg_ptr));
248 if ((info->logflags & IP6T_LOG_TCPOPT)
249 && th->doff * 4 > sizeof(struct tcphdr)) {
250 u_int8_t _opt[60 - sizeof(struct tcphdr)], *op;
252 unsigned int optsize = th->doff * 4
253 - sizeof(struct tcphdr);
255 op = skb_header_pointer(skb,
256 ptr + sizeof(struct tcphdr),
259 printk("OPT (TRUNCATED)");
263 /* Max length: 127 "OPT (" 15*4*2chars ") " */
265 for (i =0; i < optsize; i++)
266 printk("%02X", op[i]);
272 struct udphdr _udph, *uh;
274 /* Max length: 10 "PROTO=UDP " */
275 printk("PROTO=UDP ");
280 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
281 uh = skb_header_pointer(skb, ptr, sizeof(_udph), &_udph);
283 printk("INCOMPLETE [%u bytes] ", skb->len - ptr);
287 /* Max length: 20 "SPT=65535 DPT=65535 " */
288 printk("SPT=%u DPT=%u LEN=%u ",
289 ntohs(uh->source), ntohs(uh->dest),
293 case IPPROTO_ICMPV6: {
294 struct icmp6hdr _icmp6h, *ic;
296 /* Max length: 13 "PROTO=ICMPv6 " */
297 printk("PROTO=ICMPv6 ");
302 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
303 ic = skb_header_pointer(skb, ptr, sizeof(_icmp6h), &_icmp6h);
305 printk("INCOMPLETE [%u bytes] ", skb->len - ptr);
309 /* Max length: 18 "TYPE=255 CODE=255 " */
310 printk("TYPE=%u CODE=%u ", ic->icmp6_type, ic->icmp6_code);
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));
320 case ICMPV6_MGM_QUERY:
321 case ICMPV6_MGM_REPORT:
322 case ICMPV6_MGM_REDUCTION:
325 case ICMPV6_PARAMPROB:
326 /* Max length: 17 "POINTER=ffffffff " */
327 printk("POINTER=%08x ", ntohl(ic->icmp6_pointer));
329 case ICMPV6_DEST_UNREACH:
330 case ICMPV6_PKT_TOOBIG:
331 case ICMPV6_TIME_EXCEED:
332 /* Max length: 3+maxlen */
335 dump_packet(info, skb, ptr + sizeof(_icmp6h),
340 /* Max length: 10 "MTU=65535 " */
341 if (ic->icmp6_type == ICMPV6_PKT_TOOBIG)
342 printk("MTU=%u ", ntohl(ic->icmp6_mtu));
346 /* Max length: 10 "PROTO=255 " */
348 printk("PROTO=%u ", currenthdr);
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);
361 ip6t_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,
369 struct ipv6hdr *ipv6h = skb->nh.ipv6h;
371 spin_lock_bh(&log_lock);
372 printk(level_string);
373 printk("%sIN=%s OUT=%s ",
374 prefix == NULL ? loginfo->prefix : prefix,
376 out ? out->name : "");
378 /* MAC logging for input chain only. */
380 if (skb->dev && skb->dev->hard_header_len && skb->mac.raw != (void*)ipv6h) {
381 if (skb->dev->type != ARPHRD_SIT){
383 unsigned char *p = skb->mac.raw;
384 for (i = 0; i < skb->dev->hard_header_len; i++,p++)
386 i==skb->dev->hard_header_len - 1
390 unsigned char *p = skb->mac.raw;
391 if ( p - (ETH_ALEN*2+2) > skb->head ){
393 for (i = 0; i < (ETH_ALEN); i++,p++)
395 i == ETH_ALEN-1 ? "->" : ":");
397 for (i = 0; i < (ETH_ALEN); i++,p++)
399 i == ETH_ALEN-1 ? ' ' : ':');
402 if ((skb->dev->addr_len == 4) &&
403 skb->dev->hard_header_len > 20){
405 p = skb->mac.raw + 12;
406 for (i = 0; i < 4; i++,p++)
408 i == 3 ? "->" : ".");
409 for (i = 0; i < 4; i++,p++)
418 dump_packet(loginfo, skb, (u8*)skb->nh.ipv6h - skb->data, 1);
420 spin_unlock_bh(&log_lock);
424 ip6t_log_target(struct sk_buff **pskb,
425 const struct net_device *in,
426 const struct net_device *out,
427 unsigned int hooknum,
428 const void *targinfo,
431 const struct ip6t_log_info *loginfo = targinfo;
432 char level_string[4] = "< >";
434 level_string[1] = '0' + (loginfo->level % 8);
435 ip6t_log_packet(hooknum, *pskb, in, out, loginfo, level_string, NULL);
437 return IP6T_CONTINUE;
441 ip6t_logfn(unsigned int hooknum,
442 const struct sk_buff *skb,
443 const struct net_device *in,
444 const struct net_device *out,
447 struct ip6t_log_info loginfo = {
449 .logflags = IP6T_LOG_MASK,
453 ip6t_log_packet(hooknum, skb, in, out, &loginfo, KERN_WARNING, prefix);
456 static int ip6t_log_checkentry(const char *tablename,
457 const struct ip6t_entry *e,
459 unsigned int targinfosize,
460 unsigned int hook_mask)
462 const struct ip6t_log_info *loginfo = targinfo;
464 if (targinfosize != IP6T_ALIGN(sizeof(struct ip6t_log_info))) {
465 DEBUGP("LOG: targinfosize %u != %u\n",
466 targinfosize, IP6T_ALIGN(sizeof(struct ip6t_log_info)));
470 if (loginfo->level >= 8) {
471 DEBUGP("LOG: level %u >= 8\n", loginfo->level);
475 if (loginfo->prefix[sizeof(loginfo->prefix)-1] != '\0') {
476 DEBUGP("LOG: prefix term %i\n",
477 loginfo->prefix[sizeof(loginfo->prefix)-1]);
484 static struct ip6t_target ip6t_log_reg = {
486 .target = ip6t_log_target,
487 .checkentry = ip6t_log_checkentry,
491 static int __init init(void)
493 if (ip6t_register_target(&ip6t_log_reg))
496 nf_log_register(PF_INET6, &ip6t_logfn);
501 static void __exit fini(void)
504 nf_log_unregister(PF_INET6, &ip6t_logfn);
505 ip6t_unregister_target(&ip6t_log_reg);