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