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 0 */
52 static u_int8_t ip6_nexthdr(u_int8_t currenthdr, u_int8_t **hdrptr)
54 u_int8_t hdrlen, nexthdr = 0;
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;
83 /* One level of recursion won't kill us */
84 static void dump_packet(const struct ip6t_log_info *info,
85 struct ipv6hdr *ipv6h, int recurse)
87 u_int8_t currenthdr = ipv6h->nexthdr;
91 /* Max length: 88 "SRC=0000.0000.0000.0000.0000.0000.0000.0000 DST=0000.0000.0000.0000.0000.0000.0000.0000" */
92 printk("SRC=%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x ", NIP6(ipv6h->saddr));
93 printk("DST=%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x ", NIP6(ipv6h->daddr));
95 /* Max length: 44 "LEN=65535 TC=255 HOPLIMIT=255 FLOWLBL=FFFFF " */
96 printk("LEN=%Zu TC=%u HOPLIMIT=%u FLOWLBL=%u ",
97 ntohs(ipv6h->payload_len) + sizeof(struct ipv6hdr),
98 (ntohl(*(u_int32_t *)ipv6h) & 0x0ff00000) >> 20,
100 (ntohl(*(u_int32_t *)ipv6h) & 0x000fffff));
103 hdrptr = (u_int8_t *)(ipv6h + 1);
105 if ((currenthdr == IPPROTO_TCP) ||
106 (currenthdr == IPPROTO_UDP) ||
107 (currenthdr == IPPROTO_ICMPV6))
109 /* Max length: 48 "OPT (...) " */
111 switch (currenthdr) {
112 case IPPROTO_FRAGMENT: {
113 struct frag_hdr *fhdr = (struct frag_hdr *)hdrptr;
115 /* Max length: 11 "FRAG:65535 " */
116 printk("FRAG:%u ", ntohs(fhdr->frag_off) & 0xFFF8);
118 /* Max length: 11 "INCOMPLETE " */
119 if (fhdr->frag_off & htons(0x0001))
120 printk("INCOMPLETE ");
122 printk("ID:%08x ", fhdr->identification);
124 if (ntohs(fhdr->frag_off) & 0xFFF8)
129 case IPPROTO_DSTOPTS:
130 case IPPROTO_ROUTING:
131 case IPPROTO_HOPOPTS:
136 if (info->logflags & IP6T_LOG_IPOPT) {
137 struct esphdr *esph = (struct esphdr *)hdrptr;
138 int esp = (currenthdr == IPPROTO_ESP);
140 /* Max length: 4 "ESP " */
141 printk("%s ",esp ? "ESP" : "AH");
143 /* Length: 15 "SPI=0xF1234567 " */
144 printk("SPI=0x%x ", ntohl(esph->spi) );
151 currenthdr = ip6_nexthdr(currenthdr, &hdrptr);
154 switch (currenthdr) {
156 struct tcphdr *tcph = (struct tcphdr *)hdrptr;
158 /* Max length: 10 "PROTO=TCP " */
159 printk("PROTO=TCP ");
164 /* Max length: 20 "SPT=65535 DPT=65535 " */
165 printk("SPT=%u DPT=%u ",
166 ntohs(tcph->source), ntohs(tcph->dest));
167 /* Max length: 30 "SEQ=4294967295 ACK=4294967295 " */
168 if (info->logflags & IP6T_LOG_TCPSEQ)
169 printk("SEQ=%u ACK=%u ",
170 ntohl(tcph->seq), ntohl(tcph->ack_seq));
171 /* Max length: 13 "WINDOW=65535 " */
172 printk("WINDOW=%u ", ntohs(tcph->window));
173 /* Max length: 9 "RES=0x3F " */
174 printk("RES=0x%02x ", (u_int8_t)(ntohl(tcp_flag_word(tcph) & TCP_RESERVED_BITS) >> 22));
175 /* Max length: 32 "CWR ECE URG ACK PSH RST SYN FIN " */
192 /* Max length: 11 "URGP=65535 " */
193 printk("URGP=%u ", ntohs(tcph->urg_ptr));
195 if ((info->logflags & IP6T_LOG_TCPOPT)
196 && tcph->doff * 4 != sizeof(struct tcphdr)) {
199 /* Max length: 127 "OPT (" 15*4*2chars ") " */
201 for (i =sizeof(struct tcphdr); i < tcph->doff * 4; i++)
202 printk("%02X", ((u_int8_t *)tcph)[i]);
208 struct udphdr *udph = (struct udphdr *)hdrptr;
210 /* Max length: 10 "PROTO=UDP " */
211 printk("PROTO=UDP ");
216 /* Max length: 20 "SPT=65535 DPT=65535 " */
217 printk("SPT=%u DPT=%u LEN=%u ",
218 ntohs(udph->source), ntohs(udph->dest),
222 case IPPROTO_ICMPV6: {
223 struct icmp6hdr *icmp6h = (struct icmp6hdr *)hdrptr;
225 /* Max length: 13 "PROTO=ICMPv6 " */
226 printk("PROTO=ICMPv6 ");
231 /* Max length: 18 "TYPE=255 CODE=255 " */
232 printk("TYPE=%u CODE=%u ", icmp6h->icmp6_type, icmp6h->icmp6_code);
234 switch (icmp6h->icmp6_type) {
235 case ICMPV6_ECHO_REQUEST:
236 case ICMPV6_ECHO_REPLY:
237 /* Max length: 19 "ID=65535 SEQ=65535 " */
238 printk("ID=%u SEQ=%u ",
239 ntohs(icmp6h->icmp6_identifier),
240 ntohs(icmp6h->icmp6_sequence));
242 case ICMPV6_MGM_QUERY:
243 case ICMPV6_MGM_REPORT:
244 case ICMPV6_MGM_REDUCTION:
247 case ICMPV6_PARAMPROB:
248 /* Max length: 17 "POINTER=ffffffff " */
249 printk("POINTER=%08x ", ntohl(icmp6h->icmp6_pointer));
251 case ICMPV6_DEST_UNREACH:
252 case ICMPV6_PKT_TOOBIG:
253 case ICMPV6_TIME_EXCEED:
254 /* Max length: 3+maxlen */
257 dump_packet(info, (struct ipv6hdr *)(icmp6h + 1), 0);
261 /* Max length: 10 "MTU=65535 " */
262 if (icmp6h->icmp6_type == ICMPV6_PKT_TOOBIG)
263 printk("MTU=%u ", ntohl(icmp6h->icmp6_mtu));
267 /* Max length: 10 "PROTO 255 " */
269 printk("PROTO=%u ", currenthdr);
274 ip6t_log_packet(unsigned int hooknum,
275 const struct sk_buff *skb,
276 const struct net_device *in,
277 const struct net_device *out,
278 const struct ip6t_log_info *loginfo,
279 const char *level_string,
282 struct ipv6hdr *ipv6h = skb->nh.ipv6h;
284 spin_lock_bh(&log_lock);
285 printk(level_string);
286 printk("%sIN=%s OUT=%s ",
287 prefix == NULL ? loginfo->prefix : prefix,
289 out ? out->name : "");
291 /* MAC logging for input chain only. */
293 if (skb->dev && skb->dev->hard_header_len && skb->mac.raw != (void*)ipv6h) {
294 if (skb->dev->type != ARPHRD_SIT){
296 unsigned char *p = skb->mac.raw;
297 for (i = 0; i < skb->dev->hard_header_len; i++,p++)
299 i==skb->dev->hard_header_len - 1
303 unsigned char *p = skb->mac.raw;
304 if ( p - (ETH_ALEN*2+2) > skb->head ){
306 for (i = 0; i < (ETH_ALEN); i++,p++)
308 i == ETH_ALEN-1 ? "->" : ":");
310 for (i = 0; i < (ETH_ALEN); i++,p++)
312 i == ETH_ALEN-1 ? ' ' : ':');
315 if ((skb->dev->addr_len == 4) &&
316 skb->dev->hard_header_len > 20){
318 p = skb->mac.raw + 12;
319 for (i = 0; i < 4; i++,p++)
321 i == 3 ? "->" : ".");
322 for (i = 0; i < 4; i++,p++)
331 dump_packet(loginfo, ipv6h, 1);
333 spin_unlock_bh(&log_lock);
337 ip6t_log_target(struct sk_buff **pskb,
338 unsigned int hooknum,
339 const struct net_device *in,
340 const struct net_device *out,
341 const void *targinfo,
344 const struct ip6t_log_info *loginfo = targinfo;
345 char level_string[4] = "< >";
347 level_string[1] = '0' + (loginfo->level % 8);
348 ip6t_log_packet(hooknum, *pskb, in, out, loginfo, level_string, NULL);
350 return IP6T_CONTINUE;
354 ip6t_logfn(unsigned int hooknum,
355 const struct sk_buff *skb,
356 const struct net_device *in,
357 const struct net_device *out,
360 struct ip6t_log_info loginfo = {
362 .logflags = IP6T_LOG_MASK,
366 ip6t_log_packet(hooknum, skb, in, out, &loginfo, KERN_WARNING, prefix);
369 static int ip6t_log_checkentry(const char *tablename,
370 const struct ip6t_entry *e,
372 unsigned int targinfosize,
373 unsigned int hook_mask)
375 const struct ip6t_log_info *loginfo = targinfo;
377 if (targinfosize != IP6T_ALIGN(sizeof(struct ip6t_log_info))) {
378 DEBUGP("LOG: targinfosize %u != %u\n",
379 targinfosize, IP6T_ALIGN(sizeof(struct ip6t_log_info)));
383 if (loginfo->level >= 8) {
384 DEBUGP("LOG: level %u >= 8\n", loginfo->level);
388 if (loginfo->prefix[sizeof(loginfo->prefix)-1] != '\0') {
389 DEBUGP("LOG: prefix term %i\n",
390 loginfo->prefix[sizeof(loginfo->prefix)-1]);
397 static struct ip6t_target ip6t_log_reg = {
399 .target = ip6t_log_target,
400 .checkentry = ip6t_log_checkentry,
404 static int __init init(void)
406 if (ip6t_register_target(&ip6t_log_reg))
409 nf_log_register(PF_INET6, &ip6t_logfn);
414 static void __exit fini(void)
417 nf_log_unregister(PF_INET6, &ip6t_logfn);
418 ip6t_unregister_target(&ip6t_log_reg);