m68knommu: remove local gettimeofday code
[wrt350n-kernel.git] / net / ipv6 / netfilter / ip6t_LOG.c
blob86a613810b6993a4699d8e53de1e03ba4e6ed3d5
1 /*
2 * This is a module which is used for logging packets.
3 */
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>
16 #include <linux/if_arp.h>
17 #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/x_tables.h>
25 #include <linux/netfilter_ipv6/ip6_tables.h>
26 #include <net/netfilter/nf_log.h>
28 MODULE_AUTHOR("Jan Rekorajski <baggins@pld.org.pl>");
29 MODULE_DESCRIPTION("Xtables: IPv6 packet logging to syslog");
30 MODULE_LICENSE("GPL");
32 struct in_device;
33 #include <net/route.h>
34 #include <linux/netfilter_ipv6/ip6t_LOG.h>
36 /* Use lock to serialize, so printks don't overlap */
37 static DEFINE_SPINLOCK(log_lock);
39 /* One level of recursion won't kill us */
40 static void dump_packet(const struct nf_loginfo *info,
41 const struct sk_buff *skb, unsigned int ip6hoff,
42 int recurse)
44 u_int8_t currenthdr;
45 int fragment;
46 struct ipv6hdr _ip6h;
47 const struct ipv6hdr *ih;
48 unsigned int ptr;
49 unsigned int hdrlen = 0;
50 unsigned int logflags;
52 if (info->type == NF_LOG_TYPE_LOG)
53 logflags = info->u.log.logflags;
54 else
55 logflags = NF_LOG_MASK;
57 ih = skb_header_pointer(skb, ip6hoff, sizeof(_ip6h), &_ip6h);
58 if (ih == NULL) {
59 printk("TRUNCATED");
60 return;
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=" NIP6_FMT " DST=" NIP6_FMT " ", NIP6(ih->saddr), NIP6(ih->daddr));
66 /* Max length: 44 "LEN=65535 TC=255 HOPLIMIT=255 FLOWLBL=FFFFF " */
67 printk("LEN=%Zu TC=%u HOPLIMIT=%u FLOWLBL=%u ",
68 ntohs(ih->payload_len) + sizeof(struct ipv6hdr),
69 (ntohl(*(__be32 *)ih) & 0x0ff00000) >> 20,
70 ih->hop_limit,
71 (ntohl(*(__be32 *)ih) & 0x000fffff));
73 fragment = 0;
74 ptr = ip6hoff + sizeof(struct ipv6hdr);
75 currenthdr = ih->nexthdr;
76 while (currenthdr != NEXTHDR_NONE && ip6t_ext_hdr(currenthdr)) {
77 struct ipv6_opt_hdr _hdr;
78 const struct ipv6_opt_hdr *hp;
80 hp = skb_header_pointer(skb, ptr, sizeof(_hdr), &_hdr);
81 if (hp == NULL) {
82 printk("TRUNCATED");
83 return;
86 /* Max length: 48 "OPT (...) " */
87 if (logflags & IP6T_LOG_IPOPT)
88 printk("OPT ( ");
90 switch (currenthdr) {
91 case IPPROTO_FRAGMENT: {
92 struct frag_hdr _fhdr;
93 const struct frag_hdr *fh;
95 printk("FRAG:");
96 fh = skb_header_pointer(skb, ptr, sizeof(_fhdr),
97 &_fhdr);
98 if (fh == NULL) {
99 printk("TRUNCATED ");
100 return;
103 /* Max length: 6 "65535 " */
104 printk("%u ", ntohs(fh->frag_off) & 0xFFF8);
106 /* Max length: 11 "INCOMPLETE " */
107 if (fh->frag_off & htons(0x0001))
108 printk("INCOMPLETE ");
110 printk("ID:%08x ", ntohl(fh->identification));
112 if (ntohs(fh->frag_off) & 0xFFF8)
113 fragment = 1;
115 hdrlen = 8;
117 break;
119 case IPPROTO_DSTOPTS:
120 case IPPROTO_ROUTING:
121 case IPPROTO_HOPOPTS:
122 if (fragment) {
123 if (logflags & IP6T_LOG_IPOPT)
124 printk(")");
125 return;
127 hdrlen = ipv6_optlen(hp);
128 break;
129 /* Max Length */
130 case IPPROTO_AH:
131 if (logflags & IP6T_LOG_IPOPT) {
132 struct ip_auth_hdr _ahdr;
133 const struct ip_auth_hdr *ah;
135 /* Max length: 3 "AH " */
136 printk("AH ");
138 if (fragment) {
139 printk(")");
140 return;
143 ah = skb_header_pointer(skb, ptr, sizeof(_ahdr),
144 &_ahdr);
145 if (ah == NULL) {
147 * Max length: 26 "INCOMPLETE [65535
148 * bytes] )"
150 printk("INCOMPLETE [%u bytes] )",
151 skb->len - ptr);
152 return;
155 /* Length: 15 "SPI=0xF1234567 */
156 printk("SPI=0x%x ", ntohl(ah->spi));
160 hdrlen = (hp->hdrlen+2)<<2;
161 break;
162 case IPPROTO_ESP:
163 if (logflags & IP6T_LOG_IPOPT) {
164 struct ip_esp_hdr _esph;
165 const struct ip_esp_hdr *eh;
167 /* Max length: 4 "ESP " */
168 printk("ESP ");
170 if (fragment) {
171 printk(")");
172 return;
176 * Max length: 26 "INCOMPLETE [65535 bytes] )"
178 eh = skb_header_pointer(skb, ptr, sizeof(_esph),
179 &_esph);
180 if (eh == NULL) {
181 printk("INCOMPLETE [%u bytes] )",
182 skb->len - ptr);
183 return;
186 /* Length: 16 "SPI=0xF1234567 )" */
187 printk("SPI=0x%x )", ntohl(eh->spi) );
190 return;
191 default:
192 /* Max length: 20 "Unknown Ext Hdr 255" */
193 printk("Unknown Ext Hdr %u", currenthdr);
194 return;
196 if (logflags & IP6T_LOG_IPOPT)
197 printk(") ");
199 currenthdr = hp->nexthdr;
200 ptr += hdrlen;
203 switch (currenthdr) {
204 case IPPROTO_TCP: {
205 struct tcphdr _tcph;
206 const struct tcphdr *th;
208 /* Max length: 10 "PROTO=TCP " */
209 printk("PROTO=TCP ");
211 if (fragment)
212 break;
214 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
215 th = skb_header_pointer(skb, ptr, sizeof(_tcph), &_tcph);
216 if (th == NULL) {
217 printk("INCOMPLETE [%u bytes] ", skb->len - ptr);
218 return;
221 /* Max length: 20 "SPT=65535 DPT=65535 " */
222 printk("SPT=%u DPT=%u ",
223 ntohs(th->source), ntohs(th->dest));
224 /* Max length: 30 "SEQ=4294967295 ACK=4294967295 " */
225 if (logflags & IP6T_LOG_TCPSEQ)
226 printk("SEQ=%u ACK=%u ",
227 ntohl(th->seq), ntohl(th->ack_seq));
228 /* Max length: 13 "WINDOW=65535 " */
229 printk("WINDOW=%u ", ntohs(th->window));
230 /* Max length: 9 "RES=0x3C " */
231 printk("RES=0x%02x ", (u_int8_t)(ntohl(tcp_flag_word(th) & TCP_RESERVED_BITS) >> 22));
232 /* Max length: 32 "CWR ECE URG ACK PSH RST SYN FIN " */
233 if (th->cwr)
234 printk("CWR ");
235 if (th->ece)
236 printk("ECE ");
237 if (th->urg)
238 printk("URG ");
239 if (th->ack)
240 printk("ACK ");
241 if (th->psh)
242 printk("PSH ");
243 if (th->rst)
244 printk("RST ");
245 if (th->syn)
246 printk("SYN ");
247 if (th->fin)
248 printk("FIN ");
249 /* Max length: 11 "URGP=65535 " */
250 printk("URGP=%u ", ntohs(th->urg_ptr));
252 if ((logflags & IP6T_LOG_TCPOPT)
253 && th->doff * 4 > sizeof(struct tcphdr)) {
254 u_int8_t _opt[60 - sizeof(struct tcphdr)];
255 const u_int8_t *op;
256 unsigned int i;
257 unsigned int optsize = th->doff * 4
258 - sizeof(struct tcphdr);
260 op = skb_header_pointer(skb,
261 ptr + sizeof(struct tcphdr),
262 optsize, _opt);
263 if (op == NULL) {
264 printk("OPT (TRUNCATED)");
265 return;
268 /* Max length: 127 "OPT (" 15*4*2chars ") " */
269 printk("OPT (");
270 for (i =0; i < optsize; i++)
271 printk("%02X", op[i]);
272 printk(") ");
274 break;
276 case IPPROTO_UDP:
277 case IPPROTO_UDPLITE: {
278 struct udphdr _udph;
279 const struct udphdr *uh;
281 if (currenthdr == IPPROTO_UDP)
282 /* Max length: 10 "PROTO=UDP " */
283 printk("PROTO=UDP " );
284 else /* Max length: 14 "PROTO=UDPLITE " */
285 printk("PROTO=UDPLITE ");
287 if (fragment)
288 break;
290 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
291 uh = skb_header_pointer(skb, ptr, sizeof(_udph), &_udph);
292 if (uh == NULL) {
293 printk("INCOMPLETE [%u bytes] ", skb->len - ptr);
294 return;
297 /* Max length: 20 "SPT=65535 DPT=65535 " */
298 printk("SPT=%u DPT=%u LEN=%u ",
299 ntohs(uh->source), ntohs(uh->dest),
300 ntohs(uh->len));
301 break;
303 case IPPROTO_ICMPV6: {
304 struct icmp6hdr _icmp6h;
305 const struct icmp6hdr *ic;
307 /* Max length: 13 "PROTO=ICMPv6 " */
308 printk("PROTO=ICMPv6 ");
310 if (fragment)
311 break;
313 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
314 ic = skb_header_pointer(skb, ptr, sizeof(_icmp6h), &_icmp6h);
315 if (ic == NULL) {
316 printk("INCOMPLETE [%u bytes] ", skb->len - ptr);
317 return;
320 /* Max length: 18 "TYPE=255 CODE=255 " */
321 printk("TYPE=%u CODE=%u ", ic->icmp6_type, ic->icmp6_code);
323 switch (ic->icmp6_type) {
324 case ICMPV6_ECHO_REQUEST:
325 case ICMPV6_ECHO_REPLY:
326 /* Max length: 19 "ID=65535 SEQ=65535 " */
327 printk("ID=%u SEQ=%u ",
328 ntohs(ic->icmp6_identifier),
329 ntohs(ic->icmp6_sequence));
330 break;
331 case ICMPV6_MGM_QUERY:
332 case ICMPV6_MGM_REPORT:
333 case ICMPV6_MGM_REDUCTION:
334 break;
336 case ICMPV6_PARAMPROB:
337 /* Max length: 17 "POINTER=ffffffff " */
338 printk("POINTER=%08x ", ntohl(ic->icmp6_pointer));
339 /* Fall through */
340 case ICMPV6_DEST_UNREACH:
341 case ICMPV6_PKT_TOOBIG:
342 case ICMPV6_TIME_EXCEED:
343 /* Max length: 3+maxlen */
344 if (recurse) {
345 printk("[");
346 dump_packet(info, skb, ptr + sizeof(_icmp6h),
348 printk("] ");
351 /* Max length: 10 "MTU=65535 " */
352 if (ic->icmp6_type == ICMPV6_PKT_TOOBIG)
353 printk("MTU=%u ", ntohl(ic->icmp6_mtu));
355 break;
357 /* Max length: 10 "PROTO=255 " */
358 default:
359 printk("PROTO=%u ", currenthdr);
362 /* Max length: 15 "UID=4294967295 " */
363 if ((logflags & IP6T_LOG_UID) && recurse && skb->sk) {
364 read_lock_bh(&skb->sk->sk_callback_lock);
365 if (skb->sk->sk_socket && skb->sk->sk_socket->file)
366 printk("UID=%u GID=%u",
367 skb->sk->sk_socket->file->f_uid,
368 skb->sk->sk_socket->file->f_gid);
369 read_unlock_bh(&skb->sk->sk_callback_lock);
373 static struct nf_loginfo default_loginfo = {
374 .type = NF_LOG_TYPE_LOG,
375 .u = {
376 .log = {
377 .level = 0,
378 .logflags = NF_LOG_MASK,
383 static void
384 ip6t_log_packet(unsigned int pf,
385 unsigned int hooknum,
386 const struct sk_buff *skb,
387 const struct net_device *in,
388 const struct net_device *out,
389 const struct nf_loginfo *loginfo,
390 const char *prefix)
392 if (!loginfo)
393 loginfo = &default_loginfo;
395 spin_lock_bh(&log_lock);
396 printk("<%d>%sIN=%s OUT=%s ", loginfo->u.log.level,
397 prefix,
398 in ? in->name : "",
399 out ? out->name : "");
400 if (in && !out) {
401 unsigned int len;
402 /* MAC logging for input chain only. */
403 printk("MAC=");
404 if (skb->dev && (len = skb->dev->hard_header_len) &&
405 skb->mac_header != skb->network_header) {
406 const unsigned char *p = skb_mac_header(skb);
407 int i;
409 if (skb->dev->type == ARPHRD_SIT &&
410 (p -= ETH_HLEN) < skb->head)
411 p = NULL;
413 if (p != NULL) {
414 for (i = 0; i < len; i++)
415 printk("%02x%s", p[i],
416 i == len - 1 ? "" : ":");
418 printk(" ");
420 if (skb->dev->type == ARPHRD_SIT) {
421 const struct iphdr *iph =
422 (struct iphdr *)skb_mac_header(skb);
423 printk("TUNNEL=%u.%u.%u.%u->%u.%u.%u.%u ",
424 NIPQUAD(iph->saddr),
425 NIPQUAD(iph->daddr));
427 } else
428 printk(" ");
431 dump_packet(loginfo, skb, skb_network_offset(skb), 1);
432 printk("\n");
433 spin_unlock_bh(&log_lock);
436 static unsigned int
437 log_tg6(struct sk_buff *skb, const struct net_device *in,
438 const struct net_device *out, unsigned int hooknum,
439 const struct xt_target *target, const void *targinfo)
441 const struct ip6t_log_info *loginfo = targinfo;
442 struct nf_loginfo li;
444 li.type = NF_LOG_TYPE_LOG;
445 li.u.log.level = loginfo->level;
446 li.u.log.logflags = loginfo->logflags;
448 ip6t_log_packet(PF_INET6, hooknum, skb, in, out, &li, loginfo->prefix);
449 return XT_CONTINUE;
453 static bool
454 log_tg6_check(const char *tablename, const void *entry,
455 const struct xt_target *target, void *targinfo,
456 unsigned int hook_mask)
458 const struct ip6t_log_info *loginfo = targinfo;
460 if (loginfo->level >= 8) {
461 pr_debug("LOG: level %u >= 8\n", loginfo->level);
462 return false;
464 if (loginfo->prefix[sizeof(loginfo->prefix)-1] != '\0') {
465 pr_debug("LOG: prefix term %i\n",
466 loginfo->prefix[sizeof(loginfo->prefix)-1]);
467 return false;
469 return true;
472 static struct xt_target log_tg6_reg __read_mostly = {
473 .name = "LOG",
474 .family = AF_INET6,
475 .target = log_tg6,
476 .targetsize = sizeof(struct ip6t_log_info),
477 .checkentry = log_tg6_check,
478 .me = THIS_MODULE,
481 static const struct nf_logger ip6t_logger = {
482 .name = "ip6t_LOG",
483 .logfn = &ip6t_log_packet,
484 .me = THIS_MODULE,
487 static int __init log_tg6_init(void)
489 int ret;
491 ret = xt_register_target(&log_tg6_reg);
492 if (ret < 0)
493 return ret;
494 nf_log_register(PF_INET6, &ip6t_logger);
495 return 0;
498 static void __exit log_tg6_exit(void)
500 nf_log_unregister(&ip6t_logger);
501 xt_unregister_target(&log_tg6_reg);
504 module_init(log_tg6_init);
505 module_exit(log_tg6_exit);