]>
Commit | Line | Data |
---|---|---|
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 | 25 | #include <linux/netfilter_ipv6/ip6_tables.h> |
f01ffbd6 | 26 | #include <net/netfilter/nf_log.h> |
1da177e4 LT |
27 | |
28 | MODULE_AUTHOR("Jan Rekorajski <baggins@pld.org.pl>"); | |
2ae15b64 | 29 | MODULE_DESCRIPTION("Xtables: IPv6 packet logging to syslog"); |
1da177e4 LT |
30 | MODULE_LICENSE("GPL"); |
31 | ||
1da177e4 LT |
32 | struct in_device; |
33 | #include <net/route.h> | |
34 | #include <linux/netfilter_ipv6/ip6t_LOG.h> | |
35 | ||
1da177e4 LT |
36 | /* Use lock to serialize, so printks don't overlap */ |
37 | static DEFINE_SPINLOCK(log_lock); | |
38 | ||
39 | /* One level of recursion won't kill us */ | |
608c8e4f | 40 | static void dump_packet(const struct nf_loginfo *info, |
1da177e4 LT |
41 | const struct sk_buff *skb, unsigned int ip6hoff, |
42 | int recurse) | |
43 | { | |
44 | u_int8_t currenthdr; | |
45 | int fragment; | |
a47362a2 JE |
46 | struct ipv6hdr _ip6h; |
47 | const struct ipv6hdr *ih; | |
1da177e4 LT |
48 | unsigned int ptr; |
49 | unsigned int hdrlen = 0; | |
608c8e4f HW |
50 | unsigned int logflags; |
51 | ||
52 | if (info->type == NF_LOG_TYPE_LOG) | |
53 | logflags = info->u.log.logflags; | |
54 | else | |
55 | logflags = NF_LOG_MASK; | |
1da177e4 LT |
56 | |
57 | ih = skb_header_pointer(skb, ip6hoff, sizeof(_ip6h), &_ip6h); | |
58 | if (ih == NULL) { | |
59 | printk("TRUNCATED"); | |
60 | return; | |
61 | } | |
62 | ||
46b86a2d JP |
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)); | |
1da177e4 LT |
65 | |
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), | |
e69a4adc | 69 | (ntohl(*(__be32 *)ih) & 0x0ff00000) >> 20, |
1da177e4 | 70 | ih->hop_limit, |
e69a4adc | 71 | (ntohl(*(__be32 *)ih) & 0x000fffff)); |
1da177e4 LT |
72 | |
73 | fragment = 0; | |
74 | ptr = ip6hoff + sizeof(struct ipv6hdr); | |
75 | currenthdr = ih->nexthdr; | |
76 | while (currenthdr != NEXTHDR_NONE && ip6t_ext_hdr(currenthdr)) { | |
a47362a2 JE |
77 | struct ipv6_opt_hdr _hdr; |
78 | const struct ipv6_opt_hdr *hp; | |
1da177e4 LT |
79 | |
80 | hp = skb_header_pointer(skb, ptr, sizeof(_hdr), &_hdr); | |
81 | if (hp == NULL) { | |
82 | printk("TRUNCATED"); | |
83 | return; | |
84 | } | |
85 | ||
86 | /* Max length: 48 "OPT (...) " */ | |
608c8e4f | 87 | if (logflags & IP6T_LOG_IPOPT) |
1da177e4 LT |
88 | printk("OPT ( "); |
89 | ||
90 | switch (currenthdr) { | |
91 | case IPPROTO_FRAGMENT: { | |
a47362a2 JE |
92 | struct frag_hdr _fhdr; |
93 | const struct frag_hdr *fh; | |
1da177e4 LT |
94 | |
95 | printk("FRAG:"); | |
96 | fh = skb_header_pointer(skb, ptr, sizeof(_fhdr), | |
97 | &_fhdr); | |
98 | if (fh == NULL) { | |
99 | printk("TRUNCATED "); | |
100 | return; | |
101 | } | |
102 | ||
103 | /* Max length: 6 "65535 " */ | |
104 | printk("%u ", ntohs(fh->frag_off) & 0xFFF8); | |
105 | ||
106 | /* Max length: 11 "INCOMPLETE " */ | |
107 | if (fh->frag_off & htons(0x0001)) | |
108 | printk("INCOMPLETE "); | |
109 | ||
110 | printk("ID:%08x ", ntohl(fh->identification)); | |
111 | ||
112 | if (ntohs(fh->frag_off) & 0xFFF8) | |
113 | fragment = 1; | |
114 | ||
115 | hdrlen = 8; | |
116 | ||
117 | break; | |
118 | } | |
119 | case IPPROTO_DSTOPTS: | |
120 | case IPPROTO_ROUTING: | |
121 | case IPPROTO_HOPOPTS: | |
122 | if (fragment) { | |
608c8e4f | 123 | if (logflags & IP6T_LOG_IPOPT) |
1da177e4 LT |
124 | printk(")"); |
125 | return; | |
126 | } | |
127 | hdrlen = ipv6_optlen(hp); | |
128 | break; | |
129 | /* Max Length */ | |
130 | case IPPROTO_AH: | |
608c8e4f | 131 | if (logflags & IP6T_LOG_IPOPT) { |
a47362a2 JE |
132 | struct ip_auth_hdr _ahdr; |
133 | const struct ip_auth_hdr *ah; | |
1da177e4 LT |
134 | |
135 | /* Max length: 3 "AH " */ | |
136 | printk("AH "); | |
137 | ||
138 | if (fragment) { | |
139 | printk(")"); | |
140 | return; | |
141 | } | |
142 | ||
143 | ah = skb_header_pointer(skb, ptr, sizeof(_ahdr), | |
144 | &_ahdr); | |
145 | if (ah == NULL) { | |
146 | /* | |
1ab1457c | 147 | * Max length: 26 "INCOMPLETE [65535 |
1da177e4 LT |
148 | * bytes] )" |
149 | */ | |
150 | printk("INCOMPLETE [%u bytes] )", | |
151 | skb->len - ptr); | |
152 | return; | |
153 | } | |
154 | ||
155 | /* Length: 15 "SPI=0xF1234567 */ | |
156 | printk("SPI=0x%x ", ntohl(ah->spi)); | |
157 | ||
158 | } | |
159 | ||
160 | hdrlen = (hp->hdrlen+2)<<2; | |
161 | break; | |
162 | case IPPROTO_ESP: | |
608c8e4f | 163 | if (logflags & IP6T_LOG_IPOPT) { |
a47362a2 JE |
164 | struct ip_esp_hdr _esph; |
165 | const struct ip_esp_hdr *eh; | |
1da177e4 LT |
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: { | |
a47362a2 JE |
205 | struct tcphdr _tcph; |
206 | const struct tcphdr *th; | |
1da177e4 LT |
207 | |
208 | /* Max length: 10 "PROTO=TCP " */ | |
209 | printk("PROTO=TCP "); | |
210 | ||
211 | if (fragment) | |
212 | break; | |
213 | ||
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; | |
219 | } | |
220 | ||
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 " */ | |
608c8e4f | 225 | if (logflags & IP6T_LOG_TCPSEQ) |
1da177e4 LT |
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)); | |
251 | ||
608c8e4f | 252 | if ((logflags & IP6T_LOG_TCPOPT) |
1da177e4 | 253 | && th->doff * 4 > sizeof(struct tcphdr)) { |
a47362a2 JE |
254 | u_int8_t _opt[60 - sizeof(struct tcphdr)]; |
255 | const u_int8_t *op; | |
1da177e4 LT |
256 | unsigned int i; |
257 | unsigned int optsize = th->doff * 4 | |
258 | - sizeof(struct tcphdr); | |
259 | ||
260 | op = skb_header_pointer(skb, | |
261 | ptr + sizeof(struct tcphdr), | |
262 | optsize, _opt); | |
263 | if (op == NULL) { | |
264 | printk("OPT (TRUNCATED)"); | |
265 | return; | |
266 | } | |
267 | ||
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(") "); | |
273 | } | |
274 | break; | |
275 | } | |
ba4e58ec GR |
276 | case IPPROTO_UDP: |
277 | case IPPROTO_UDPLITE: { | |
a47362a2 JE |
278 | struct udphdr _udph; |
279 | const struct udphdr *uh; | |
1da177e4 | 280 | |
ba4e58ec GR |
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 "); | |
1da177e4 LT |
286 | |
287 | if (fragment) | |
288 | break; | |
289 | ||
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; | |
295 | } | |
296 | ||
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; | |
302 | } | |
303 | case IPPROTO_ICMPV6: { | |
a47362a2 JE |
304 | struct icmp6hdr _icmp6h; |
305 | const struct icmp6hdr *ic; | |
1da177e4 LT |
306 | |
307 | /* Max length: 13 "PROTO=ICMPv6 " */ | |
308 | printk("PROTO=ICMPv6 "); | |
309 | ||
310 | if (fragment) | |
311 | break; | |
312 | ||
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; | |
318 | } | |
319 | ||
320 | /* Max length: 18 "TYPE=255 CODE=255 " */ | |
321 | printk("TYPE=%u CODE=%u ", ic->icmp6_type, ic->icmp6_code); | |
322 | ||
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; | |
335 | ||
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), | |
347 | 0); | |
348 | printk("] "); | |
349 | } | |
350 | ||
351 | /* Max length: 10 "MTU=65535 " */ | |
352 | if (ic->icmp6_type == ICMPV6_PKT_TOOBIG) | |
353 | printk("MTU=%u ", ntohl(ic->icmp6_mtu)); | |
354 | } | |
355 | break; | |
356 | } | |
357 | /* Max length: 10 "PROTO=255 " */ | |
358 | default: | |
359 | printk("PROTO=%u ", currenthdr); | |
360 | } | |
361 | ||
362 | /* Max length: 15 "UID=4294967295 " */ | |
608c8e4f | 363 | if ((logflags & IP6T_LOG_UID) && recurse && skb->sk) { |
1da177e4 LT |
364 | read_lock_bh(&skb->sk->sk_callback_lock); |
365 | if (skb->sk->sk_socket && skb->sk->sk_socket->file) | |
36e2a1b0 | 366 | printk("UID=%u GID=%u ", |
17dfc93f MS |
367 | skb->sk->sk_socket->file->f_uid, |
368 | skb->sk->sk_socket->file->f_gid); | |
1da177e4 LT |
369 | read_unlock_bh(&skb->sk->sk_callback_lock); |
370 | } | |
36e2a1b0 PM |
371 | |
372 | /* Max length: 16 "MARK=0xFFFFFFFF " */ | |
373 | if (!recurse && skb->mark) | |
374 | printk("MARK=0x%x ", skb->mark); | |
1da177e4 LT |
375 | } |
376 | ||
608c8e4f HW |
377 | static struct nf_loginfo default_loginfo = { |
378 | .type = NF_LOG_TYPE_LOG, | |
379 | .u = { | |
380 | .log = { | |
381 | .level = 0, | |
382 | .logflags = NF_LOG_MASK, | |
383 | }, | |
384 | }, | |
385 | }; | |
386 | ||
1da177e4 | 387 | static void |
76108cea | 388 | ip6t_log_packet(u_int8_t pf, |
608c8e4f | 389 | unsigned int hooknum, |
1da177e4 LT |
390 | const struct sk_buff *skb, |
391 | const struct net_device *in, | |
392 | const struct net_device *out, | |
608c8e4f | 393 | const struct nf_loginfo *loginfo, |
1da177e4 LT |
394 | const char *prefix) |
395 | { | |
608c8e4f HW |
396 | if (!loginfo) |
397 | loginfo = &default_loginfo; | |
398 | ||
1da177e4 | 399 | spin_lock_bh(&log_lock); |
1ab1457c | 400 | printk("<%d>%sIN=%s OUT=%s ", loginfo->u.log.level, |
608c8e4f | 401 | prefix, |
1da177e4 LT |
402 | in ? in->name : "", |
403 | out ? out->name : ""); | |
404 | if (in && !out) { | |
d3984a6b | 405 | unsigned int len; |
1da177e4 LT |
406 | /* MAC logging for input chain only. */ |
407 | printk("MAC="); | |
d3984a6b | 408 | if (skb->dev && (len = skb->dev->hard_header_len) && |
b0e380b1 | 409 | skb->mac_header != skb->network_header) { |
98e399f8 | 410 | const unsigned char *p = skb_mac_header(skb); |
047601bf PM |
411 | int i; |
412 | ||
413 | if (skb->dev->type == ARPHRD_SIT && | |
414 | (p -= ETH_HLEN) < skb->head) | |
415 | p = NULL; | |
416 | ||
d3984a6b PM |
417 | if (p != NULL) { |
418 | for (i = 0; i < len; i++) | |
419 | printk("%02x%s", p[i], | |
420 | i == len - 1 ? "" : ":"); | |
421 | } | |
047601bf PM |
422 | printk(" "); |
423 | ||
424 | if (skb->dev->type == ARPHRD_SIT) { | |
98e399f8 ACM |
425 | const struct iphdr *iph = |
426 | (struct iphdr *)skb_mac_header(skb); | |
047601bf PM |
427 | printk("TUNNEL=%u.%u.%u.%u->%u.%u.%u.%u ", |
428 | NIPQUAD(iph->saddr), | |
429 | NIPQUAD(iph->daddr)); | |
1da177e4 LT |
430 | } |
431 | } else | |
432 | printk(" "); | |
433 | } | |
434 | ||
0660e03f | 435 | dump_packet(loginfo, skb, skb_network_offset(skb), 1); |
1da177e4 LT |
436 | printk("\n"); |
437 | spin_unlock_bh(&log_lock); | |
438 | } | |
439 | ||
440 | static unsigned int | |
d3c5ee6d JE |
441 | log_tg6(struct sk_buff *skb, const struct net_device *in, |
442 | const struct net_device *out, unsigned int hooknum, | |
443 | const struct xt_target *target, const void *targinfo) | |
1da177e4 LT |
444 | { |
445 | const struct ip6t_log_info *loginfo = targinfo; | |
608c8e4f HW |
446 | struct nf_loginfo li; |
447 | ||
448 | li.type = NF_LOG_TYPE_LOG; | |
449 | li.u.log.level = loginfo->level; | |
450 | li.u.log.logflags = loginfo->logflags; | |
1da177e4 | 451 | |
ee999d8b JE |
452 | ip6t_log_packet(NFPROTO_IPV6, hooknum, skb, in, out, |
453 | &li, loginfo->prefix); | |
6709dbbb | 454 | return XT_CONTINUE; |
1da177e4 LT |
455 | } |
456 | ||
1da177e4 | 457 | |
d3c5ee6d JE |
458 | static bool |
459 | log_tg6_check(const char *tablename, const void *entry, | |
460 | const struct xt_target *target, void *targinfo, | |
461 | unsigned int hook_mask) | |
1da177e4 LT |
462 | { |
463 | const struct ip6t_log_info *loginfo = targinfo; | |
464 | ||
1da177e4 | 465 | if (loginfo->level >= 8) { |
0d53778e | 466 | pr_debug("LOG: level %u >= 8\n", loginfo->level); |
e1931b78 | 467 | return false; |
1da177e4 | 468 | } |
1da177e4 | 469 | if (loginfo->prefix[sizeof(loginfo->prefix)-1] != '\0') { |
0d53778e PM |
470 | pr_debug("LOG: prefix term %i\n", |
471 | loginfo->prefix[sizeof(loginfo->prefix)-1]); | |
e1931b78 | 472 | return false; |
1da177e4 | 473 | } |
e1931b78 | 474 | return true; |
1da177e4 LT |
475 | } |
476 | ||
d3c5ee6d | 477 | static struct xt_target log_tg6_reg __read_mostly = { |
1da177e4 | 478 | .name = "LOG", |
ee999d8b | 479 | .family = NFPROTO_IPV6, |
d3c5ee6d | 480 | .target = log_tg6, |
7f939713 | 481 | .targetsize = sizeof(struct ip6t_log_info), |
d3c5ee6d | 482 | .checkentry = log_tg6_check, |
1da177e4 LT |
483 | .me = THIS_MODULE, |
484 | }; | |
485 | ||
7b2f9631 | 486 | static const struct nf_logger ip6t_logger = { |
608c8e4f HW |
487 | .name = "ip6t_LOG", |
488 | .logfn = &ip6t_log_packet, | |
489 | .me = THIS_MODULE, | |
490 | }; | |
491 | ||
d3c5ee6d | 492 | static int __init log_tg6_init(void) |
1da177e4 | 493 | { |
e1fd0586 JE |
494 | int ret; |
495 | ||
d3c5ee6d | 496 | ret = xt_register_target(&log_tg6_reg); |
e1fd0586 JE |
497 | if (ret < 0) |
498 | return ret; | |
ee999d8b | 499 | nf_log_register(NFPROTO_IPV6, &ip6t_logger); |
7e2acc7e | 500 | return 0; |
1da177e4 LT |
501 | } |
502 | ||
d3c5ee6d | 503 | static void __exit log_tg6_exit(void) |
1da177e4 | 504 | { |
e92ad99c | 505 | nf_log_unregister(&ip6t_logger); |
d3c5ee6d | 506 | xt_unregister_target(&log_tg6_reg); |
1da177e4 LT |
507 | } |
508 | ||
d3c5ee6d JE |
509 | module_init(log_tg6_init); |
510 | module_exit(log_tg6_exit); |