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]; 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_EXTERNAL_ACTION: 280 snprintf(SNPARGS(action2, 0), "Eaction %s", 281 ((struct named_object *)SRV_OBJECT(chain, 282 cmd->arg1))->name); 283 break; 284 default: 285 action = "UNKNOWN"; 286 break; 287 } 288 } 289 290 if (hlen == 0) { /* non-ip */ 291 snprintf(SNPARGS(proto, 0), "MAC"); 292 293 } else { 294 int len; 295 #ifdef INET6 296 char src[INET6_ADDRSTRLEN + 2], dst[INET6_ADDRSTRLEN + 2]; 297 #else 298 char src[INET_ADDRSTRLEN], dst[INET_ADDRSTRLEN]; 299 #endif 300 struct icmphdr *icmp; 301 struct tcphdr *tcp; 302 struct udphdr *udp; 303 #ifdef INET6 304 struct ip6_hdr *ip6 = NULL; 305 struct icmp6_hdr *icmp6; 306 u_short ip6f_mf; 307 #endif 308 src[0] = '\0'; 309 dst[0] = '\0'; 310 #ifdef INET6 311 ip6f_mf = offset & IP6F_MORE_FRAG; 312 offset &= IP6F_OFF_MASK; 313 314 if (IS_IP6_FLOW_ID(&(args->f_id))) { 315 char ip6buf[INET6_ADDRSTRLEN]; 316 snprintf(src, sizeof(src), "[%s]", 317 ip6_sprintf(ip6buf, &args->f_id.src_ip6)); 318 snprintf(dst, sizeof(dst), "[%s]", 319 ip6_sprintf(ip6buf, &args->f_id.dst_ip6)); 320 321 ip6 = (struct ip6_hdr *)ip; 322 tcp = (struct tcphdr *)(((char *)ip) + hlen); 323 udp = (struct udphdr *)(((char *)ip) + hlen); 324 } else 325 #endif 326 { 327 tcp = L3HDR(struct tcphdr, ip); 328 udp = L3HDR(struct udphdr, ip); 329 330 inet_ntop(AF_INET, &ip->ip_src, src, sizeof(src)); 331 inet_ntop(AF_INET, &ip->ip_dst, dst, sizeof(dst)); 332 } 333 334 switch (args->f_id.proto) { 335 case IPPROTO_TCP: 336 len = snprintf(SNPARGS(proto, 0), "TCP %s", src); 337 if (offset == 0) 338 snprintf(SNPARGS(proto, len), ":%d %s:%d", 339 ntohs(tcp->th_sport), 340 dst, 341 ntohs(tcp->th_dport)); 342 else 343 snprintf(SNPARGS(proto, len), " %s", dst); 344 break; 345 346 case IPPROTO_UDP: 347 case IPPROTO_UDPLITE: 348 len = snprintf(SNPARGS(proto, 0), "UDP%s%s", 349 args->f_id.proto == IPPROTO_UDP ? " ": "Lite ", 350 src); 351 if (offset == 0) 352 snprintf(SNPARGS(proto, len), ":%d %s:%d", 353 ntohs(udp->uh_sport), 354 dst, 355 ntohs(udp->uh_dport)); 356 else 357 snprintf(SNPARGS(proto, len), " %s", dst); 358 break; 359 360 case IPPROTO_ICMP: 361 icmp = L3HDR(struct icmphdr, ip); 362 if (offset == 0) 363 len = snprintf(SNPARGS(proto, 0), 364 "ICMP:%u.%u ", 365 icmp->icmp_type, icmp->icmp_code); 366 else 367 len = snprintf(SNPARGS(proto, 0), "ICMP "); 368 len += snprintf(SNPARGS(proto, len), "%s", src); 369 snprintf(SNPARGS(proto, len), " %s", dst); 370 break; 371 #ifdef INET6 372 case IPPROTO_ICMPV6: 373 icmp6 = (struct icmp6_hdr *)(((char *)ip) + hlen); 374 if (offset == 0) 375 len = snprintf(SNPARGS(proto, 0), 376 "ICMPv6:%u.%u ", 377 icmp6->icmp6_type, icmp6->icmp6_code); 378 else 379 len = snprintf(SNPARGS(proto, 0), "ICMPv6 "); 380 len += snprintf(SNPARGS(proto, len), "%s", src); 381 snprintf(SNPARGS(proto, len), " %s", dst); 382 break; 383 #endif 384 default: 385 len = snprintf(SNPARGS(proto, 0), "P:%d %s", 386 args->f_id.proto, src); 387 snprintf(SNPARGS(proto, len), " %s", dst); 388 break; 389 } 390 391 #ifdef INET6 392 if (IS_IP6_FLOW_ID(&(args->f_id))) { 393 if (offset || ip6f_mf) 394 snprintf(SNPARGS(fragment, 0), 395 " (frag %08x:%d@%d%s)", 396 args->f_id.extra, 397 ntohs(ip6->ip6_plen) - hlen, 398 ntohs(offset) << 3, ip6f_mf ? "+" : ""); 399 } else 400 #endif 401 { 402 int ipoff, iplen; 403 ipoff = ntohs(ip->ip_off); 404 iplen = ntohs(ip->ip_len); 405 if (ipoff & (IP_MF | IP_OFFMASK)) 406 snprintf(SNPARGS(fragment, 0), 407 " (frag %d:%d@%d%s)", 408 ntohs(ip->ip_id), iplen - (ip->ip_hl << 2), 409 offset << 3, 410 (ipoff & IP_MF) ? "+" : ""); 411 } 412 } 413 #ifdef __FreeBSD__ 414 log(LOG_SECURITY | LOG_INFO, "ipfw: %d %s %s %s via %s%s\n", 415 f ? f->rulenum : -1, action, proto, 416 args->flags & IPFW_ARGS_OUT ? "out" : "in", args->ifp->if_xname, 417 fragment); 418 #else 419 log(LOG_SECURITY | LOG_INFO, "ipfw: %d %s %s [no if info]%s\n", 420 f ? f->rulenum : -1, action, proto, fragment); 421 #endif 422 if (limit_reached) 423 log(LOG_SECURITY | LOG_NOTICE, 424 "ipfw: limit %d reached on entry %d\n", 425 limit_reached, f ? f->rulenum : -1); 426 } 427 /* end of file */ 428