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/skbuff.h>
16 #include <linux/spinlock.h>
17 #include <linux/icmpv6.h>
21 #include <linux/netfilter.h>
22 #include <linux/netfilter_ipv6/ip6_tables.h>
24 MODULE_AUTHOR("Jan Rekorajski <baggins@pld.org.pl>");
25 MODULE_DESCRIPTION("IP6 tables LOG target module");
26 MODULE_LICENSE("GPL");
28 static unsigned int nflog = 1;
29 MODULE_PARM(nflog, "i");
30 MODULE_PARM_DESC(nflog, "register as internal netfilter logging module");
33 #include <net/route.h>
34 #include <linux/netfilter_ipv6/ip6t_LOG.h>
39 #define DEBUGP(format, args...)
44 }; /* FIXME evil kludge */
46 /* Use lock to serialize, so printks don't overlap */
47 static spinlock_t log_lock = SPIN_LOCK_UNLOCKED;
49 /* takes in current header and pointer to the header */
50 /* if another header exists, sets hdrptr to the next header
51 and returns the new header value, else returns IPPROTO_NONE */
52 static u_int8_t ip6_nexthdr(u_int8_t currenthdr, u_int8_t **hdrptr)
54 u_int8_t hdrlen, nexthdr = IPPROTO_NONE;
58 /* whoever decided to do the length of AUTH for ipv6
59 in 32bit units unlike other headers should be beaten...
60 repeatedly...with a large stick...no, an even LARGER
61 stick...no, you're still not thinking big enough */
63 hdrlen = *hdrptr[1] * 4 + 8;
64 *hdrptr = *hdrptr + hdrlen;
71 hdrlen = *hdrptr[1] * 8 + 8;
72 *hdrptr = *hdrptr + hdrlen;
74 case IPPROTO_FRAGMENT:
76 *hdrptr = *hdrptr + 8;
82 /* One level of recursion won't kill us */
83 static void dump_packet(const struct ip6t_log_info *info,
84 struct ipv6hdr *ipv6h, int recurse)
86 u_int8_t currenthdr = ipv6h->nexthdr;
90 /* Max length: 88 "SRC=0000.0000.0000.0000.0000.0000.0000.0000 DST=0000.0000.0000.0000.0000.0000.0000.0000" */
91 printk("SRC=%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x ", NIP6(ipv6h->saddr));
92 printk("DST=%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x ", NIP6(ipv6h->daddr));
94 /* Max length: 44 "LEN=65535 TC=255 HOPLIMIT=255 FLOWLBL=FFFFF " */
95 printk("LEN=%Zu TC=%u HOPLIMIT=%u FLOWLBL=%u ",
96 ntohs(ipv6h->payload_len) + sizeof(struct ipv6hdr),
97 (ntohl(*(u_int32_t *)ipv6h) & 0x0ff00000) >> 20,
99 (ntohl(*(u_int32_t *)ipv6h) & 0x000fffff));
102 hdrptr = (u_int8_t *)(ipv6h + 1);
103 while (currenthdr != IPPROTO_NONE) {
104 if ((currenthdr == IPPROTO_TCP) ||
105 (currenthdr == IPPROTO_UDP) ||
106 (currenthdr == IPPROTO_ICMPV6))
108 /* Max length: 48 "OPT (...) " */
110 switch (currenthdr) {
111 case IPPROTO_FRAGMENT: {
112 struct frag_hdr *fhdr = (struct frag_hdr *)hdrptr;
114 /* Max length: 11 "FRAG:65535 " */
115 printk("FRAG:%u ", ntohs(fhdr->frag_off) & 0xFFF8);
117 /* Max length: 11 "INCOMPLETE " */
118 if (fhdr->frag_off & htons(0x0001))
119 printk("INCOMPLETE ");
121 printk("ID:%08x ", fhdr->identification);
123 if (ntohs(fhdr->frag_off) & 0xFFF8)
128 case IPPROTO_DSTOPTS:
129 case IPPROTO_ROUTING:
130 case IPPROTO_HOPOPTS:
135 if (info->logflags & IP6T_LOG_IPOPT) {
136 struct esphdr *esph = (struct esphdr *)hdrptr;
137 int esp = (currenthdr == IPPROTO_ESP);
139 /* Max length: 4 "ESP " */
140 printk("%s ",esp ? "ESP" : "AH");
142 /* Length: 15 "SPI=0xF1234567 " */
143 printk("SPI=0x%x ", ntohl(esph->spi) );
150 currenthdr = ip6_nexthdr(currenthdr, &hdrptr);
153 switch (currenthdr) {
155 struct tcphdr *tcph = (struct tcphdr *)hdrptr;
157 /* Max length: 10 "PROTO=TCP " */
158 printk("PROTO=TCP ");
163 /* Max length: 20 "SPT=65535 DPT=65535 " */
164 printk("SPT=%u DPT=%u ",
165 ntohs(tcph->source), ntohs(tcph->dest));
166 /* Max length: 30 "SEQ=4294967295 ACK=4294967295 " */
167 if (info->logflags & IP6T_LOG_TCPSEQ)
168 printk("SEQ=%u ACK=%u ",
169 ntohl(tcph->seq), ntohl(tcph->ack_seq));
170 /* Max length: 13 "WINDOW=65535 " */
171 printk("WINDOW=%u ", ntohs(tcph->window));
172 /* Max length: 9 "RES=0x3F " */
173 printk("RES=0x%02x ", (u_int8_t)(ntohl(tcp_flag_word(tcph) & TCP_RESERVED_BITS) >> 22));
174 /* Max length: 32 "CWR ECE URG ACK PSH RST SYN FIN " */
191 /* Max length: 11 "URGP=65535 " */
192 printk("URGP=%u ", ntohs(tcph->urg_ptr));
194 if ((info->logflags & IP6T_LOG_TCPOPT)
195 && tcph->doff * 4 != sizeof(struct tcphdr)) {
198 /* Max length: 127 "OPT (" 15*4*2chars ") " */
200 for (i =sizeof(struct tcphdr); i < tcph->doff * 4; i++)
201 printk("%02X", ((u_int8_t *)tcph)[i]);
207 struct udphdr *udph = (struct udphdr *)hdrptr;
209 /* Max length: 10 "PROTO=UDP " */
210 printk("PROTO=UDP ");
215 /* Max length: 20 "SPT=65535 DPT=65535 " */
216 printk("SPT=%u DPT=%u LEN=%u ",
217 ntohs(udph->source), ntohs(udph->dest),
221 case IPPROTO_ICMPV6: {
222 struct icmp6hdr *icmp6h = (struct icmp6hdr *)hdrptr;
224 /* Max length: 13 "PROTO=ICMPv6 " */
225 printk("PROTO=ICMPv6 ");
230 /* Max length: 18 "TYPE=255 CODE=255 " */
231 printk("TYPE=%u CODE=%u ", icmp6h->icmp6_type, icmp6h->icmp6_code);
233 switch (icmp6h->icmp6_type) {
234 case ICMPV6_ECHO_REQUEST:
235 case ICMPV6_ECHO_REPLY:
236 /* Max length: 19 "ID=65535 SEQ=65535 " */
237 printk("ID=%u SEQ=%u ",
238 ntohs(icmp6h->icmp6_identifier),
239 ntohs(icmp6h->icmp6_sequence));
241 case ICMPV6_MGM_QUERY:
242 case ICMPV6_MGM_REPORT:
243 case ICMPV6_MGM_REDUCTION:
246 case ICMPV6_PARAMPROB:
247 /* Max length: 17 "POINTER=ffffffff " */
248 printk("POINTER=%08x ", ntohl(icmp6h->icmp6_pointer));
250 case ICMPV6_DEST_UNREACH:
251 case ICMPV6_PKT_TOOBIG:
252 case ICMPV6_TIME_EXCEED:
253 /* Max length: 3+maxlen */
256 dump_packet(info, (struct ipv6hdr *)(icmp6h + 1), 0);
260 /* Max length: 10 "MTU=65535 " */
261 if (icmp6h->icmp6_type == ICMPV6_PKT_TOOBIG)
262 printk("MTU=%u ", ntohl(icmp6h->icmp6_mtu));
266 /* Max length: 10 "PROTO=255 " */
268 printk("PROTO=%u ", currenthdr);
273 ip6t_log_packet(unsigned int hooknum,
274 const struct sk_buff *skb,
275 const struct net_device *in,
276 const struct net_device *out,
277 const struct ip6t_log_info *loginfo,
278 const char *level_string,
281 struct ipv6hdr *ipv6h = skb->nh.ipv6h;
283 spin_lock_bh(&log_lock);
284 printk(level_string);
285 printk("%sIN=%s OUT=%s ",
286 prefix == NULL ? loginfo->prefix : prefix,
288 out ? out->name : "");
290 /* MAC logging for input chain only. */
292 if (skb->dev && skb->dev->hard_header_len && skb->mac.raw != (void*)ipv6h) {
293 if (skb->dev->type != ARPHRD_SIT){
295 unsigned char *p = skb->mac.raw;
296 for (i = 0; i < skb->dev->hard_header_len; i++,p++)
298 i==skb->dev->hard_header_len - 1
302 unsigned char *p = skb->mac.raw;
303 if ( p - (ETH_ALEN*2+2) > skb->head ){
305 for (i = 0; i < (ETH_ALEN); i++,p++)
307 i == ETH_ALEN-1 ? "->" : ":");
309 for (i = 0; i < (ETH_ALEN); i++,p++)
311 i == ETH_ALEN-1 ? ' ' : ':');
314 if ((skb->dev->addr_len == 4) &&
315 skb->dev->hard_header_len > 20){
317 p = skb->mac.raw + 12;
318 for (i = 0; i < 4; i++,p++)
320 i == 3 ? "->" : ".");
321 for (i = 0; i < 4; i++,p++)
330 dump_packet(loginfo, ipv6h, 1);
332 spin_unlock_bh(&log_lock);
336 ip6t_log_target(struct sk_buff **pskb,
337 unsigned int hooknum,
338 const struct net_device *in,
339 const struct net_device *out,
340 const void *targinfo,
343 const struct ip6t_log_info *loginfo = targinfo;
344 char level_string[4] = "< >";
346 level_string[1] = '0' + (loginfo->level % 8);
347 ip6t_log_packet(hooknum, *pskb, in, out, loginfo, level_string, NULL);
349 return IP6T_CONTINUE;
353 ip6t_logfn(unsigned int hooknum,
354 const struct sk_buff *skb,
355 const struct net_device *in,
356 const struct net_device *out,
359 struct ip6t_log_info loginfo = {
361 .logflags = IP6T_LOG_MASK,
365 ip6t_log_packet(hooknum, skb, in, out, &loginfo, KERN_WARNING, prefix);
368 static int ip6t_log_checkentry(const char *tablename,
369 const struct ip6t_entry *e,
371 unsigned int targinfosize,
372 unsigned int hook_mask)
374 const struct ip6t_log_info *loginfo = targinfo;
376 if (targinfosize != IP6T_ALIGN(sizeof(struct ip6t_log_info))) {
377 DEBUGP("LOG: targinfosize %u != %u\n",
378 targinfosize, IP6T_ALIGN(sizeof(struct ip6t_log_info)));
382 if (loginfo->level >= 8) {
383 DEBUGP("LOG: level %u >= 8\n", loginfo->level);
387 if (loginfo->prefix[sizeof(loginfo->prefix)-1] != '\0') {
388 DEBUGP("LOG: prefix term %i\n",
389 loginfo->prefix[sizeof(loginfo->prefix)-1]);
396 static struct ip6t_target ip6t_log_reg = {
398 .target = ip6t_log_target,
399 .checkentry = ip6t_log_checkentry,
403 static int __init init(void)
405 if (ip6t_register_target(&ip6t_log_reg))
408 nf_log_register(PF_INET6, &ip6t_logfn);
413 static void __exit fini(void)
416 nf_log_unregister(PF_INET6, &ip6t_logfn);
417 ip6t_unregister_target(&ip6t_log_reg);