1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2002-2009 Luigi Rizzo, Universita` di Pisa 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 /* 32 * Logging support for ipfw 33 */ 34 35 #include "opt_ipfw.h" 36 #include "opt_inet.h" 37 #ifndef INET 38 #error IPFIREWALL requires INET. 39 #endif /* INET */ 40 #include "opt_inet6.h" 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/kernel.h> 45 #include <sys/mbuf.h> 46 #include <sys/socket.h> 47 #include <sys/sysctl.h> 48 #include <sys/syslog.h> 49 #include <net/ethernet.h> /* for ETHERTYPE_IP */ 50 #include <net/if.h> 51 #include <net/if_var.h> 52 #include <net/if_private.h> 53 #include <net/vnet.h> 54 55 #include <netinet/in.h> 56 #include <netinet/ip.h> 57 #include <netinet/ip_icmp.h> 58 #include <netinet/ip_var.h> 59 #include <netinet/ip_fw.h> 60 #include <netinet/udp.h> 61 #include <netinet/tcp.h> 62 63 #include <netinet/ip6.h> 64 #include <netinet/icmp6.h> 65 #ifdef INET6 66 #include <netinet6/in6_var.h> /* ip6_sprintf() */ 67 #endif 68 69 #include <netpfil/ipfw/ip_fw_private.h> 70 71 #ifdef MAC 72 #include <security/mac/mac_framework.h> 73 #endif 74 75 /* 76 * L3HDR maps an ipv4 pointer into a layer3 header pointer of type T 77 * Other macros just cast void * into the appropriate type 78 */ 79 #define L3HDR(T, ip) ((T *)((u_int32_t *)(ip) + (ip)->ip_hl)) 80 #define TCP(p) ((struct tcphdr *)(p)) 81 #define SCTP(p) ((struct sctphdr *)(p)) 82 #define UDP(p) ((struct udphdr *)(p)) 83 #define ICMP(p) ((struct icmphdr *)(p)) 84 #define ICMP6(p) ((struct icmp6_hdr *)(p)) 85 86 #ifdef __APPLE__ 87 #undef snprintf 88 #define snprintf sprintf 89 #define SNPARGS(buf, len) buf + len 90 #define SNP(buf) buf 91 #else /* !__APPLE__ */ 92 #define SNPARGS(buf, len) buf + len, sizeof(buf) > len ? sizeof(buf) - len : 0 93 #define SNP(buf) buf, sizeof(buf) 94 #endif /* !__APPLE__ */ 95 96 #define TARG(k, f) IP_FW_ARG_TABLEARG(chain, k, f) 97 /* 98 * We enter here when we have a rule with O_LOG. 99 * XXX this function alone takes about 2Kbytes of code! 100 */ 101 void 102 ipfw_log(struct ip_fw_chain *chain, struct ip_fw *f, u_int hlen, 103 struct ip_fw_args *args, u_short offset, uint32_t tablearg, struct ip *ip) 104 { 105 char *action; 106 int limit_reached = 0; 107 char action2[92], proto[128], fragment[32], mark_str[24]; 108 109 if (V_fw_verbose == 0) { 110 if (args->flags & IPFW_ARGS_LENMASK) 111 ipfw_bpf_tap(args->mem, IPFW_ARGS_LENGTH(args->flags)); 112 else if (args->flags & IPFW_ARGS_ETHER) 113 /* layer2, use orig hdr */ 114 ipfw_bpf_mtap(args->m); 115 else { 116 /* Add fake header. Later we will store 117 * more info in the header. 118 */ 119 if (ip->ip_v == 4) 120 ipfw_bpf_mtap2("DDDDDDSSSSSS\x08\x00", 121 ETHER_HDR_LEN, args->m); 122 else if (ip->ip_v == 6) 123 ipfw_bpf_mtap2("DDDDDDSSSSSS\x86\xdd", 124 ETHER_HDR_LEN, args->m); 125 else 126 /* Obviously bogus EtherType. */ 127 ipfw_bpf_mtap2("DDDDDDSSSSSS\xff\xff", 128 ETHER_HDR_LEN, args->m); 129 } 130 return; 131 } 132 /* the old 'log' function */ 133 fragment[0] = '\0'; 134 proto[0] = '\0'; 135 136 if (f == NULL) { /* bogus pkt */ 137 if (V_verbose_limit != 0 && V_norule_counter >= V_verbose_limit) 138 return; 139 V_norule_counter++; 140 if (V_norule_counter == V_verbose_limit) 141 limit_reached = V_verbose_limit; 142 action = "Refuse"; 143 } else { /* O_LOG is the first action, find the real one */ 144 ipfw_insn *cmd = ACTION_PTR(f); 145 ipfw_insn_log *l = (ipfw_insn_log *)cmd; 146 147 if (l->max_log != 0 && l->log_left == 0) 148 return; 149 l->log_left--; 150 if (l->log_left == 0) 151 limit_reached = l->max_log; 152 cmd += F_LEN(cmd); /* point to first action */ 153 if (cmd->opcode == O_ALTQ) { 154 ipfw_insn_altq *altq = (ipfw_insn_altq *)cmd; 155 156 snprintf(SNPARGS(action2, 0), "Altq %d", 157 altq->qid); 158 cmd += F_LEN(cmd); 159 } 160 if (cmd->opcode == O_PROB || cmd->opcode == O_TAG) 161 cmd += F_LEN(cmd); 162 163 action = action2; 164 switch (cmd->opcode) { 165 case O_DENY: 166 action = "Deny"; 167 break; 168 169 case O_REJECT: 170 if (cmd->arg1==ICMP_REJECT_RST) 171 action = "Reset"; 172 else if (cmd->arg1==ICMP_REJECT_ABORT) 173 action = "Abort"; 174 else if (cmd->arg1==ICMP_UNREACH_HOST) 175 action = "Reject"; 176 else 177 snprintf(SNPARGS(action2, 0), "Unreach %d", 178 cmd->arg1); 179 break; 180 181 case O_UNREACH6: 182 if (cmd->arg1==ICMP6_UNREACH_RST) 183 action = "Reset"; 184 else if (cmd->arg1==ICMP6_UNREACH_ABORT) 185 action = "Abort"; 186 else 187 snprintf(SNPARGS(action2, 0), "Unreach %d", 188 cmd->arg1); 189 break; 190 191 case O_ACCEPT: 192 action = "Accept"; 193 break; 194 case O_COUNT: 195 action = "Count"; 196 break; 197 case O_DIVERT: 198 snprintf(SNPARGS(action2, 0), "Divert %d", 199 TARG(cmd->arg1, divert)); 200 break; 201 case O_TEE: 202 snprintf(SNPARGS(action2, 0), "Tee %d", 203 TARG(cmd->arg1, divert)); 204 break; 205 case O_SETDSCP: 206 snprintf(SNPARGS(action2, 0), "SetDscp %d", 207 TARG(cmd->arg1, dscp) & 0x3F); 208 break; 209 case O_SETFIB: 210 snprintf(SNPARGS(action2, 0), "SetFib %d", 211 TARG(cmd->arg1, fib) & 0x7FFF); 212 break; 213 case O_SKIPTO: 214 snprintf(SNPARGS(action2, 0), "SkipTo %d", 215 TARG(cmd->arg1, skipto)); 216 break; 217 case O_PIPE: 218 snprintf(SNPARGS(action2, 0), "Pipe %d", 219 TARG(cmd->arg1, pipe)); 220 break; 221 case O_QUEUE: 222 snprintf(SNPARGS(action2, 0), "Queue %d", 223 TARG(cmd->arg1, pipe)); 224 break; 225 case O_FORWARD_IP: { 226 char buf[INET_ADDRSTRLEN]; 227 ipfw_insn_sa *sa = (ipfw_insn_sa *)cmd; 228 int len; 229 struct in_addr dummyaddr; 230 if (sa->sa.sin_addr.s_addr == INADDR_ANY) 231 dummyaddr.s_addr = htonl(tablearg); 232 else 233 dummyaddr.s_addr = sa->sa.sin_addr.s_addr; 234 235 len = snprintf(SNPARGS(action2, 0), "Forward to %s", 236 inet_ntoa_r(dummyaddr, buf)); 237 238 if (sa->sa.sin_port) 239 snprintf(SNPARGS(action2, len), ":%d", 240 sa->sa.sin_port); 241 } 242 break; 243 #ifdef INET6 244 case O_FORWARD_IP6: { 245 char buf[INET6_ADDRSTRLEN]; 246 ipfw_insn_sa6 *sa = (ipfw_insn_sa6 *)cmd; 247 int len; 248 249 len = snprintf(SNPARGS(action2, 0), "Forward to [%s]", 250 ip6_sprintf(buf, &sa->sa.sin6_addr)); 251 252 if (sa->sa.sin6_port) 253 snprintf(SNPARGS(action2, len), ":%u", 254 sa->sa.sin6_port); 255 } 256 break; 257 #endif 258 case O_NETGRAPH: 259 snprintf(SNPARGS(action2, 0), "Netgraph %d", 260 cmd->arg1); 261 break; 262 case O_NGTEE: 263 snprintf(SNPARGS(action2, 0), "Ngtee %d", 264 cmd->arg1); 265 break; 266 case O_NAT: 267 action = "Nat"; 268 break; 269 case O_REASS: 270 action = "Reass"; 271 break; 272 case O_CALLRETURN: 273 if (cmd->len & F_NOT) 274 action = "Return"; 275 else 276 snprintf(SNPARGS(action2, 0), "Call %d", 277 cmd->arg1); 278 break; 279 case O_SETMARK: 280 if (cmd->arg1 == IP_FW_TARG) 281 snprintf(SNPARGS(action2, 0), "SetMark %#x", 282 TARG(cmd->arg1, mark)); 283 else 284 snprintf(SNPARGS(action2, 0), "SetMark %#x", 285 ((ipfw_insn_u32 *)cmd)->d[0]); 286 break; 287 case O_EXTERNAL_ACTION: 288 snprintf(SNPARGS(action2, 0), "Eaction %s", 289 ((struct named_object *)SRV_OBJECT(chain, 290 cmd->arg1))->name); 291 break; 292 default: 293 action = "UNKNOWN"; 294 break; 295 } 296 } 297 298 if (hlen == 0) { /* non-ip */ 299 snprintf(SNPARGS(proto, 0), "MAC"); 300 301 } else { 302 int len; 303 #ifdef INET6 304 char src[INET6_ADDRSTRLEN + 2], dst[INET6_ADDRSTRLEN + 2]; 305 #else 306 char src[INET_ADDRSTRLEN], dst[INET_ADDRSTRLEN]; 307 #endif 308 struct icmphdr *icmp; 309 struct tcphdr *tcp; 310 struct udphdr *udp; 311 #ifdef INET6 312 struct ip6_hdr *ip6 = NULL; 313 struct icmp6_hdr *icmp6; 314 u_short ip6f_mf; 315 #endif 316 src[0] = '\0'; 317 dst[0] = '\0'; 318 #ifdef INET6 319 ip6f_mf = offset & IP6F_MORE_FRAG; 320 offset &= IP6F_OFF_MASK; 321 322 if (IS_IP6_FLOW_ID(&(args->f_id))) { 323 char ip6buf[INET6_ADDRSTRLEN]; 324 snprintf(src, sizeof(src), "[%s]", 325 ip6_sprintf(ip6buf, &args->f_id.src_ip6)); 326 snprintf(dst, sizeof(dst), "[%s]", 327 ip6_sprintf(ip6buf, &args->f_id.dst_ip6)); 328 329 ip6 = (struct ip6_hdr *)ip; 330 tcp = (struct tcphdr *)(((char *)ip) + hlen); 331 udp = (struct udphdr *)(((char *)ip) + hlen); 332 } else 333 #endif 334 { 335 tcp = L3HDR(struct tcphdr, ip); 336 udp = L3HDR(struct udphdr, ip); 337 338 inet_ntop(AF_INET, &ip->ip_src, src, sizeof(src)); 339 inet_ntop(AF_INET, &ip->ip_dst, dst, sizeof(dst)); 340 } 341 342 switch (args->f_id.proto) { 343 case IPPROTO_TCP: 344 len = snprintf(SNPARGS(proto, 0), "TCP %s", src); 345 if (offset == 0) 346 snprintf(SNPARGS(proto, len), ":%d %s:%d", 347 ntohs(tcp->th_sport), 348 dst, 349 ntohs(tcp->th_dport)); 350 else 351 snprintf(SNPARGS(proto, len), " %s", dst); 352 break; 353 354 case IPPROTO_UDP: 355 case IPPROTO_UDPLITE: 356 len = snprintf(SNPARGS(proto, 0), "UDP%s%s", 357 args->f_id.proto == IPPROTO_UDP ? " ": "Lite ", 358 src); 359 if (offset == 0) 360 snprintf(SNPARGS(proto, len), ":%d %s:%d", 361 ntohs(udp->uh_sport), 362 dst, 363 ntohs(udp->uh_dport)); 364 else 365 snprintf(SNPARGS(proto, len), " %s", dst); 366 break; 367 368 case IPPROTO_ICMP: 369 icmp = L3HDR(struct icmphdr, ip); 370 if (offset == 0) 371 len = snprintf(SNPARGS(proto, 0), 372 "ICMP:%u.%u ", 373 icmp->icmp_type, icmp->icmp_code); 374 else 375 len = snprintf(SNPARGS(proto, 0), "ICMP "); 376 len += snprintf(SNPARGS(proto, len), "%s", src); 377 snprintf(SNPARGS(proto, len), " %s", dst); 378 break; 379 #ifdef INET6 380 case IPPROTO_ICMPV6: 381 icmp6 = (struct icmp6_hdr *)(((char *)ip) + hlen); 382 if (offset == 0) 383 len = snprintf(SNPARGS(proto, 0), 384 "ICMPv6:%u.%u ", 385 icmp6->icmp6_type, icmp6->icmp6_code); 386 else 387 len = snprintf(SNPARGS(proto, 0), "ICMPv6 "); 388 len += snprintf(SNPARGS(proto, len), "%s", src); 389 snprintf(SNPARGS(proto, len), " %s", dst); 390 break; 391 #endif 392 default: 393 len = snprintf(SNPARGS(proto, 0), "P:%d %s", 394 args->f_id.proto, src); 395 snprintf(SNPARGS(proto, len), " %s", dst); 396 break; 397 } 398 399 #ifdef INET6 400 if (IS_IP6_FLOW_ID(&(args->f_id))) { 401 if (offset || ip6f_mf) 402 snprintf(SNPARGS(fragment, 0), 403 " (frag %08x:%d@%d%s)", 404 args->f_id.extra, 405 ntohs(ip6->ip6_plen) - hlen, 406 ntohs(offset) << 3, ip6f_mf ? "+" : ""); 407 } else 408 #endif 409 { 410 int ipoff, iplen; 411 ipoff = ntohs(ip->ip_off); 412 iplen = ntohs(ip->ip_len); 413 if (ipoff & (IP_MF | IP_OFFMASK)) 414 snprintf(SNPARGS(fragment, 0), 415 " (frag %d:%d@%d%s)", 416 ntohs(ip->ip_id), iplen - (ip->ip_hl << 2), 417 offset << 3, 418 (ipoff & IP_MF) ? "+" : ""); 419 } 420 } 421 422 /* [fw]mark */ 423 if (args->rule.pkt_mark) 424 snprintf(SNPARGS(mark_str, 0), " mark:%#x", 425 args->rule.pkt_mark); 426 else 427 mark_str[0] = '\0'; 428 429 #ifdef __FreeBSD__ 430 log(LOG_SECURITY | LOG_INFO, "ipfw: %d %s %s%s %s via %s%s\n", 431 f ? f->rulenum : -1, action, proto, mark_str, 432 args->flags & IPFW_ARGS_OUT ? "out" : "in", args->ifp->if_xname, 433 fragment); 434 #else 435 log(LOG_SECURITY | LOG_INFO, "ipfw: %d %s %s%s [no if info]%s\n", 436 f ? f->rulenum : -1, action, proto, mark_str, fragment); 437 #endif 438 if (limit_reached) 439 log(LOG_SECURITY | LOG_NOTICE, 440 "ipfw: limit %d reached on entry %d\n", 441 limit_reached, f ? f->rulenum : -1); 442 } 443 /* end of file */ 444