1 /*- 2 * Copyright (c) 2002-2009 Luigi Rizzo, Universita` di Pisa 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23 * SUCH DAMAGE. 24 */ 25 26 #include <sys/cdefs.h> 27 __FBSDID("$FreeBSD$"); 28 29 /* 30 * Logging support for ipfw 31 */ 32 33 #include "opt_ipfw.h" 34 #include "opt_inet.h" 35 #ifndef INET 36 #error IPFIREWALL requires INET. 37 #endif /* INET */ 38 #include "opt_inet6.h" 39 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/mbuf.h> 43 #include <sys/kernel.h> 44 #include <sys/socket.h> 45 #include <sys/sysctl.h> 46 #include <sys/syslog.h> 47 #include <sys/lock.h> 48 #include <sys/rwlock.h> 49 #include <net/ethernet.h> /* for ETHERTYPE_IP */ 50 #include <net/if.h> 51 #include <net/if_var.h> 52 #include <net/if_clone.h> 53 #include <net/vnet.h> 54 #include <net/if_types.h> /* for IFT_PFLOG */ 55 #include <net/bpf.h> /* for BPF */ 56 57 #include <netinet/in.h> 58 #include <netinet/ip.h> 59 #include <netinet/ip_icmp.h> 60 #include <netinet/ip_var.h> 61 #include <netinet/ip_fw.h> 62 #include <netinet/tcp_var.h> 63 #include <netinet/udp.h> 64 65 #include <netinet/ip6.h> 66 #include <netinet/icmp6.h> 67 #ifdef INET6 68 #include <netinet6/in6_var.h> /* ip6_sprintf() */ 69 #endif 70 71 #include <netpfil/ipfw/ip_fw_private.h> 72 73 #ifdef MAC 74 #include <security/mac/mac_framework.h> 75 #endif 76 77 /* 78 * L3HDR maps an ipv4 pointer into a layer3 header pointer of type T 79 * Other macros just cast void * into the appropriate type 80 */ 81 #define L3HDR(T, ip) ((T *)((u_int32_t *)(ip) + (ip)->ip_hl)) 82 #define TCP(p) ((struct tcphdr *)(p)) 83 #define SCTP(p) ((struct sctphdr *)(p)) 84 #define UDP(p) ((struct udphdr *)(p)) 85 #define ICMP(p) ((struct icmphdr *)(p)) 86 #define ICMP6(p) ((struct icmp6_hdr *)(p)) 87 88 #ifdef __APPLE__ 89 #undef snprintf 90 #define snprintf sprintf 91 #define SNPARGS(buf, len) buf + len 92 #define SNP(buf) buf 93 #else /* !__APPLE__ */ 94 #define SNPARGS(buf, len) buf + len, sizeof(buf) > len ? sizeof(buf) - len : 0 95 #define SNP(buf) buf, sizeof(buf) 96 #endif /* !__APPLE__ */ 97 98 #ifdef WITHOUT_BPF 99 void 100 ipfw_log_bpf(int onoff) 101 { 102 } 103 #else /* !WITHOUT_BPF */ 104 static struct ifnet *log_if; /* hook to attach to bpf */ 105 static struct rwlock log_if_lock; 106 #define LOGIF_LOCK_INIT(x) rw_init(&log_if_lock, "ipfw log_if lock") 107 #define LOGIF_LOCK_DESTROY(x) rw_destroy(&log_if_lock) 108 #define LOGIF_RLOCK(x) rw_rlock(&log_if_lock) 109 #define LOGIF_RUNLOCK(x) rw_runlock(&log_if_lock) 110 #define LOGIF_WLOCK(x) rw_wlock(&log_if_lock) 111 #define LOGIF_WUNLOCK(x) rw_wunlock(&log_if_lock) 112 113 static const char ipfwname[] = "ipfw"; 114 115 /* we use this dummy function for all ifnet callbacks */ 116 static int 117 log_dummy(struct ifnet *ifp, u_long cmd, caddr_t addr) 118 { 119 return EINVAL; 120 } 121 122 static int 123 ipfw_log_output(struct ifnet *ifp, struct mbuf *m, 124 const struct sockaddr *dst, struct route *ro) 125 { 126 if (m != NULL) 127 FREE_PKT(m); 128 return EINVAL; 129 } 130 131 static void 132 ipfw_log_start(struct ifnet* ifp) 133 { 134 panic("ipfw_log_start() must not be called"); 135 } 136 137 static const u_char ipfwbroadcastaddr[6] = 138 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; 139 140 static int 141 ipfw_log_clone_match(struct if_clone *ifc, const char *name) 142 { 143 144 return (strncmp(name, ipfwname, sizeof(ipfwname) - 1) == 0); 145 } 146 147 static int 148 ipfw_log_clone_create(struct if_clone *ifc, char *name, size_t len, 149 caddr_t params) 150 { 151 int error; 152 int unit; 153 struct ifnet *ifp; 154 155 error = ifc_name2unit(name, &unit); 156 if (error) 157 return (error); 158 159 error = ifc_alloc_unit(ifc, &unit); 160 if (error) 161 return (error); 162 163 ifp = if_alloc(IFT_PFLOG); 164 if (ifp == NULL) { 165 ifc_free_unit(ifc, unit); 166 return (ENOSPC); 167 } 168 ifp->if_dname = ipfwname; 169 ifp->if_dunit = unit; 170 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", ipfwname, unit); 171 strlcpy(name, ifp->if_xname, len); 172 ifp->if_mtu = 65536; 173 ifp->if_flags = IFF_UP | IFF_SIMPLEX | IFF_MULTICAST; 174 ifp->if_init = (void *)log_dummy; 175 ifp->if_ioctl = log_dummy; 176 ifp->if_start = ipfw_log_start; 177 ifp->if_output = ipfw_log_output; 178 ifp->if_addrlen = 6; 179 ifp->if_hdrlen = 14; 180 ifp->if_broadcastaddr = ipfwbroadcastaddr; 181 ifp->if_baudrate = IF_Mbps(10); 182 183 LOGIF_WLOCK(); 184 if (log_if == NULL) 185 log_if = ifp; 186 else { 187 LOGIF_WUNLOCK(); 188 if_free(ifp); 189 ifc_free_unit(ifc, unit); 190 return (EEXIST); 191 } 192 LOGIF_WUNLOCK(); 193 if_attach(ifp); 194 bpfattach(ifp, DLT_EN10MB, 14); 195 196 return (0); 197 } 198 199 static int 200 ipfw_log_clone_destroy(struct if_clone *ifc, struct ifnet *ifp) 201 { 202 int unit; 203 204 if (ifp == NULL) 205 return (0); 206 207 LOGIF_WLOCK(); 208 if (log_if != NULL && ifp == log_if) 209 log_if = NULL; 210 else { 211 LOGIF_WUNLOCK(); 212 return (EINVAL); 213 } 214 LOGIF_WUNLOCK(); 215 216 unit = ifp->if_dunit; 217 bpfdetach(ifp); 218 if_detach(ifp); 219 if_free(ifp); 220 ifc_free_unit(ifc, unit); 221 222 return (0); 223 } 224 225 static struct if_clone *ipfw_log_cloner; 226 227 void 228 ipfw_log_bpf(int onoff) 229 { 230 231 if (onoff) { 232 LOGIF_LOCK_INIT(); 233 ipfw_log_cloner = if_clone_advanced(ipfwname, 0, 234 ipfw_log_clone_match, ipfw_log_clone_create, 235 ipfw_log_clone_destroy); 236 } else { 237 if_clone_detach(ipfw_log_cloner); 238 LOGIF_LOCK_DESTROY(); 239 } 240 } 241 #endif /* !WITHOUT_BPF */ 242 243 /* 244 * We enter here when we have a rule with O_LOG. 245 * XXX this function alone takes about 2Kbytes of code! 246 */ 247 void 248 ipfw_log(struct ip_fw *f, u_int hlen, struct ip_fw_args *args, 249 struct mbuf *m, struct ifnet *oif, u_short offset, uint32_t tablearg, 250 struct ip *ip) 251 { 252 char *action; 253 int limit_reached = 0; 254 char action2[92], proto[128], fragment[32]; 255 256 if (V_fw_verbose == 0) { 257 #ifndef WITHOUT_BPF 258 LOGIF_RLOCK(); 259 if (log_if == NULL || log_if->if_bpf == NULL) { 260 LOGIF_RUNLOCK(); 261 return; 262 } 263 264 if (args->eh) /* layer2, use orig hdr */ 265 BPF_MTAP2(log_if, args->eh, ETHER_HDR_LEN, m); 266 else { 267 /* Add fake header. Later we will store 268 * more info in the header. 269 */ 270 if (ip->ip_v == 4) 271 BPF_MTAP2(log_if, "DDDDDDSSSSSS\x08\x00", ETHER_HDR_LEN, m); 272 else if (ip->ip_v == 6) 273 BPF_MTAP2(log_if, "DDDDDDSSSSSS\x86\xdd", ETHER_HDR_LEN, m); 274 else 275 /* Obviously bogus EtherType. */ 276 BPF_MTAP2(log_if, "DDDDDDSSSSSS\xff\xff", ETHER_HDR_LEN, m); 277 } 278 LOGIF_RUNLOCK(); 279 #endif /* !WITHOUT_BPF */ 280 return; 281 } 282 /* the old 'log' function */ 283 fragment[0] = '\0'; 284 proto[0] = '\0'; 285 286 if (f == NULL) { /* bogus pkt */ 287 if (V_verbose_limit != 0 && V_norule_counter >= V_verbose_limit) 288 return; 289 V_norule_counter++; 290 if (V_norule_counter == V_verbose_limit) 291 limit_reached = V_verbose_limit; 292 action = "Refuse"; 293 } else { /* O_LOG is the first action, find the real one */ 294 ipfw_insn *cmd = ACTION_PTR(f); 295 ipfw_insn_log *l = (ipfw_insn_log *)cmd; 296 297 if (l->max_log != 0 && l->log_left == 0) 298 return; 299 l->log_left--; 300 if (l->log_left == 0) 301 limit_reached = l->max_log; 302 cmd += F_LEN(cmd); /* point to first action */ 303 if (cmd->opcode == O_ALTQ) { 304 ipfw_insn_altq *altq = (ipfw_insn_altq *)cmd; 305 306 snprintf(SNPARGS(action2, 0), "Altq %d", 307 altq->qid); 308 cmd += F_LEN(cmd); 309 } 310 if (cmd->opcode == O_PROB || cmd->opcode == O_TAG || 311 cmd->opcode == O_SETDSCP) 312 cmd += F_LEN(cmd); 313 314 action = action2; 315 switch (cmd->opcode) { 316 case O_DENY: 317 action = "Deny"; 318 break; 319 320 case O_REJECT: 321 if (cmd->arg1==ICMP_REJECT_RST) 322 action = "Reset"; 323 else if (cmd->arg1==ICMP_UNREACH_HOST) 324 action = "Reject"; 325 else 326 snprintf(SNPARGS(action2, 0), "Unreach %d", 327 cmd->arg1); 328 break; 329 330 case O_UNREACH6: 331 if (cmd->arg1==ICMP6_UNREACH_RST) 332 action = "Reset"; 333 else 334 snprintf(SNPARGS(action2, 0), "Unreach %d", 335 cmd->arg1); 336 break; 337 338 case O_ACCEPT: 339 action = "Accept"; 340 break; 341 case O_COUNT: 342 action = "Count"; 343 break; 344 case O_DIVERT: 345 snprintf(SNPARGS(action2, 0), "Divert %d", 346 cmd->arg1); 347 break; 348 case O_TEE: 349 snprintf(SNPARGS(action2, 0), "Tee %d", 350 cmd->arg1); 351 break; 352 case O_SETFIB: 353 snprintf(SNPARGS(action2, 0), "SetFib %d", 354 IP_FW_ARG_TABLEARG(cmd->arg1)); 355 break; 356 case O_SKIPTO: 357 snprintf(SNPARGS(action2, 0), "SkipTo %d", 358 IP_FW_ARG_TABLEARG(cmd->arg1)); 359 break; 360 case O_PIPE: 361 snprintf(SNPARGS(action2, 0), "Pipe %d", 362 IP_FW_ARG_TABLEARG(cmd->arg1)); 363 break; 364 case O_QUEUE: 365 snprintf(SNPARGS(action2, 0), "Queue %d", 366 IP_FW_ARG_TABLEARG(cmd->arg1)); 367 break; 368 case O_FORWARD_IP: { 369 ipfw_insn_sa *sa = (ipfw_insn_sa *)cmd; 370 int len; 371 struct in_addr dummyaddr; 372 if (sa->sa.sin_addr.s_addr == INADDR_ANY) 373 dummyaddr.s_addr = htonl(tablearg); 374 else 375 dummyaddr.s_addr = sa->sa.sin_addr.s_addr; 376 377 len = snprintf(SNPARGS(action2, 0), "Forward to %s", 378 inet_ntoa(dummyaddr)); 379 380 if (sa->sa.sin_port) 381 snprintf(SNPARGS(action2, len), ":%d", 382 sa->sa.sin_port); 383 } 384 break; 385 #ifdef INET6 386 case O_FORWARD_IP6: { 387 char buf[INET6_ADDRSTRLEN]; 388 ipfw_insn_sa6 *sa = (ipfw_insn_sa6 *)cmd; 389 int len; 390 391 len = snprintf(SNPARGS(action2, 0), "Forward to [%s]", 392 ip6_sprintf(buf, &sa->sa.sin6_addr)); 393 394 if (sa->sa.sin6_port) 395 snprintf(SNPARGS(action2, len), ":%u", 396 sa->sa.sin6_port); 397 } 398 break; 399 #endif 400 case O_NETGRAPH: 401 snprintf(SNPARGS(action2, 0), "Netgraph %d", 402 cmd->arg1); 403 break; 404 case O_NGTEE: 405 snprintf(SNPARGS(action2, 0), "Ngtee %d", 406 cmd->arg1); 407 break; 408 case O_NAT: 409 action = "Nat"; 410 break; 411 case O_REASS: 412 action = "Reass"; 413 break; 414 case O_CALLRETURN: 415 if (cmd->len & F_NOT) 416 action = "Return"; 417 else 418 snprintf(SNPARGS(action2, 0), "Call %d", 419 cmd->arg1); 420 break; 421 default: 422 action = "UNKNOWN"; 423 break; 424 } 425 } 426 427 if (hlen == 0) { /* non-ip */ 428 snprintf(SNPARGS(proto, 0), "MAC"); 429 430 } else { 431 int len; 432 #ifdef INET6 433 char src[INET6_ADDRSTRLEN + 2], dst[INET6_ADDRSTRLEN + 2]; 434 #else 435 char src[INET_ADDRSTRLEN], dst[INET_ADDRSTRLEN]; 436 #endif 437 struct icmphdr *icmp; 438 struct tcphdr *tcp; 439 struct udphdr *udp; 440 #ifdef INET6 441 struct ip6_hdr *ip6 = NULL; 442 struct icmp6_hdr *icmp6; 443 u_short ip6f_mf; 444 #endif 445 src[0] = '\0'; 446 dst[0] = '\0'; 447 #ifdef INET6 448 ip6f_mf = offset & IP6F_MORE_FRAG; 449 offset &= IP6F_OFF_MASK; 450 451 if (IS_IP6_FLOW_ID(&(args->f_id))) { 452 char ip6buf[INET6_ADDRSTRLEN]; 453 snprintf(src, sizeof(src), "[%s]", 454 ip6_sprintf(ip6buf, &args->f_id.src_ip6)); 455 snprintf(dst, sizeof(dst), "[%s]", 456 ip6_sprintf(ip6buf, &args->f_id.dst_ip6)); 457 458 ip6 = (struct ip6_hdr *)ip; 459 tcp = (struct tcphdr *)(((char *)ip) + hlen); 460 udp = (struct udphdr *)(((char *)ip) + hlen); 461 } else 462 #endif 463 { 464 tcp = L3HDR(struct tcphdr, ip); 465 udp = L3HDR(struct udphdr, ip); 466 467 inet_ntop(AF_INET, &ip->ip_src, src, sizeof(src)); 468 inet_ntop(AF_INET, &ip->ip_dst, dst, sizeof(dst)); 469 } 470 471 switch (args->f_id.proto) { 472 case IPPROTO_TCP: 473 len = snprintf(SNPARGS(proto, 0), "TCP %s", src); 474 if (offset == 0) 475 snprintf(SNPARGS(proto, len), ":%d %s:%d", 476 ntohs(tcp->th_sport), 477 dst, 478 ntohs(tcp->th_dport)); 479 else 480 snprintf(SNPARGS(proto, len), " %s", dst); 481 break; 482 483 case IPPROTO_UDP: 484 len = snprintf(SNPARGS(proto, 0), "UDP %s", src); 485 if (offset == 0) 486 snprintf(SNPARGS(proto, len), ":%d %s:%d", 487 ntohs(udp->uh_sport), 488 dst, 489 ntohs(udp->uh_dport)); 490 else 491 snprintf(SNPARGS(proto, len), " %s", dst); 492 break; 493 494 case IPPROTO_ICMP: 495 icmp = L3HDR(struct icmphdr, ip); 496 if (offset == 0) 497 len = snprintf(SNPARGS(proto, 0), 498 "ICMP:%u.%u ", 499 icmp->icmp_type, icmp->icmp_code); 500 else 501 len = snprintf(SNPARGS(proto, 0), "ICMP "); 502 len += snprintf(SNPARGS(proto, len), "%s", src); 503 snprintf(SNPARGS(proto, len), " %s", dst); 504 break; 505 #ifdef INET6 506 case IPPROTO_ICMPV6: 507 icmp6 = (struct icmp6_hdr *)(((char *)ip) + hlen); 508 if (offset == 0) 509 len = snprintf(SNPARGS(proto, 0), 510 "ICMPv6:%u.%u ", 511 icmp6->icmp6_type, icmp6->icmp6_code); 512 else 513 len = snprintf(SNPARGS(proto, 0), "ICMPv6 "); 514 len += snprintf(SNPARGS(proto, len), "%s", src); 515 snprintf(SNPARGS(proto, len), " %s", dst); 516 break; 517 #endif 518 default: 519 len = snprintf(SNPARGS(proto, 0), "P:%d %s", 520 args->f_id.proto, src); 521 snprintf(SNPARGS(proto, len), " %s", dst); 522 break; 523 } 524 525 #ifdef INET6 526 if (IS_IP6_FLOW_ID(&(args->f_id))) { 527 if (offset & (IP6F_OFF_MASK | IP6F_MORE_FRAG)) 528 snprintf(SNPARGS(fragment, 0), 529 " (frag %08x:%d@%d%s)", 530 args->f_id.extra, 531 ntohs(ip6->ip6_plen) - hlen, 532 ntohs(offset) << 3, ip6f_mf ? "+" : ""); 533 } else 534 #endif 535 { 536 int ipoff, iplen; 537 ipoff = ntohs(ip->ip_off); 538 iplen = ntohs(ip->ip_len); 539 if (ipoff & (IP_MF | IP_OFFMASK)) 540 snprintf(SNPARGS(fragment, 0), 541 " (frag %d:%d@%d%s)", 542 ntohs(ip->ip_id), iplen - (ip->ip_hl << 2), 543 offset << 3, 544 (ipoff & IP_MF) ? "+" : ""); 545 } 546 } 547 #ifdef __FreeBSD__ 548 if (oif || m->m_pkthdr.rcvif) 549 log(LOG_SECURITY | LOG_INFO, 550 "ipfw: %d %s %s %s via %s%s\n", 551 f ? f->rulenum : -1, 552 action, proto, oif ? "out" : "in", 553 oif ? oif->if_xname : m->m_pkthdr.rcvif->if_xname, 554 fragment); 555 else 556 #endif 557 log(LOG_SECURITY | LOG_INFO, 558 "ipfw: %d %s %s [no if info]%s\n", 559 f ? f->rulenum : -1, 560 action, proto, fragment); 561 if (limit_reached) 562 log(LOG_SECURITY | LOG_NOTICE, 563 "ipfw: limit %d reached on entry %d\n", 564 limit_reached, f ? f->rulenum : -1); 565 } 566 /* end of file */ 567