[NETFILTER]: x_tables: mark matches and targets __read_mostly
[deliverable/linux.git] / net / ipv6 / netfilter / ip6t_LOG.c
CommitLineData
1da177e4
LT
1/*
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>
14c85021 16#include <linux/if_arp.h>
1da177e4
LT
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>
6709dbbb 24#include <linux/netfilter/x_tables.h>
1da177e4
LT
25#include <linux/netfilter_ipv6/ip6_tables.h>
26
27MODULE_AUTHOR("Jan Rekorajski <baggins@pld.org.pl>");
28MODULE_DESCRIPTION("IP6 tables LOG target module");
29MODULE_LICENSE("GPL");
30
1da177e4
LT
31struct in_device;
32#include <net/route.h>
33#include <linux/netfilter_ipv6/ip6t_LOG.h>
34
35#if 0
36#define DEBUGP printk
37#else
38#define DEBUGP(format, args...)
39#endif
40
41/* Use lock to serialize, so printks don't overlap */
42static DEFINE_SPINLOCK(log_lock);
43
44/* One level of recursion won't kill us */
608c8e4f 45static void dump_packet(const struct nf_loginfo *info,
1da177e4
LT
46 const struct sk_buff *skb, unsigned int ip6hoff,
47 int recurse)
48{
49 u_int8_t currenthdr;
50 int fragment;
a47362a2
JE
51 struct ipv6hdr _ip6h;
52 const struct ipv6hdr *ih;
1da177e4
LT
53 unsigned int ptr;
54 unsigned int hdrlen = 0;
608c8e4f
HW
55 unsigned int logflags;
56
57 if (info->type == NF_LOG_TYPE_LOG)
58 logflags = info->u.log.logflags;
59 else
60 logflags = NF_LOG_MASK;
1da177e4
LT
61
62 ih = skb_header_pointer(skb, ip6hoff, sizeof(_ip6h), &_ip6h);
63 if (ih == NULL) {
64 printk("TRUNCATED");
65 return;
66 }
67
46b86a2d
JP
68 /* Max length: 88 "SRC=0000.0000.0000.0000.0000.0000.0000.0000 DST=0000.0000.0000.0000.0000.0000.0000.0000 " */
69 printk("SRC=" NIP6_FMT " DST=" NIP6_FMT " ", NIP6(ih->saddr), NIP6(ih->daddr));
1da177e4
LT
70
71 /* Max length: 44 "LEN=65535 TC=255 HOPLIMIT=255 FLOWLBL=FFFFF " */
72 printk("LEN=%Zu TC=%u HOPLIMIT=%u FLOWLBL=%u ",
73 ntohs(ih->payload_len) + sizeof(struct ipv6hdr),
e69a4adc 74 (ntohl(*(__be32 *)ih) & 0x0ff00000) >> 20,
1da177e4 75 ih->hop_limit,
e69a4adc 76 (ntohl(*(__be32 *)ih) & 0x000fffff));
1da177e4
LT
77
78 fragment = 0;
79 ptr = ip6hoff + sizeof(struct ipv6hdr);
80 currenthdr = ih->nexthdr;
81 while (currenthdr != NEXTHDR_NONE && ip6t_ext_hdr(currenthdr)) {
a47362a2
JE
82 struct ipv6_opt_hdr _hdr;
83 const struct ipv6_opt_hdr *hp;
1da177e4
LT
84
85 hp = skb_header_pointer(skb, ptr, sizeof(_hdr), &_hdr);
86 if (hp == NULL) {
87 printk("TRUNCATED");
88 return;
89 }
90
91 /* Max length: 48 "OPT (...) " */
608c8e4f 92 if (logflags & IP6T_LOG_IPOPT)
1da177e4
LT
93 printk("OPT ( ");
94
95 switch (currenthdr) {
96 case IPPROTO_FRAGMENT: {
a47362a2
JE
97 struct frag_hdr _fhdr;
98 const struct frag_hdr *fh;
1da177e4
LT
99
100 printk("FRAG:");
101 fh = skb_header_pointer(skb, ptr, sizeof(_fhdr),
102 &_fhdr);
103 if (fh == NULL) {
104 printk("TRUNCATED ");
105 return;
106 }
107
108 /* Max length: 6 "65535 " */
109 printk("%u ", ntohs(fh->frag_off) & 0xFFF8);
110
111 /* Max length: 11 "INCOMPLETE " */
112 if (fh->frag_off & htons(0x0001))
113 printk("INCOMPLETE ");
114
115 printk("ID:%08x ", ntohl(fh->identification));
116
117 if (ntohs(fh->frag_off) & 0xFFF8)
118 fragment = 1;
119
120 hdrlen = 8;
121
122 break;
123 }
124 case IPPROTO_DSTOPTS:
125 case IPPROTO_ROUTING:
126 case IPPROTO_HOPOPTS:
127 if (fragment) {
608c8e4f 128 if (logflags & IP6T_LOG_IPOPT)
1da177e4
LT
129 printk(")");
130 return;
131 }
132 hdrlen = ipv6_optlen(hp);
133 break;
134 /* Max Length */
135 case IPPROTO_AH:
608c8e4f 136 if (logflags & IP6T_LOG_IPOPT) {
a47362a2
JE
137 struct ip_auth_hdr _ahdr;
138 const struct ip_auth_hdr *ah;
1da177e4
LT
139
140 /* Max length: 3 "AH " */
141 printk("AH ");
142
143 if (fragment) {
144 printk(")");
145 return;
146 }
147
148 ah = skb_header_pointer(skb, ptr, sizeof(_ahdr),
149 &_ahdr);
150 if (ah == NULL) {
151 /*
1ab1457c 152 * Max length: 26 "INCOMPLETE [65535
1da177e4
LT
153 * bytes] )"
154 */
155 printk("INCOMPLETE [%u bytes] )",
156 skb->len - ptr);
157 return;
158 }
159
160 /* Length: 15 "SPI=0xF1234567 */
161 printk("SPI=0x%x ", ntohl(ah->spi));
162
163 }
164
165 hdrlen = (hp->hdrlen+2)<<2;
166 break;
167 case IPPROTO_ESP:
608c8e4f 168 if (logflags & IP6T_LOG_IPOPT) {
a47362a2
JE
169 struct ip_esp_hdr _esph;
170 const struct ip_esp_hdr *eh;
1da177e4
LT
171
172 /* Max length: 4 "ESP " */
173 printk("ESP ");
174
175 if (fragment) {
176 printk(")");
177 return;
178 }
179
180 /*
181 * Max length: 26 "INCOMPLETE [65535 bytes] )"
182 */
183 eh = skb_header_pointer(skb, ptr, sizeof(_esph),
184 &_esph);
185 if (eh == NULL) {
186 printk("INCOMPLETE [%u bytes] )",
187 skb->len - ptr);
188 return;
189 }
190
191 /* Length: 16 "SPI=0xF1234567 )" */
192 printk("SPI=0x%x )", ntohl(eh->spi) );
193
194 }
195 return;
196 default:
197 /* Max length: 20 "Unknown Ext Hdr 255" */
198 printk("Unknown Ext Hdr %u", currenthdr);
199 return;
200 }
608c8e4f 201 if (logflags & IP6T_LOG_IPOPT)
1da177e4
LT
202 printk(") ");
203
204 currenthdr = hp->nexthdr;
205 ptr += hdrlen;
206 }
207
208 switch (currenthdr) {
209 case IPPROTO_TCP: {
a47362a2
JE
210 struct tcphdr _tcph;
211 const struct tcphdr *th;
1da177e4
LT
212
213 /* Max length: 10 "PROTO=TCP " */
214 printk("PROTO=TCP ");
215
216 if (fragment)
217 break;
218
219 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
220 th = skb_header_pointer(skb, ptr, sizeof(_tcph), &_tcph);
221 if (th == NULL) {
222 printk("INCOMPLETE [%u bytes] ", skb->len - ptr);
223 return;
224 }
225
226 /* Max length: 20 "SPT=65535 DPT=65535 " */
227 printk("SPT=%u DPT=%u ",
228 ntohs(th->source), ntohs(th->dest));
229 /* Max length: 30 "SEQ=4294967295 ACK=4294967295 " */
608c8e4f 230 if (logflags & IP6T_LOG_TCPSEQ)
1da177e4
LT
231 printk("SEQ=%u ACK=%u ",
232 ntohl(th->seq), ntohl(th->ack_seq));
233 /* Max length: 13 "WINDOW=65535 " */
234 printk("WINDOW=%u ", ntohs(th->window));
235 /* Max length: 9 "RES=0x3C " */
236 printk("RES=0x%02x ", (u_int8_t)(ntohl(tcp_flag_word(th) & TCP_RESERVED_BITS) >> 22));
237 /* Max length: 32 "CWR ECE URG ACK PSH RST SYN FIN " */
238 if (th->cwr)
239 printk("CWR ");
240 if (th->ece)
241 printk("ECE ");
242 if (th->urg)
243 printk("URG ");
244 if (th->ack)
245 printk("ACK ");
246 if (th->psh)
247 printk("PSH ");
248 if (th->rst)
249 printk("RST ");
250 if (th->syn)
251 printk("SYN ");
252 if (th->fin)
253 printk("FIN ");
254 /* Max length: 11 "URGP=65535 " */
255 printk("URGP=%u ", ntohs(th->urg_ptr));
256
608c8e4f 257 if ((logflags & IP6T_LOG_TCPOPT)
1da177e4 258 && th->doff * 4 > sizeof(struct tcphdr)) {
a47362a2
JE
259 u_int8_t _opt[60 - sizeof(struct tcphdr)];
260 const u_int8_t *op;
1da177e4
LT
261 unsigned int i;
262 unsigned int optsize = th->doff * 4
263 - sizeof(struct tcphdr);
264
265 op = skb_header_pointer(skb,
266 ptr + sizeof(struct tcphdr),
267 optsize, _opt);
268 if (op == NULL) {
269 printk("OPT (TRUNCATED)");
270 return;
271 }
272
273 /* Max length: 127 "OPT (" 15*4*2chars ") " */
274 printk("OPT (");
275 for (i =0; i < optsize; i++)
276 printk("%02X", op[i]);
277 printk(") ");
278 }
279 break;
280 }
ba4e58ec
GR
281 case IPPROTO_UDP:
282 case IPPROTO_UDPLITE: {
a47362a2
JE
283 struct udphdr _udph;
284 const struct udphdr *uh;
1da177e4 285
ba4e58ec
GR
286 if (currenthdr == IPPROTO_UDP)
287 /* Max length: 10 "PROTO=UDP " */
288 printk("PROTO=UDP " );
289 else /* Max length: 14 "PROTO=UDPLITE " */
290 printk("PROTO=UDPLITE ");
1da177e4
LT
291
292 if (fragment)
293 break;
294
295 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
296 uh = skb_header_pointer(skb, ptr, sizeof(_udph), &_udph);
297 if (uh == NULL) {
298 printk("INCOMPLETE [%u bytes] ", skb->len - ptr);
299 return;
300 }
301
302 /* Max length: 20 "SPT=65535 DPT=65535 " */
303 printk("SPT=%u DPT=%u LEN=%u ",
304 ntohs(uh->source), ntohs(uh->dest),
305 ntohs(uh->len));
306 break;
307 }
308 case IPPROTO_ICMPV6: {
a47362a2
JE
309 struct icmp6hdr _icmp6h;
310 const struct icmp6hdr *ic;
1da177e4
LT
311
312 /* Max length: 13 "PROTO=ICMPv6 " */
313 printk("PROTO=ICMPv6 ");
314
315 if (fragment)
316 break;
317
318 /* Max length: 25 "INCOMPLETE [65535 bytes] " */
319 ic = skb_header_pointer(skb, ptr, sizeof(_icmp6h), &_icmp6h);
320 if (ic == NULL) {
321 printk("INCOMPLETE [%u bytes] ", skb->len - ptr);
322 return;
323 }
324
325 /* Max length: 18 "TYPE=255 CODE=255 " */
326 printk("TYPE=%u CODE=%u ", ic->icmp6_type, ic->icmp6_code);
327
328 switch (ic->icmp6_type) {
329 case ICMPV6_ECHO_REQUEST:
330 case ICMPV6_ECHO_REPLY:
331 /* Max length: 19 "ID=65535 SEQ=65535 " */
332 printk("ID=%u SEQ=%u ",
333 ntohs(ic->icmp6_identifier),
334 ntohs(ic->icmp6_sequence));
335 break;
336 case ICMPV6_MGM_QUERY:
337 case ICMPV6_MGM_REPORT:
338 case ICMPV6_MGM_REDUCTION:
339 break;
340
341 case ICMPV6_PARAMPROB:
342 /* Max length: 17 "POINTER=ffffffff " */
343 printk("POINTER=%08x ", ntohl(ic->icmp6_pointer));
344 /* Fall through */
345 case ICMPV6_DEST_UNREACH:
346 case ICMPV6_PKT_TOOBIG:
347 case ICMPV6_TIME_EXCEED:
348 /* Max length: 3+maxlen */
349 if (recurse) {
350 printk("[");
351 dump_packet(info, skb, ptr + sizeof(_icmp6h),
352 0);
353 printk("] ");
354 }
355
356 /* Max length: 10 "MTU=65535 " */
357 if (ic->icmp6_type == ICMPV6_PKT_TOOBIG)
358 printk("MTU=%u ", ntohl(ic->icmp6_mtu));
359 }
360 break;
361 }
362 /* Max length: 10 "PROTO=255 " */
363 default:
364 printk("PROTO=%u ", currenthdr);
365 }
366
367 /* Max length: 15 "UID=4294967295 " */
608c8e4f 368 if ((logflags & IP6T_LOG_UID) && recurse && skb->sk) {
1da177e4
LT
369 read_lock_bh(&skb->sk->sk_callback_lock);
370 if (skb->sk->sk_socket && skb->sk->sk_socket->file)
371 printk("UID=%u ", skb->sk->sk_socket->file->f_uid);
372 read_unlock_bh(&skb->sk->sk_callback_lock);
373 }
374}
375
608c8e4f
HW
376static struct nf_loginfo default_loginfo = {
377 .type = NF_LOG_TYPE_LOG,
378 .u = {
379 .log = {
380 .level = 0,
381 .logflags = NF_LOG_MASK,
382 },
383 },
384};
385
1da177e4 386static void
608c8e4f
HW
387ip6t_log_packet(unsigned int pf,
388 unsigned int hooknum,
1da177e4
LT
389 const struct sk_buff *skb,
390 const struct net_device *in,
391 const struct net_device *out,
608c8e4f 392 const struct nf_loginfo *loginfo,
1da177e4
LT
393 const char *prefix)
394{
608c8e4f
HW
395 if (!loginfo)
396 loginfo = &default_loginfo;
397
1da177e4 398 spin_lock_bh(&log_lock);
1ab1457c 399 printk("<%d>%sIN=%s OUT=%s ", loginfo->u.log.level,
608c8e4f 400 prefix,
1da177e4
LT
401 in ? in->name : "",
402 out ? out->name : "");
403 if (in && !out) {
d3984a6b 404 unsigned int len;
1da177e4
LT
405 /* MAC logging for input chain only. */
406 printk("MAC=");
d3984a6b 407 if (skb->dev && (len = skb->dev->hard_header_len) &&
b0e380b1 408 skb->mac_header != skb->network_header) {
98e399f8 409 const unsigned char *p = skb_mac_header(skb);
047601bf
PM
410 int i;
411
412 if (skb->dev->type == ARPHRD_SIT &&
413 (p -= ETH_HLEN) < skb->head)
414 p = NULL;
415
d3984a6b
PM
416 if (p != NULL) {
417 for (i = 0; i < len; i++)
418 printk("%02x%s", p[i],
419 i == len - 1 ? "" : ":");
420 }
047601bf
PM
421 printk(" ");
422
423 if (skb->dev->type == ARPHRD_SIT) {
98e399f8
ACM
424 const struct iphdr *iph =
425 (struct iphdr *)skb_mac_header(skb);
047601bf
PM
426 printk("TUNNEL=%u.%u.%u.%u->%u.%u.%u.%u ",
427 NIPQUAD(iph->saddr),
428 NIPQUAD(iph->daddr));
1da177e4
LT
429 }
430 } else
431 printk(" ");
432 }
433
0660e03f 434 dump_packet(loginfo, skb, skb_network_offset(skb), 1);
1da177e4
LT
435 printk("\n");
436 spin_unlock_bh(&log_lock);
437}
438
439static unsigned int
440ip6t_log_target(struct sk_buff **pskb,
441 const struct net_device *in,
442 const struct net_device *out,
443 unsigned int hooknum,
c4986734 444 const struct xt_target *target,
fe1cb108 445 const void *targinfo)
1da177e4
LT
446{
447 const struct ip6t_log_info *loginfo = targinfo;
608c8e4f
HW
448 struct nf_loginfo li;
449
450 li.type = NF_LOG_TYPE_LOG;
451 li.u.log.level = loginfo->level;
452 li.u.log.logflags = loginfo->logflags;
1da177e4 453
baf7b1e1 454 ip6t_log_packet(PF_INET6, hooknum, *pskb, in, out, &li,
1ab1457c 455 loginfo->prefix);
6709dbbb 456 return XT_CONTINUE;
1da177e4
LT
457}
458
1da177e4 459
e1931b78
JE
460static bool ip6t_log_checkentry(const char *tablename,
461 const void *entry,
462 const struct xt_target *target,
463 void *targinfo,
464 unsigned int hook_mask)
1da177e4
LT
465{
466 const struct ip6t_log_info *loginfo = targinfo;
467
1da177e4
LT
468 if (loginfo->level >= 8) {
469 DEBUGP("LOG: level %u >= 8\n", loginfo->level);
e1931b78 470 return false;
1da177e4 471 }
1da177e4
LT
472 if (loginfo->prefix[sizeof(loginfo->prefix)-1] != '\0') {
473 DEBUGP("LOG: prefix term %i\n",
474 loginfo->prefix[sizeof(loginfo->prefix)-1]);
e1931b78 475 return false;
1da177e4 476 }
e1931b78 477 return true;
1da177e4
LT
478}
479
9f15c530 480static struct xt_target ip6t_log_reg __read_mostly = {
1da177e4 481 .name = "LOG",
6709dbbb 482 .family = AF_INET6,
1ab1457c 483 .target = ip6t_log_target,
7f939713 484 .targetsize = sizeof(struct ip6t_log_info),
1ab1457c 485 .checkentry = ip6t_log_checkentry,
1da177e4
LT
486 .me = THIS_MODULE,
487};
488
608c8e4f
HW
489static struct nf_logger ip6t_logger = {
490 .name = "ip6t_LOG",
491 .logfn = &ip6t_log_packet,
492 .me = THIS_MODULE,
493};
494
65b4b4e8 495static int __init ip6t_log_init(void)
1da177e4 496{
e1fd0586
JE
497 int ret;
498
6709dbbb 499 ret = xt_register_target(&ip6t_log_reg);
e1fd0586
JE
500 if (ret < 0)
501 return ret;
3b5018d6
PM
502 ret = nf_log_register(PF_INET6, &ip6t_logger);
503 if (ret < 0 && ret != -EEXIST)
504 xt_unregister_target(&ip6t_log_reg);
505 return ret;
1da177e4
LT
506}
507
65b4b4e8 508static void __exit ip6t_log_fini(void)
1da177e4 509{
e92ad99c 510 nf_log_unregister(&ip6t_logger);
6709dbbb 511 xt_unregister_target(&ip6t_log_reg);
1da177e4
LT
512}
513
65b4b4e8
AM
514module_init(ip6t_log_init);
515module_exit(ip6t_log_fini);
This page took 0.290571 seconds and 5 git commands to generate.