Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This is a module which is used for logging packets. |
| 3 | */ |
| 4 | |
| 5 | /* (C) 1999-2001 Paul `Rusty' Russell |
| 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/spinlock.h> |
| 15 | #include <linux/skbuff.h> |
| 16 | #include <linux/ip.h> |
| 17 | #include <net/icmp.h> |
| 18 | #include <net/udp.h> |
| 19 | #include <net/tcp.h> |
| 20 | #include <net/route.h> |
| 21 | |
| 22 | #include <linux/netfilter.h> |
| 23 | #include <linux/netfilter_ipv4/ip_tables.h> |
| 24 | #include <linux/netfilter_ipv4/ipt_LOG.h> |
| 25 | |
| 26 | MODULE_LICENSE("GPL"); |
| 27 | MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>"); |
| 28 | MODULE_DESCRIPTION("iptables syslog logging module"); |
| 29 | |
| 30 | static unsigned int nflog = 1; |
| 31 | module_param(nflog, int, 0400); |
| 32 | MODULE_PARM_DESC(nflog, "register as internal netfilter logging module"); |
| 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 */ |
| 41 | static DEFINE_SPINLOCK(log_lock); |
| 42 | |
| 43 | /* One level of recursion won't kill us */ |
| 44 | static void dump_packet(const struct ipt_log_info *info, |
| 45 | const struct sk_buff *skb, |
| 46 | unsigned int iphoff) |
| 47 | { |
| 48 | struct iphdr _iph, *ih; |
| 49 | |
| 50 | ih = skb_header_pointer(skb, iphoff, sizeof(_iph), &_iph); |
| 51 | if (ih == NULL) { |
| 52 | printk("TRUNCATED"); |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | /* Important fields: |
| 57 | * TOS, len, DF/MF, fragment offset, TTL, src, dst, options. */ |
| 58 | /* Max length: 40 "SRC=255.255.255.255 DST=255.255.255.255 " */ |
| 59 | printk("SRC=%u.%u.%u.%u DST=%u.%u.%u.%u ", |
| 60 | NIPQUAD(ih->saddr), NIPQUAD(ih->daddr)); |
| 61 | |
| 62 | /* Max length: 46 "LEN=65535 TOS=0xFF PREC=0xFF TTL=255 ID=65535 " */ |
| 63 | printk("LEN=%u TOS=0x%02X PREC=0x%02X TTL=%u ID=%u ", |
| 64 | ntohs(ih->tot_len), ih->tos & IPTOS_TOS_MASK, |
| 65 | ih->tos & IPTOS_PREC_MASK, ih->ttl, ntohs(ih->id)); |
| 66 | |
| 67 | /* Max length: 6 "CE DF MF " */ |
| 68 | if (ntohs(ih->frag_off) & IP_CE) |
| 69 | printk("CE "); |
| 70 | if (ntohs(ih->frag_off) & IP_DF) |
| 71 | printk("DF "); |
| 72 | if (ntohs(ih->frag_off) & IP_MF) |
| 73 | printk("MF "); |
| 74 | |
| 75 | /* Max length: 11 "FRAG:65535 " */ |
| 76 | if (ntohs(ih->frag_off) & IP_OFFSET) |
| 77 | printk("FRAG:%u ", ntohs(ih->frag_off) & IP_OFFSET); |
| 78 | |
| 79 | if ((info->logflags & IPT_LOG_IPOPT) |
| 80 | && ih->ihl * 4 > sizeof(struct iphdr)) { |
| 81 | unsigned char _opt[4 * 15 - sizeof(struct iphdr)], *op; |
| 82 | unsigned int i, optsize; |
| 83 | |
| 84 | optsize = ih->ihl * 4 - sizeof(struct iphdr); |
| 85 | op = skb_header_pointer(skb, iphoff+sizeof(_iph), |
| 86 | optsize, _opt); |
| 87 | if (op == NULL) { |
| 88 | printk("TRUNCATED"); |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | /* Max length: 127 "OPT (" 15*4*2chars ") " */ |
| 93 | printk("OPT ("); |
| 94 | for (i = 0; i < optsize; i++) |
| 95 | printk("%02X", op[i]); |
| 96 | printk(") "); |
| 97 | } |
| 98 | |
| 99 | switch (ih->protocol) { |
| 100 | case IPPROTO_TCP: { |
| 101 | struct tcphdr _tcph, *th; |
| 102 | |
| 103 | /* Max length: 10 "PROTO=TCP " */ |
| 104 | printk("PROTO=TCP "); |
| 105 | |
| 106 | if (ntohs(ih->frag_off) & IP_OFFSET) |
| 107 | break; |
| 108 | |
| 109 | /* Max length: 25 "INCOMPLETE [65535 bytes] " */ |
| 110 | th = skb_header_pointer(skb, iphoff + ih->ihl * 4, |
| 111 | sizeof(_tcph), &_tcph); |
| 112 | if (th == NULL) { |
| 113 | printk("INCOMPLETE [%u bytes] ", |
| 114 | skb->len - iphoff - ih->ihl*4); |
| 115 | break; |
| 116 | } |
| 117 | |
| 118 | /* Max length: 20 "SPT=65535 DPT=65535 " */ |
| 119 | printk("SPT=%u DPT=%u ", |
| 120 | ntohs(th->source), ntohs(th->dest)); |
| 121 | /* Max length: 30 "SEQ=4294967295 ACK=4294967295 " */ |
| 122 | if (info->logflags & IPT_LOG_TCPSEQ) |
| 123 | printk("SEQ=%u ACK=%u ", |
| 124 | ntohl(th->seq), ntohl(th->ack_seq)); |
| 125 | /* Max length: 13 "WINDOW=65535 " */ |
| 126 | printk("WINDOW=%u ", ntohs(th->window)); |
| 127 | /* Max length: 9 "RES=0x3F " */ |
| 128 | printk("RES=0x%02x ", (u8)(ntohl(tcp_flag_word(th) & TCP_RESERVED_BITS) >> 22)); |
| 129 | /* Max length: 32 "CWR ECE URG ACK PSH RST SYN FIN " */ |
| 130 | if (th->cwr) |
| 131 | printk("CWR "); |
| 132 | if (th->ece) |
| 133 | printk("ECE "); |
| 134 | if (th->urg) |
| 135 | printk("URG "); |
| 136 | if (th->ack) |
| 137 | printk("ACK "); |
| 138 | if (th->psh) |
| 139 | printk("PSH "); |
| 140 | if (th->rst) |
| 141 | printk("RST "); |
| 142 | if (th->syn) |
| 143 | printk("SYN "); |
| 144 | if (th->fin) |
| 145 | printk("FIN "); |
| 146 | /* Max length: 11 "URGP=65535 " */ |
| 147 | printk("URGP=%u ", ntohs(th->urg_ptr)); |
| 148 | |
| 149 | if ((info->logflags & IPT_LOG_TCPOPT) |
| 150 | && th->doff * 4 > sizeof(struct tcphdr)) { |
| 151 | unsigned char _opt[4 * 15 - sizeof(struct tcphdr)]; |
| 152 | unsigned char *op; |
| 153 | unsigned int i, optsize; |
| 154 | |
| 155 | optsize = th->doff * 4 - sizeof(struct tcphdr); |
| 156 | op = skb_header_pointer(skb, |
| 157 | iphoff+ih->ihl*4+sizeof(_tcph), |
| 158 | optsize, _opt); |
| 159 | if (op == NULL) { |
| 160 | printk("TRUNCATED"); |
| 161 | return; |
| 162 | } |
| 163 | |
| 164 | /* Max length: 127 "OPT (" 15*4*2chars ") " */ |
| 165 | printk("OPT ("); |
| 166 | for (i = 0; i < optsize; i++) |
| 167 | printk("%02X", op[i]); |
| 168 | printk(") "); |
| 169 | } |
| 170 | break; |
| 171 | } |
| 172 | case IPPROTO_UDP: { |
| 173 | struct udphdr _udph, *uh; |
| 174 | |
| 175 | /* Max length: 10 "PROTO=UDP " */ |
| 176 | printk("PROTO=UDP "); |
| 177 | |
| 178 | if (ntohs(ih->frag_off) & IP_OFFSET) |
| 179 | break; |
| 180 | |
| 181 | /* Max length: 25 "INCOMPLETE [65535 bytes] " */ |
| 182 | uh = skb_header_pointer(skb, iphoff+ih->ihl*4, |
| 183 | sizeof(_udph), &_udph); |
| 184 | if (uh == NULL) { |
| 185 | printk("INCOMPLETE [%u bytes] ", |
| 186 | skb->len - iphoff - ih->ihl*4); |
| 187 | break; |
| 188 | } |
| 189 | |
| 190 | /* Max length: 20 "SPT=65535 DPT=65535 " */ |
| 191 | printk("SPT=%u DPT=%u LEN=%u ", |
| 192 | ntohs(uh->source), ntohs(uh->dest), |
| 193 | ntohs(uh->len)); |
| 194 | break; |
| 195 | } |
| 196 | case IPPROTO_ICMP: { |
| 197 | struct icmphdr _icmph, *ich; |
| 198 | static size_t required_len[NR_ICMP_TYPES+1] |
| 199 | = { [ICMP_ECHOREPLY] = 4, |
| 200 | [ICMP_DEST_UNREACH] |
| 201 | = 8 + sizeof(struct iphdr), |
| 202 | [ICMP_SOURCE_QUENCH] |
| 203 | = 8 + sizeof(struct iphdr), |
| 204 | [ICMP_REDIRECT] |
| 205 | = 8 + sizeof(struct iphdr), |
| 206 | [ICMP_ECHO] = 4, |
| 207 | [ICMP_TIME_EXCEEDED] |
| 208 | = 8 + sizeof(struct iphdr), |
| 209 | [ICMP_PARAMETERPROB] |
| 210 | = 8 + sizeof(struct iphdr), |
| 211 | [ICMP_TIMESTAMP] = 20, |
| 212 | [ICMP_TIMESTAMPREPLY] = 20, |
| 213 | [ICMP_ADDRESS] = 12, |
| 214 | [ICMP_ADDRESSREPLY] = 12 }; |
| 215 | |
| 216 | /* Max length: 11 "PROTO=ICMP " */ |
| 217 | printk("PROTO=ICMP "); |
| 218 | |
| 219 | if (ntohs(ih->frag_off) & IP_OFFSET) |
| 220 | break; |
| 221 | |
| 222 | /* Max length: 25 "INCOMPLETE [65535 bytes] " */ |
| 223 | ich = skb_header_pointer(skb, iphoff + ih->ihl * 4, |
| 224 | sizeof(_icmph), &_icmph); |
| 225 | if (ich == NULL) { |
| 226 | printk("INCOMPLETE [%u bytes] ", |
| 227 | skb->len - iphoff - ih->ihl*4); |
| 228 | break; |
| 229 | } |
| 230 | |
| 231 | /* Max length: 18 "TYPE=255 CODE=255 " */ |
| 232 | printk("TYPE=%u CODE=%u ", ich->type, ich->code); |
| 233 | |
| 234 | /* Max length: 25 "INCOMPLETE [65535 bytes] " */ |
| 235 | if (ich->type <= NR_ICMP_TYPES |
| 236 | && required_len[ich->type] |
| 237 | && skb->len-iphoff-ih->ihl*4 < required_len[ich->type]) { |
| 238 | printk("INCOMPLETE [%u bytes] ", |
| 239 | skb->len - iphoff - ih->ihl*4); |
| 240 | break; |
| 241 | } |
| 242 | |
| 243 | switch (ich->type) { |
| 244 | case ICMP_ECHOREPLY: |
| 245 | case ICMP_ECHO: |
| 246 | /* Max length: 19 "ID=65535 SEQ=65535 " */ |
| 247 | printk("ID=%u SEQ=%u ", |
| 248 | ntohs(ich->un.echo.id), |
| 249 | ntohs(ich->un.echo.sequence)); |
| 250 | break; |
| 251 | |
| 252 | case ICMP_PARAMETERPROB: |
| 253 | /* Max length: 14 "PARAMETER=255 " */ |
| 254 | printk("PARAMETER=%u ", |
| 255 | ntohl(ich->un.gateway) >> 24); |
| 256 | break; |
| 257 | case ICMP_REDIRECT: |
| 258 | /* Max length: 24 "GATEWAY=255.255.255.255 " */ |
| 259 | printk("GATEWAY=%u.%u.%u.%u ", |
| 260 | NIPQUAD(ich->un.gateway)); |
| 261 | /* Fall through */ |
| 262 | case ICMP_DEST_UNREACH: |
| 263 | case ICMP_SOURCE_QUENCH: |
| 264 | case ICMP_TIME_EXCEEDED: |
| 265 | /* Max length: 3+maxlen */ |
| 266 | if (!iphoff) { /* Only recurse once. */ |
| 267 | printk("["); |
| 268 | dump_packet(info, skb, |
| 269 | iphoff + ih->ihl*4+sizeof(_icmph)); |
| 270 | printk("] "); |
| 271 | } |
| 272 | |
| 273 | /* Max length: 10 "MTU=65535 " */ |
| 274 | if (ich->type == ICMP_DEST_UNREACH |
| 275 | && ich->code == ICMP_FRAG_NEEDED) |
| 276 | printk("MTU=%u ", ntohs(ich->un.frag.mtu)); |
| 277 | } |
| 278 | break; |
| 279 | } |
| 280 | /* Max Length */ |
| 281 | case IPPROTO_AH: { |
| 282 | struct ip_auth_hdr _ahdr, *ah; |
| 283 | |
| 284 | if (ntohs(ih->frag_off) & IP_OFFSET) |
| 285 | break; |
| 286 | |
| 287 | /* Max length: 9 "PROTO=AH " */ |
| 288 | printk("PROTO=AH "); |
| 289 | |
| 290 | /* Max length: 25 "INCOMPLETE [65535 bytes] " */ |
| 291 | ah = skb_header_pointer(skb, iphoff+ih->ihl*4, |
| 292 | sizeof(_ahdr), &_ahdr); |
| 293 | if (ah == NULL) { |
| 294 | printk("INCOMPLETE [%u bytes] ", |
| 295 | skb->len - iphoff - ih->ihl*4); |
| 296 | break; |
| 297 | } |
| 298 | |
| 299 | /* Length: 15 "SPI=0xF1234567 " */ |
| 300 | printk("SPI=0x%x ", ntohl(ah->spi)); |
| 301 | break; |
| 302 | } |
| 303 | case IPPROTO_ESP: { |
| 304 | struct ip_esp_hdr _esph, *eh; |
| 305 | |
| 306 | /* Max length: 10 "PROTO=ESP " */ |
| 307 | printk("PROTO=ESP "); |
| 308 | |
| 309 | if (ntohs(ih->frag_off) & IP_OFFSET) |
| 310 | break; |
| 311 | |
| 312 | /* Max length: 25 "INCOMPLETE [65535 bytes] " */ |
| 313 | eh = skb_header_pointer(skb, iphoff+ih->ihl*4, |
| 314 | sizeof(_esph), &_esph); |
| 315 | if (eh == NULL) { |
| 316 | printk("INCOMPLETE [%u bytes] ", |
| 317 | skb->len - iphoff - ih->ihl*4); |
| 318 | break; |
| 319 | } |
| 320 | |
| 321 | /* Length: 15 "SPI=0xF1234567 " */ |
| 322 | printk("SPI=0x%x ", ntohl(eh->spi)); |
| 323 | break; |
| 324 | } |
| 325 | /* Max length: 10 "PROTO 255 " */ |
| 326 | default: |
| 327 | printk("PROTO=%u ", ih->protocol); |
| 328 | } |
| 329 | |
| 330 | /* Max length: 15 "UID=4294967295 " */ |
| 331 | if ((info->logflags & IPT_LOG_UID) && !iphoff && skb->sk) { |
| 332 | read_lock_bh(&skb->sk->sk_callback_lock); |
| 333 | if (skb->sk->sk_socket && skb->sk->sk_socket->file) |
| 334 | printk("UID=%u ", skb->sk->sk_socket->file->f_uid); |
| 335 | read_unlock_bh(&skb->sk->sk_callback_lock); |
| 336 | } |
| 337 | |
| 338 | /* Proto Max log string length */ |
| 339 | /* IP: 40+46+6+11+127 = 230 */ |
| 340 | /* TCP: 10+max(25,20+30+13+9+32+11+127) = 252 */ |
| 341 | /* UDP: 10+max(25,20) = 35 */ |
| 342 | /* ICMP: 11+max(25, 18+25+max(19,14,24+3+n+10,3+n+10)) = 91+n */ |
| 343 | /* ESP: 10+max(25)+15 = 50 */ |
| 344 | /* AH: 9+max(25)+15 = 49 */ |
| 345 | /* unknown: 10 */ |
| 346 | |
| 347 | /* (ICMP allows recursion one level deep) */ |
| 348 | /* maxlen = IP + ICMP + IP + max(TCP,UDP,ICMP,unknown) */ |
| 349 | /* maxlen = 230+ 91 + 230 + 252 = 803 */ |
| 350 | } |
| 351 | |
| 352 | static void |
| 353 | ipt_log_packet(unsigned int hooknum, |
| 354 | const struct sk_buff *skb, |
| 355 | const struct net_device *in, |
| 356 | const struct net_device *out, |
| 357 | const struct ipt_log_info *loginfo, |
| 358 | const char *level_string, |
| 359 | const char *prefix) |
| 360 | { |
| 361 | spin_lock_bh(&log_lock); |
| 362 | printk(level_string); |
| 363 | printk("%sIN=%s OUT=%s ", |
| 364 | prefix == NULL ? loginfo->prefix : prefix, |
| 365 | in ? in->name : "", |
| 366 | out ? out->name : ""); |
| 367 | #ifdef CONFIG_BRIDGE_NETFILTER |
| 368 | if (skb->nf_bridge) { |
| 369 | struct net_device *physindev = skb->nf_bridge->physindev; |
| 370 | struct net_device *physoutdev = skb->nf_bridge->physoutdev; |
| 371 | |
| 372 | if (physindev && in != physindev) |
| 373 | printk("PHYSIN=%s ", physindev->name); |
| 374 | if (physoutdev && out != physoutdev) |
| 375 | printk("PHYSOUT=%s ", physoutdev->name); |
| 376 | } |
| 377 | #endif |
| 378 | |
| 379 | if (in && !out) { |
| 380 | /* MAC logging for input chain only. */ |
| 381 | printk("MAC="); |
| 382 | if (skb->dev && skb->dev->hard_header_len |
| 383 | && skb->mac.raw != (void*)skb->nh.iph) { |
| 384 | int i; |
| 385 | unsigned char *p = skb->mac.raw; |
| 386 | for (i = 0; i < skb->dev->hard_header_len; i++,p++) |
| 387 | printk("%02x%c", *p, |
| 388 | i==skb->dev->hard_header_len - 1 |
| 389 | ? ' ':':'); |
| 390 | } else |
| 391 | printk(" "); |
| 392 | } |
| 393 | |
| 394 | dump_packet(loginfo, skb, 0); |
| 395 | printk("\n"); |
| 396 | spin_unlock_bh(&log_lock); |
| 397 | } |
| 398 | |
| 399 | static unsigned int |
| 400 | ipt_log_target(struct sk_buff **pskb, |
| 401 | const struct net_device *in, |
| 402 | const struct net_device *out, |
| 403 | unsigned int hooknum, |
| 404 | const void *targinfo, |
| 405 | void *userinfo) |
| 406 | { |
| 407 | const struct ipt_log_info *loginfo = targinfo; |
| 408 | char level_string[4] = "< >"; |
| 409 | |
| 410 | level_string[1] = '0' + (loginfo->level % 8); |
| 411 | ipt_log_packet(hooknum, *pskb, in, out, loginfo, level_string, NULL); |
| 412 | |
| 413 | return IPT_CONTINUE; |
| 414 | } |
| 415 | |
| 416 | static void |
| 417 | ipt_logfn(unsigned int hooknum, |
| 418 | const struct sk_buff *skb, |
| 419 | const struct net_device *in, |
| 420 | const struct net_device *out, |
| 421 | const char *prefix) |
| 422 | { |
| 423 | struct ipt_log_info loginfo = { |
| 424 | .level = 0, |
| 425 | .logflags = IPT_LOG_MASK, |
| 426 | .prefix = "" |
| 427 | }; |
| 428 | |
| 429 | ipt_log_packet(hooknum, skb, in, out, &loginfo, KERN_WARNING, prefix); |
| 430 | } |
| 431 | |
| 432 | static int ipt_log_checkentry(const char *tablename, |
| 433 | const struct ipt_entry *e, |
| 434 | void *targinfo, |
| 435 | unsigned int targinfosize, |
| 436 | unsigned int hook_mask) |
| 437 | { |
| 438 | const struct ipt_log_info *loginfo = targinfo; |
| 439 | |
| 440 | if (targinfosize != IPT_ALIGN(sizeof(struct ipt_log_info))) { |
| 441 | DEBUGP("LOG: targinfosize %u != %u\n", |
| 442 | targinfosize, IPT_ALIGN(sizeof(struct ipt_log_info))); |
| 443 | return 0; |
| 444 | } |
| 445 | |
| 446 | if (loginfo->level >= 8) { |
| 447 | DEBUGP("LOG: level %u >= 8\n", loginfo->level); |
| 448 | return 0; |
| 449 | } |
| 450 | |
| 451 | if (loginfo->prefix[sizeof(loginfo->prefix)-1] != '\0') { |
| 452 | DEBUGP("LOG: prefix term %i\n", |
| 453 | loginfo->prefix[sizeof(loginfo->prefix)-1]); |
| 454 | return 0; |
| 455 | } |
| 456 | |
| 457 | return 1; |
| 458 | } |
| 459 | |
| 460 | static struct ipt_target ipt_log_reg = { |
| 461 | .name = "LOG", |
| 462 | .target = ipt_log_target, |
| 463 | .checkentry = ipt_log_checkentry, |
| 464 | .me = THIS_MODULE, |
| 465 | }; |
| 466 | |
| 467 | static int __init init(void) |
| 468 | { |
| 469 | if (ipt_register_target(&ipt_log_reg)) |
| 470 | return -EINVAL; |
| 471 | if (nflog) |
| 472 | nf_log_register(PF_INET, &ipt_logfn); |
| 473 | |
| 474 | return 0; |
| 475 | } |
| 476 | |
| 477 | static void __exit fini(void) |
| 478 | { |
| 479 | if (nflog) |
| 480 | nf_log_unregister(PF_INET, &ipt_logfn); |
| 481 | ipt_unregister_target(&ipt_log_reg); |
| 482 | } |
| 483 | |
| 484 | module_init(init); |
| 485 | module_exit(fini); |