at v3.6-rc7 926 lines 23 kB view raw
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#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 14#include <linux/module.h> 15#include <linux/spinlock.h> 16#include <linux/skbuff.h> 17#include <linux/if_arp.h> 18#include <linux/ip.h> 19#include <net/ipv6.h> 20#include <net/icmp.h> 21#include <net/udp.h> 22#include <net/tcp.h> 23#include <net/route.h> 24 25#include <linux/netfilter.h> 26#include <linux/netfilter/x_tables.h> 27#include <linux/netfilter/xt_LOG.h> 28#include <linux/netfilter_ipv6/ip6_tables.h> 29#include <net/netfilter/nf_log.h> 30#include <net/netfilter/xt_log.h> 31 32static struct nf_loginfo default_loginfo = { 33 .type = NF_LOG_TYPE_LOG, 34 .u = { 35 .log = { 36 .level = 5, 37 .logflags = NF_LOG_MASK, 38 }, 39 }, 40}; 41 42static int dump_udp_header(struct sbuff *m, const struct sk_buff *skb, 43 u8 proto, int fragment, unsigned int offset) 44{ 45 struct udphdr _udph; 46 const struct udphdr *uh; 47 48 if (proto == IPPROTO_UDP) 49 /* Max length: 10 "PROTO=UDP " */ 50 sb_add(m, "PROTO=UDP "); 51 else /* Max length: 14 "PROTO=UDPLITE " */ 52 sb_add(m, "PROTO=UDPLITE "); 53 54 if (fragment) 55 goto out; 56 57 /* Max length: 25 "INCOMPLETE [65535 bytes] " */ 58 uh = skb_header_pointer(skb, offset, sizeof(_udph), &_udph); 59 if (uh == NULL) { 60 sb_add(m, "INCOMPLETE [%u bytes] ", skb->len - offset); 61 62 return 1; 63 } 64 65 /* Max length: 20 "SPT=65535 DPT=65535 " */ 66 sb_add(m, "SPT=%u DPT=%u LEN=%u ", ntohs(uh->source), ntohs(uh->dest), 67 ntohs(uh->len)); 68 69out: 70 return 0; 71} 72 73static int dump_tcp_header(struct sbuff *m, const struct sk_buff *skb, 74 u8 proto, int fragment, unsigned int offset, 75 unsigned int logflags) 76{ 77 struct tcphdr _tcph; 78 const struct tcphdr *th; 79 80 /* Max length: 10 "PROTO=TCP " */ 81 sb_add(m, "PROTO=TCP "); 82 83 if (fragment) 84 return 0; 85 86 /* Max length: 25 "INCOMPLETE [65535 bytes] " */ 87 th = skb_header_pointer(skb, offset, sizeof(_tcph), &_tcph); 88 if (th == NULL) { 89 sb_add(m, "INCOMPLETE [%u bytes] ", skb->len - offset); 90 return 1; 91 } 92 93 /* Max length: 20 "SPT=65535 DPT=65535 " */ 94 sb_add(m, "SPT=%u DPT=%u ", ntohs(th->source), ntohs(th->dest)); 95 /* Max length: 30 "SEQ=4294967295 ACK=4294967295 " */ 96 if (logflags & XT_LOG_TCPSEQ) 97 sb_add(m, "SEQ=%u ACK=%u ", ntohl(th->seq), ntohl(th->ack_seq)); 98 99 /* Max length: 13 "WINDOW=65535 " */ 100 sb_add(m, "WINDOW=%u ", ntohs(th->window)); 101 /* Max length: 9 "RES=0x3C " */ 102 sb_add(m, "RES=0x%02x ", (u_int8_t)(ntohl(tcp_flag_word(th) & 103 TCP_RESERVED_BITS) >> 22)); 104 /* Max length: 32 "CWR ECE URG ACK PSH RST SYN FIN " */ 105 if (th->cwr) 106 sb_add(m, "CWR "); 107 if (th->ece) 108 sb_add(m, "ECE "); 109 if (th->urg) 110 sb_add(m, "URG "); 111 if (th->ack) 112 sb_add(m, "ACK "); 113 if (th->psh) 114 sb_add(m, "PSH "); 115 if (th->rst) 116 sb_add(m, "RST "); 117 if (th->syn) 118 sb_add(m, "SYN "); 119 if (th->fin) 120 sb_add(m, "FIN "); 121 /* Max length: 11 "URGP=65535 " */ 122 sb_add(m, "URGP=%u ", ntohs(th->urg_ptr)); 123 124 if ((logflags & XT_LOG_TCPOPT) && th->doff*4 > sizeof(struct tcphdr)) { 125 u_int8_t _opt[60 - sizeof(struct tcphdr)]; 126 const u_int8_t *op; 127 unsigned int i; 128 unsigned int optsize = th->doff*4 - sizeof(struct tcphdr); 129 130 op = skb_header_pointer(skb, offset + sizeof(struct tcphdr), 131 optsize, _opt); 132 if (op == NULL) { 133 sb_add(m, "OPT (TRUNCATED)"); 134 return 1; 135 } 136 137 /* Max length: 127 "OPT (" 15*4*2chars ") " */ 138 sb_add(m, "OPT ("); 139 for (i = 0; i < optsize; i++) 140 sb_add(m, "%02X", op[i]); 141 142 sb_add(m, ") "); 143 } 144 145 return 0; 146} 147 148static void dump_sk_uid_gid(struct sbuff *m, struct sock *sk) 149{ 150 if (!sk || sk->sk_state == TCP_TIME_WAIT) 151 return; 152 153 read_lock_bh(&sk->sk_callback_lock); 154 if (sk->sk_socket && sk->sk_socket->file) 155 sb_add(m, "UID=%u GID=%u ", 156 sk->sk_socket->file->f_cred->fsuid, 157 sk->sk_socket->file->f_cred->fsgid); 158 read_unlock_bh(&sk->sk_callback_lock); 159} 160 161/* One level of recursion won't kill us */ 162static void dump_ipv4_packet(struct sbuff *m, 163 const struct nf_loginfo *info, 164 const struct sk_buff *skb, 165 unsigned int iphoff) 166{ 167 struct iphdr _iph; 168 const struct iphdr *ih; 169 unsigned int logflags; 170 171 if (info->type == NF_LOG_TYPE_LOG) 172 logflags = info->u.log.logflags; 173 else 174 logflags = NF_LOG_MASK; 175 176 ih = skb_header_pointer(skb, iphoff, sizeof(_iph), &_iph); 177 if (ih == NULL) { 178 sb_add(m, "TRUNCATED"); 179 return; 180 } 181 182 /* Important fields: 183 * TOS, len, DF/MF, fragment offset, TTL, src, dst, options. */ 184 /* Max length: 40 "SRC=255.255.255.255 DST=255.255.255.255 " */ 185 sb_add(m, "SRC=%pI4 DST=%pI4 ", 186 &ih->saddr, &ih->daddr); 187 188 /* Max length: 46 "LEN=65535 TOS=0xFF PREC=0xFF TTL=255 ID=65535 " */ 189 sb_add(m, "LEN=%u TOS=0x%02X PREC=0x%02X TTL=%u ID=%u ", 190 ntohs(ih->tot_len), ih->tos & IPTOS_TOS_MASK, 191 ih->tos & IPTOS_PREC_MASK, ih->ttl, ntohs(ih->id)); 192 193 /* Max length: 6 "CE DF MF " */ 194 if (ntohs(ih->frag_off) & IP_CE) 195 sb_add(m, "CE "); 196 if (ntohs(ih->frag_off) & IP_DF) 197 sb_add(m, "DF "); 198 if (ntohs(ih->frag_off) & IP_MF) 199 sb_add(m, "MF "); 200 201 /* Max length: 11 "FRAG:65535 " */ 202 if (ntohs(ih->frag_off) & IP_OFFSET) 203 sb_add(m, "FRAG:%u ", ntohs(ih->frag_off) & IP_OFFSET); 204 205 if ((logflags & XT_LOG_IPOPT) && 206 ih->ihl * 4 > sizeof(struct iphdr)) { 207 const unsigned char *op; 208 unsigned char _opt[4 * 15 - sizeof(struct iphdr)]; 209 unsigned int i, optsize; 210 211 optsize = ih->ihl * 4 - sizeof(struct iphdr); 212 op = skb_header_pointer(skb, iphoff+sizeof(_iph), 213 optsize, _opt); 214 if (op == NULL) { 215 sb_add(m, "TRUNCATED"); 216 return; 217 } 218 219 /* Max length: 127 "OPT (" 15*4*2chars ") " */ 220 sb_add(m, "OPT ("); 221 for (i = 0; i < optsize; i++) 222 sb_add(m, "%02X", op[i]); 223 sb_add(m, ") "); 224 } 225 226 switch (ih->protocol) { 227 case IPPROTO_TCP: 228 if (dump_tcp_header(m, skb, ih->protocol, 229 ntohs(ih->frag_off) & IP_OFFSET, 230 iphoff+ih->ihl*4, logflags)) 231 return; 232 break; 233 case IPPROTO_UDP: 234 case IPPROTO_UDPLITE: 235 if (dump_udp_header(m, skb, ih->protocol, 236 ntohs(ih->frag_off) & IP_OFFSET, 237 iphoff+ih->ihl*4)) 238 return; 239 break; 240 case IPPROTO_ICMP: { 241 struct icmphdr _icmph; 242 const struct icmphdr *ich; 243 static const size_t required_len[NR_ICMP_TYPES+1] 244 = { [ICMP_ECHOREPLY] = 4, 245 [ICMP_DEST_UNREACH] 246 = 8 + sizeof(struct iphdr), 247 [ICMP_SOURCE_QUENCH] 248 = 8 + sizeof(struct iphdr), 249 [ICMP_REDIRECT] 250 = 8 + sizeof(struct iphdr), 251 [ICMP_ECHO] = 4, 252 [ICMP_TIME_EXCEEDED] 253 = 8 + sizeof(struct iphdr), 254 [ICMP_PARAMETERPROB] 255 = 8 + sizeof(struct iphdr), 256 [ICMP_TIMESTAMP] = 20, 257 [ICMP_TIMESTAMPREPLY] = 20, 258 [ICMP_ADDRESS] = 12, 259 [ICMP_ADDRESSREPLY] = 12 }; 260 261 /* Max length: 11 "PROTO=ICMP " */ 262 sb_add(m, "PROTO=ICMP "); 263 264 if (ntohs(ih->frag_off) & IP_OFFSET) 265 break; 266 267 /* Max length: 25 "INCOMPLETE [65535 bytes] " */ 268 ich = skb_header_pointer(skb, iphoff + ih->ihl * 4, 269 sizeof(_icmph), &_icmph); 270 if (ich == NULL) { 271 sb_add(m, "INCOMPLETE [%u bytes] ", 272 skb->len - iphoff - ih->ihl*4); 273 break; 274 } 275 276 /* Max length: 18 "TYPE=255 CODE=255 " */ 277 sb_add(m, "TYPE=%u CODE=%u ", ich->type, ich->code); 278 279 /* Max length: 25 "INCOMPLETE [65535 bytes] " */ 280 if (ich->type <= NR_ICMP_TYPES && 281 required_len[ich->type] && 282 skb->len-iphoff-ih->ihl*4 < required_len[ich->type]) { 283 sb_add(m, "INCOMPLETE [%u bytes] ", 284 skb->len - iphoff - ih->ihl*4); 285 break; 286 } 287 288 switch (ich->type) { 289 case ICMP_ECHOREPLY: 290 case ICMP_ECHO: 291 /* Max length: 19 "ID=65535 SEQ=65535 " */ 292 sb_add(m, "ID=%u SEQ=%u ", 293 ntohs(ich->un.echo.id), 294 ntohs(ich->un.echo.sequence)); 295 break; 296 297 case ICMP_PARAMETERPROB: 298 /* Max length: 14 "PARAMETER=255 " */ 299 sb_add(m, "PARAMETER=%u ", 300 ntohl(ich->un.gateway) >> 24); 301 break; 302 case ICMP_REDIRECT: 303 /* Max length: 24 "GATEWAY=255.255.255.255 " */ 304 sb_add(m, "GATEWAY=%pI4 ", &ich->un.gateway); 305 /* Fall through */ 306 case ICMP_DEST_UNREACH: 307 case ICMP_SOURCE_QUENCH: 308 case ICMP_TIME_EXCEEDED: 309 /* Max length: 3+maxlen */ 310 if (!iphoff) { /* Only recurse once. */ 311 sb_add(m, "["); 312 dump_ipv4_packet(m, info, skb, 313 iphoff + ih->ihl*4+sizeof(_icmph)); 314 sb_add(m, "] "); 315 } 316 317 /* Max length: 10 "MTU=65535 " */ 318 if (ich->type == ICMP_DEST_UNREACH && 319 ich->code == ICMP_FRAG_NEEDED) 320 sb_add(m, "MTU=%u ", ntohs(ich->un.frag.mtu)); 321 } 322 break; 323 } 324 /* Max Length */ 325 case IPPROTO_AH: { 326 struct ip_auth_hdr _ahdr; 327 const struct ip_auth_hdr *ah; 328 329 if (ntohs(ih->frag_off) & IP_OFFSET) 330 break; 331 332 /* Max length: 9 "PROTO=AH " */ 333 sb_add(m, "PROTO=AH "); 334 335 /* Max length: 25 "INCOMPLETE [65535 bytes] " */ 336 ah = skb_header_pointer(skb, iphoff+ih->ihl*4, 337 sizeof(_ahdr), &_ahdr); 338 if (ah == NULL) { 339 sb_add(m, "INCOMPLETE [%u bytes] ", 340 skb->len - iphoff - ih->ihl*4); 341 break; 342 } 343 344 /* Length: 15 "SPI=0xF1234567 " */ 345 sb_add(m, "SPI=0x%x ", ntohl(ah->spi)); 346 break; 347 } 348 case IPPROTO_ESP: { 349 struct ip_esp_hdr _esph; 350 const struct ip_esp_hdr *eh; 351 352 /* Max length: 10 "PROTO=ESP " */ 353 sb_add(m, "PROTO=ESP "); 354 355 if (ntohs(ih->frag_off) & IP_OFFSET) 356 break; 357 358 /* Max length: 25 "INCOMPLETE [65535 bytes] " */ 359 eh = skb_header_pointer(skb, iphoff+ih->ihl*4, 360 sizeof(_esph), &_esph); 361 if (eh == NULL) { 362 sb_add(m, "INCOMPLETE [%u bytes] ", 363 skb->len - iphoff - ih->ihl*4); 364 break; 365 } 366 367 /* Length: 15 "SPI=0xF1234567 " */ 368 sb_add(m, "SPI=0x%x ", ntohl(eh->spi)); 369 break; 370 } 371 /* Max length: 10 "PROTO 255 " */ 372 default: 373 sb_add(m, "PROTO=%u ", ih->protocol); 374 } 375 376 /* Max length: 15 "UID=4294967295 " */ 377 if ((logflags & XT_LOG_UID) && !iphoff) 378 dump_sk_uid_gid(m, skb->sk); 379 380 /* Max length: 16 "MARK=0xFFFFFFFF " */ 381 if (!iphoff && skb->mark) 382 sb_add(m, "MARK=0x%x ", skb->mark); 383 384 /* Proto Max log string length */ 385 /* IP: 40+46+6+11+127 = 230 */ 386 /* TCP: 10+max(25,20+30+13+9+32+11+127) = 252 */ 387 /* UDP: 10+max(25,20) = 35 */ 388 /* UDPLITE: 14+max(25,20) = 39 */ 389 /* ICMP: 11+max(25, 18+25+max(19,14,24+3+n+10,3+n+10)) = 91+n */ 390 /* ESP: 10+max(25)+15 = 50 */ 391 /* AH: 9+max(25)+15 = 49 */ 392 /* unknown: 10 */ 393 394 /* (ICMP allows recursion one level deep) */ 395 /* maxlen = IP + ICMP + IP + max(TCP,UDP,ICMP,unknown) */ 396 /* maxlen = 230+ 91 + 230 + 252 = 803 */ 397} 398 399static void dump_ipv4_mac_header(struct sbuff *m, 400 const struct nf_loginfo *info, 401 const struct sk_buff *skb) 402{ 403 struct net_device *dev = skb->dev; 404 unsigned int logflags = 0; 405 406 if (info->type == NF_LOG_TYPE_LOG) 407 logflags = info->u.log.logflags; 408 409 if (!(logflags & XT_LOG_MACDECODE)) 410 goto fallback; 411 412 switch (dev->type) { 413 case ARPHRD_ETHER: 414 sb_add(m, "MACSRC=%pM MACDST=%pM MACPROTO=%04x ", 415 eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest, 416 ntohs(eth_hdr(skb)->h_proto)); 417 return; 418 default: 419 break; 420 } 421 422fallback: 423 sb_add(m, "MAC="); 424 if (dev->hard_header_len && 425 skb->mac_header != skb->network_header) { 426 const unsigned char *p = skb_mac_header(skb); 427 unsigned int i; 428 429 sb_add(m, "%02x", *p++); 430 for (i = 1; i < dev->hard_header_len; i++, p++) 431 sb_add(m, ":%02x", *p); 432 } 433 sb_add(m, " "); 434} 435 436static void 437log_packet_common(struct sbuff *m, 438 u_int8_t pf, 439 unsigned int hooknum, 440 const struct sk_buff *skb, 441 const struct net_device *in, 442 const struct net_device *out, 443 const struct nf_loginfo *loginfo, 444 const char *prefix) 445{ 446 sb_add(m, KERN_SOH "%c%sIN=%s OUT=%s ", 447 '0' + loginfo->u.log.level, prefix, 448 in ? in->name : "", 449 out ? out->name : ""); 450#ifdef CONFIG_BRIDGE_NETFILTER 451 if (skb->nf_bridge) { 452 const struct net_device *physindev; 453 const struct net_device *physoutdev; 454 455 physindev = skb->nf_bridge->physindev; 456 if (physindev && in != physindev) 457 sb_add(m, "PHYSIN=%s ", physindev->name); 458 physoutdev = skb->nf_bridge->physoutdev; 459 if (physoutdev && out != physoutdev) 460 sb_add(m, "PHYSOUT=%s ", physoutdev->name); 461 } 462#endif 463} 464 465 466static void 467ipt_log_packet(u_int8_t pf, 468 unsigned int hooknum, 469 const struct sk_buff *skb, 470 const struct net_device *in, 471 const struct net_device *out, 472 const struct nf_loginfo *loginfo, 473 const char *prefix) 474{ 475 struct sbuff *m = sb_open(); 476 477 if (!loginfo) 478 loginfo = &default_loginfo; 479 480 log_packet_common(m, pf, hooknum, skb, in, out, loginfo, prefix); 481 482 if (in != NULL) 483 dump_ipv4_mac_header(m, loginfo, skb); 484 485 dump_ipv4_packet(m, loginfo, skb, 0); 486 487 sb_close(m); 488} 489 490#if IS_ENABLED(CONFIG_IP6_NF_IPTABLES) 491/* One level of recursion won't kill us */ 492static void dump_ipv6_packet(struct sbuff *m, 493 const struct nf_loginfo *info, 494 const struct sk_buff *skb, unsigned int ip6hoff, 495 int recurse) 496{ 497 u_int8_t currenthdr; 498 int fragment; 499 struct ipv6hdr _ip6h; 500 const struct ipv6hdr *ih; 501 unsigned int ptr; 502 unsigned int hdrlen = 0; 503 unsigned int logflags; 504 505 if (info->type == NF_LOG_TYPE_LOG) 506 logflags = info->u.log.logflags; 507 else 508 logflags = NF_LOG_MASK; 509 510 ih = skb_header_pointer(skb, ip6hoff, sizeof(_ip6h), &_ip6h); 511 if (ih == NULL) { 512 sb_add(m, "TRUNCATED"); 513 return; 514 } 515 516 /* Max length: 88 "SRC=0000.0000.0000.0000.0000.0000.0000.0000 DST=0000.0000.0000.0000.0000.0000.0000.0000 " */ 517 sb_add(m, "SRC=%pI6 DST=%pI6 ", &ih->saddr, &ih->daddr); 518 519 /* Max length: 44 "LEN=65535 TC=255 HOPLIMIT=255 FLOWLBL=FFFFF " */ 520 sb_add(m, "LEN=%Zu TC=%u HOPLIMIT=%u FLOWLBL=%u ", 521 ntohs(ih->payload_len) + sizeof(struct ipv6hdr), 522 (ntohl(*(__be32 *)ih) & 0x0ff00000) >> 20, 523 ih->hop_limit, 524 (ntohl(*(__be32 *)ih) & 0x000fffff)); 525 526 fragment = 0; 527 ptr = ip6hoff + sizeof(struct ipv6hdr); 528 currenthdr = ih->nexthdr; 529 while (currenthdr != NEXTHDR_NONE && ip6t_ext_hdr(currenthdr)) { 530 struct ipv6_opt_hdr _hdr; 531 const struct ipv6_opt_hdr *hp; 532 533 hp = skb_header_pointer(skb, ptr, sizeof(_hdr), &_hdr); 534 if (hp == NULL) { 535 sb_add(m, "TRUNCATED"); 536 return; 537 } 538 539 /* Max length: 48 "OPT (...) " */ 540 if (logflags & XT_LOG_IPOPT) 541 sb_add(m, "OPT ( "); 542 543 switch (currenthdr) { 544 case IPPROTO_FRAGMENT: { 545 struct frag_hdr _fhdr; 546 const struct frag_hdr *fh; 547 548 sb_add(m, "FRAG:"); 549 fh = skb_header_pointer(skb, ptr, sizeof(_fhdr), 550 &_fhdr); 551 if (fh == NULL) { 552 sb_add(m, "TRUNCATED "); 553 return; 554 } 555 556 /* Max length: 6 "65535 " */ 557 sb_add(m, "%u ", ntohs(fh->frag_off) & 0xFFF8); 558 559 /* Max length: 11 "INCOMPLETE " */ 560 if (fh->frag_off & htons(0x0001)) 561 sb_add(m, "INCOMPLETE "); 562 563 sb_add(m, "ID:%08x ", ntohl(fh->identification)); 564 565 if (ntohs(fh->frag_off) & 0xFFF8) 566 fragment = 1; 567 568 hdrlen = 8; 569 570 break; 571 } 572 case IPPROTO_DSTOPTS: 573 case IPPROTO_ROUTING: 574 case IPPROTO_HOPOPTS: 575 if (fragment) { 576 if (logflags & XT_LOG_IPOPT) 577 sb_add(m, ")"); 578 return; 579 } 580 hdrlen = ipv6_optlen(hp); 581 break; 582 /* Max Length */ 583 case IPPROTO_AH: 584 if (logflags & XT_LOG_IPOPT) { 585 struct ip_auth_hdr _ahdr; 586 const struct ip_auth_hdr *ah; 587 588 /* Max length: 3 "AH " */ 589 sb_add(m, "AH "); 590 591 if (fragment) { 592 sb_add(m, ")"); 593 return; 594 } 595 596 ah = skb_header_pointer(skb, ptr, sizeof(_ahdr), 597 &_ahdr); 598 if (ah == NULL) { 599 /* 600 * Max length: 26 "INCOMPLETE [65535 601 * bytes] )" 602 */ 603 sb_add(m, "INCOMPLETE [%u bytes] )", 604 skb->len - ptr); 605 return; 606 } 607 608 /* Length: 15 "SPI=0xF1234567 */ 609 sb_add(m, "SPI=0x%x ", ntohl(ah->spi)); 610 611 } 612 613 hdrlen = (hp->hdrlen+2)<<2; 614 break; 615 case IPPROTO_ESP: 616 if (logflags & XT_LOG_IPOPT) { 617 struct ip_esp_hdr _esph; 618 const struct ip_esp_hdr *eh; 619 620 /* Max length: 4 "ESP " */ 621 sb_add(m, "ESP "); 622 623 if (fragment) { 624 sb_add(m, ")"); 625 return; 626 } 627 628 /* 629 * Max length: 26 "INCOMPLETE [65535 bytes] )" 630 */ 631 eh = skb_header_pointer(skb, ptr, sizeof(_esph), 632 &_esph); 633 if (eh == NULL) { 634 sb_add(m, "INCOMPLETE [%u bytes] )", 635 skb->len - ptr); 636 return; 637 } 638 639 /* Length: 16 "SPI=0xF1234567 )" */ 640 sb_add(m, "SPI=0x%x )", ntohl(eh->spi)); 641 642 } 643 return; 644 default: 645 /* Max length: 20 "Unknown Ext Hdr 255" */ 646 sb_add(m, "Unknown Ext Hdr %u", currenthdr); 647 return; 648 } 649 if (logflags & XT_LOG_IPOPT) 650 sb_add(m, ") "); 651 652 currenthdr = hp->nexthdr; 653 ptr += hdrlen; 654 } 655 656 switch (currenthdr) { 657 case IPPROTO_TCP: 658 if (dump_tcp_header(m, skb, currenthdr, fragment, ptr, 659 logflags)) 660 return; 661 break; 662 case IPPROTO_UDP: 663 case IPPROTO_UDPLITE: 664 if (dump_udp_header(m, skb, currenthdr, fragment, ptr)) 665 return; 666 break; 667 case IPPROTO_ICMPV6: { 668 struct icmp6hdr _icmp6h; 669 const struct icmp6hdr *ic; 670 671 /* Max length: 13 "PROTO=ICMPv6 " */ 672 sb_add(m, "PROTO=ICMPv6 "); 673 674 if (fragment) 675 break; 676 677 /* Max length: 25 "INCOMPLETE [65535 bytes] " */ 678 ic = skb_header_pointer(skb, ptr, sizeof(_icmp6h), &_icmp6h); 679 if (ic == NULL) { 680 sb_add(m, "INCOMPLETE [%u bytes] ", skb->len - ptr); 681 return; 682 } 683 684 /* Max length: 18 "TYPE=255 CODE=255 " */ 685 sb_add(m, "TYPE=%u CODE=%u ", ic->icmp6_type, ic->icmp6_code); 686 687 switch (ic->icmp6_type) { 688 case ICMPV6_ECHO_REQUEST: 689 case ICMPV6_ECHO_REPLY: 690 /* Max length: 19 "ID=65535 SEQ=65535 " */ 691 sb_add(m, "ID=%u SEQ=%u ", 692 ntohs(ic->icmp6_identifier), 693 ntohs(ic->icmp6_sequence)); 694 break; 695 case ICMPV6_MGM_QUERY: 696 case ICMPV6_MGM_REPORT: 697 case ICMPV6_MGM_REDUCTION: 698 break; 699 700 case ICMPV6_PARAMPROB: 701 /* Max length: 17 "POINTER=ffffffff " */ 702 sb_add(m, "POINTER=%08x ", ntohl(ic->icmp6_pointer)); 703 /* Fall through */ 704 case ICMPV6_DEST_UNREACH: 705 case ICMPV6_PKT_TOOBIG: 706 case ICMPV6_TIME_EXCEED: 707 /* Max length: 3+maxlen */ 708 if (recurse) { 709 sb_add(m, "["); 710 dump_ipv6_packet(m, info, skb, 711 ptr + sizeof(_icmp6h), 0); 712 sb_add(m, "] "); 713 } 714 715 /* Max length: 10 "MTU=65535 " */ 716 if (ic->icmp6_type == ICMPV6_PKT_TOOBIG) 717 sb_add(m, "MTU=%u ", ntohl(ic->icmp6_mtu)); 718 } 719 break; 720 } 721 /* Max length: 10 "PROTO=255 " */ 722 default: 723 sb_add(m, "PROTO=%u ", currenthdr); 724 } 725 726 /* Max length: 15 "UID=4294967295 " */ 727 if ((logflags & XT_LOG_UID) && recurse) 728 dump_sk_uid_gid(m, skb->sk); 729 730 /* Max length: 16 "MARK=0xFFFFFFFF " */ 731 if (!recurse && skb->mark) 732 sb_add(m, "MARK=0x%x ", skb->mark); 733} 734 735static void dump_ipv6_mac_header(struct sbuff *m, 736 const struct nf_loginfo *info, 737 const struct sk_buff *skb) 738{ 739 struct net_device *dev = skb->dev; 740 unsigned int logflags = 0; 741 742 if (info->type == NF_LOG_TYPE_LOG) 743 logflags = info->u.log.logflags; 744 745 if (!(logflags & XT_LOG_MACDECODE)) 746 goto fallback; 747 748 switch (dev->type) { 749 case ARPHRD_ETHER: 750 sb_add(m, "MACSRC=%pM MACDST=%pM MACPROTO=%04x ", 751 eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest, 752 ntohs(eth_hdr(skb)->h_proto)); 753 return; 754 default: 755 break; 756 } 757 758fallback: 759 sb_add(m, "MAC="); 760 if (dev->hard_header_len && 761 skb->mac_header != skb->network_header) { 762 const unsigned char *p = skb_mac_header(skb); 763 unsigned int len = dev->hard_header_len; 764 unsigned int i; 765 766 if (dev->type == ARPHRD_SIT) { 767 p -= ETH_HLEN; 768 769 if (p < skb->head) 770 p = NULL; 771 } 772 773 if (p != NULL) { 774 sb_add(m, "%02x", *p++); 775 for (i = 1; i < len; i++) 776 sb_add(m, ":%02x", *p++); 777 } 778 sb_add(m, " "); 779 780 if (dev->type == ARPHRD_SIT) { 781 const struct iphdr *iph = 782 (struct iphdr *)skb_mac_header(skb); 783 sb_add(m, "TUNNEL=%pI4->%pI4 ", &iph->saddr, 784 &iph->daddr); 785 } 786 } else 787 sb_add(m, " "); 788} 789 790static void 791ip6t_log_packet(u_int8_t pf, 792 unsigned int hooknum, 793 const struct sk_buff *skb, 794 const struct net_device *in, 795 const struct net_device *out, 796 const struct nf_loginfo *loginfo, 797 const char *prefix) 798{ 799 struct sbuff *m = sb_open(); 800 801 if (!loginfo) 802 loginfo = &default_loginfo; 803 804 log_packet_common(m, pf, hooknum, skb, in, out, loginfo, prefix); 805 806 if (in != NULL) 807 dump_ipv6_mac_header(m, loginfo, skb); 808 809 dump_ipv6_packet(m, loginfo, skb, skb_network_offset(skb), 1); 810 811 sb_close(m); 812} 813#endif 814 815static unsigned int 816log_tg(struct sk_buff *skb, const struct xt_action_param *par) 817{ 818 const struct xt_log_info *loginfo = par->targinfo; 819 struct nf_loginfo li; 820 821 li.type = NF_LOG_TYPE_LOG; 822 li.u.log.level = loginfo->level; 823 li.u.log.logflags = loginfo->logflags; 824 825 if (par->family == NFPROTO_IPV4) 826 ipt_log_packet(NFPROTO_IPV4, par->hooknum, skb, par->in, 827 par->out, &li, loginfo->prefix); 828#if IS_ENABLED(CONFIG_IP6_NF_IPTABLES) 829 else if (par->family == NFPROTO_IPV6) 830 ip6t_log_packet(NFPROTO_IPV6, par->hooknum, skb, par->in, 831 par->out, &li, loginfo->prefix); 832#endif 833 else 834 WARN_ON_ONCE(1); 835 836 return XT_CONTINUE; 837} 838 839static int log_tg_check(const struct xt_tgchk_param *par) 840{ 841 const struct xt_log_info *loginfo = par->targinfo; 842 843 if (par->family != NFPROTO_IPV4 && par->family != NFPROTO_IPV6) 844 return -EINVAL; 845 846 if (loginfo->level >= 8) { 847 pr_debug("level %u >= 8\n", loginfo->level); 848 return -EINVAL; 849 } 850 851 if (loginfo->prefix[sizeof(loginfo->prefix)-1] != '\0') { 852 pr_debug("prefix is not null-terminated\n"); 853 return -EINVAL; 854 } 855 856 return 0; 857} 858 859static struct xt_target log_tg_regs[] __read_mostly = { 860 { 861 .name = "LOG", 862 .family = NFPROTO_IPV4, 863 .target = log_tg, 864 .targetsize = sizeof(struct xt_log_info), 865 .checkentry = log_tg_check, 866 .me = THIS_MODULE, 867 }, 868#if IS_ENABLED(CONFIG_IP6_NF_IPTABLES) 869 { 870 .name = "LOG", 871 .family = NFPROTO_IPV6, 872 .target = log_tg, 873 .targetsize = sizeof(struct xt_log_info), 874 .checkentry = log_tg_check, 875 .me = THIS_MODULE, 876 }, 877#endif 878}; 879 880static struct nf_logger ipt_log_logger __read_mostly = { 881 .name = "ipt_LOG", 882 .logfn = &ipt_log_packet, 883 .me = THIS_MODULE, 884}; 885 886#if IS_ENABLED(CONFIG_IP6_NF_IPTABLES) 887static struct nf_logger ip6t_log_logger __read_mostly = { 888 .name = "ip6t_LOG", 889 .logfn = &ip6t_log_packet, 890 .me = THIS_MODULE, 891}; 892#endif 893 894static int __init log_tg_init(void) 895{ 896 int ret; 897 898 ret = xt_register_targets(log_tg_regs, ARRAY_SIZE(log_tg_regs)); 899 if (ret < 0) 900 return ret; 901 902 nf_log_register(NFPROTO_IPV4, &ipt_log_logger); 903#if IS_ENABLED(CONFIG_IP6_NF_IPTABLES) 904 nf_log_register(NFPROTO_IPV6, &ip6t_log_logger); 905#endif 906 return 0; 907} 908 909static void __exit log_tg_exit(void) 910{ 911 nf_log_unregister(&ipt_log_logger); 912#if IS_ENABLED(CONFIG_IP6_NF_IPTABLES) 913 nf_log_unregister(&ip6t_log_logger); 914#endif 915 xt_unregister_targets(log_tg_regs, ARRAY_SIZE(log_tg_regs)); 916} 917 918module_init(log_tg_init); 919module_exit(log_tg_exit); 920 921MODULE_LICENSE("GPL"); 922MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>"); 923MODULE_AUTHOR("Jan Rekorajski <baggins@pld.org.pl>"); 924MODULE_DESCRIPTION("Xtables: IPv4/IPv6 packet logging"); 925MODULE_ALIAS("ipt_LOG"); 926MODULE_ALIAS("ip6t_LOG");