Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 | /* * Copyright (c) 2023-2024 Apple Inc. All rights reserved. * * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. The rights granted to you under the License * may not be used to create, or enable the creation or redistribution of, * unlawful or unlicensed copies of an Apple operating system, or to * circumvent, violate, or enable the circumvention or violation of, any * terms of an Apple operating system software license agreement. * * Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ */ #include <sys/param.h> #include <sys/protosw.h> #include <sys/systm.h> #include <sys/sysctl.h> #include <kern/bits.h> #include <netinet/ip.h> #include <netinet/ip6.h> #include <netinet/inp_log.h> #include <netinet/in_pcb.h> #include <netinet/udp_log.h> #include <netinet/udp_var.h> SYSCTL_NODE(_net_inet_udp, OID_AUTO, log, CTLFLAG_RW | CTLFLAG_LOCKED, 0, "UDP"); #if (DEVELOPMENT || DEBUG) #define UDP_LOG_ENABLE_DEFAULT \ (ULEF_CONNECT | ULEF_DST_LOCAL | ULEF_DST_GW) #else /* (DEVELOPMENT || DEBUG) */ #define UDP_LOG_ENABLE_DEFAULT \ (ULEF_CONNECT | ULEF_DST_LOCAL | ULEF_DST_GW) #endif /* (DEVELOPMENT || DEBUG) */ uint32_t udp_log_enable_flags = UDP_LOG_ENABLE_DEFAULT; SYSCTL_UINT(_net_inet_udp_log, OID_AUTO, enable, CTLFLAG_RW | CTLFLAG_LOCKED, &udp_log_enable_flags, 0, ""); /* * The following is a help to describe the values of the flags */ #define X(name, value, description, ...) #description ":" #value " " SYSCTL_STRING(_net_inet_udp_log, OID_AUTO, enable_usage, CTLFLAG_RD | CTLFLAG_LOCKED, UDP_ENABLE_FLAG_LIST, 0, ""); #undef X /* * For binding */ int udp_log_bind_anon_port = 0; SYSCTL_INT(_net_inet_udp_log, OID_AUTO, bind_anon_port, CTLFLAG_RW | CTLFLAG_LOCKED, &udp_log_bind_anon_port, 0, ""); int udp_log_local_port_included = 0; SYSCTL_PROC(_net_inet_udp_log, OID_AUTO, local_port_included, CTLFLAG_RW | CTLFLAG_LOCKED, &udp_log_local_port_included, 0, &sysctl_inp_log_port, "I", ""); int udp_log_remote_port_included = 0; SYSCTL_PROC(_net_inet_udp_log, OID_AUTO, remote_port_included, CTLFLAG_RW | CTLFLAG_LOCKED, &udp_log_remote_port_included, 0, &sysctl_inp_log_port, "I", ""); int udp_log_local_port_excluded = 0; SYSCTL_PROC(_net_inet_udp_log, OID_AUTO, local_port_excluded, CTLFLAG_RW | CTLFLAG_LOCKED, &udp_log_local_port_excluded, 0, &sysctl_inp_log_port, "I", ""); int udp_log_remote_port_excluded = 0; SYSCTL_PROC(_net_inet_udp_log, OID_AUTO, remote_port_excluded, CTLFLAG_RW | CTLFLAG_LOCKED, &udp_log_remote_port_excluded, 0, &sysctl_inp_log_port, "I", ""); #define UDP_LOG_RATE_LIMIT 1000 static unsigned int udp_log_rate_limit = UDP_LOG_RATE_LIMIT; SYSCTL_UINT(_net_inet_udp_log, OID_AUTO, rate_limit, CTLFLAG_RW | CTLFLAG_LOCKED, &udp_log_rate_limit, 0, ""); /* 1 minute by default */ #define UDP_LOG_RATE_DURATION 60 static unsigned int udp_log_rate_duration = UDP_LOG_RATE_DURATION; SYSCTL_UINT(_net_inet_udp_log, OID_AUTO, rate_duration, CTLFLAG_RW | CTLFLAG_LOCKED, &udp_log_rate_duration, 0, ""); static unsigned long udp_log_rate_max = 0; SYSCTL_ULONG(_net_inet_udp_log, OID_AUTO, rate_max, CTLFLAG_RD | CTLFLAG_LOCKED, &udp_log_rate_max, ""); static unsigned long udp_log_rate_exceeded_total = 0; SYSCTL_ULONG(_net_inet_udp_log, OID_AUTO, rate_exceeded_total, CTLFLAG_RD | CTLFLAG_LOCKED, &udp_log_rate_exceeded_total, ""); static unsigned long udp_log_rate_current = 0; SYSCTL_ULONG(_net_inet_udp_log, OID_AUTO, rate_current, CTLFLAG_RD | CTLFLAG_LOCKED, &udp_log_rate_current, ""); static bool udp_log_rate_exceeded_logged = false; static uint64_t udp_log_current_period = 0; os_log_t udp_log_handle = OS_LOG_DEFAULT; #define UDP_LOG_COMMON_FMT \ "[%s:%u<->%s:%u] " \ "interface: %s " \ "(skipped: %lu)\n" #define UDP_LOG_COMMON_ARGS \ laddr_buf, ntohs(local_port), faddr_buf, ntohs(foreign_port), \ ifp != NULL ? if_name(ifp) : "", \ udp_log_rate_exceeded_total #define UDP_LOG_COMMON_PCB_FMT \ UDP_LOG_COMMON_FMT \ "so_gencnt: %llu " \ "so_state: 0x%04x " \ "process: %s:%u " #define UDP_LOG_COMMON_PCB_ARGS \ UDP_LOG_COMMON_ARGS, \ so != NULL ? so->so_gencnt : 0, \ so != NULL ? so->so_state : 0, \ inp->inp_last_proc_name, so->last_pid /* * Returns true when above the rate limit */ static bool udp_log_is_rate_limited(void) { uint64_t current_net_period = net_uptime(); /* When set to zero it means to reset to default */ if (udp_log_rate_duration == 0) { udp_log_rate_duration = UDP_LOG_RATE_DURATION; } if (udp_log_rate_limit == 0) { udp_log_rate_duration = UDP_LOG_RATE_LIMIT; } if (current_net_period > udp_log_current_period + udp_log_rate_duration) { if (udp_log_rate_current > udp_log_rate_max) { udp_log_rate_max = udp_log_rate_current; } udp_log_current_period = current_net_period; udp_log_rate_current = 0; udp_log_rate_exceeded_logged = false; } udp_log_rate_current += 1; if (udp_log_rate_current > (unsigned long) udp_log_rate_limit) { udp_log_rate_exceeded_total += 1; return true; } return false; } static bool udp_log_port_allowed(struct inpcb *inp) { if (udp_log_local_port_included != 0 || udp_log_remote_port_included != 0) { if (ntohs(inp->inp_lport) == udp_log_local_port_included || ntohs(inp->inp_fport) == udp_log_remote_port_included) { return true; } else { return false; } } if (udp_log_local_port_excluded != 0 || udp_log_remote_port_excluded != 0) { if (ntohs(inp->inp_lport) == udp_log_local_port_excluded || ntohs(inp->inp_fport) == udp_log_remote_port_excluded) { return false; } } return true; } static inline void udp_log_common(struct inpcb *inp, const char *event, int error) { struct socket *so = inp->inp_socket; struct ifnet *ifp; char laddr_buf[ADDRESS_STR_LEN]; char faddr_buf[ADDRESS_STR_LEN]; in_port_t local_port; in_port_t foreign_port; /* Clear the logging flags as one may reconnect an UDP socket */ inp->inp_flags2 &= ~INP2_LOGGED_SUMMARY; inp->inp_flags2 |= INP2_LOGGING_ENABLED; so = inp->inp_socket; local_port = inp->inp_lport; foreign_port = inp->inp_fport; ifp = inp->inp_last_outifp != NULL ? inp->inp_last_outifp : inp->inp_boundifp != NULL ? inp->inp_boundifp : NULL; inp_log_addresses(inp, laddr_buf, sizeof(laddr_buf), faddr_buf, sizeof(faddr_buf)); #define UDP_LOG_CONNECTION_FMT \ "udp %s: " \ UDP_LOG_COMMON_PCB_FMT \ "bytes in/out: %llu/%llu " \ "pkts in/out: %llu/%llu " \ "error: %d " \ "so_error: %d " \ "svc/tc: %u " \ "flow: 0x%x" #define UDP_LOG_CONNECTION_ARGS \ event, \ UDP_LOG_COMMON_PCB_ARGS, \ inp->inp_mstat.ms_total.ts_rxbytes, inp->inp_mstat.ms_total.ts_txbytes, \ inp->inp_mstat.ms_total.ts_rxpackets, inp->inp_mstat.ms_total.ts_txpackets, \ error, \ so->so_error, \ (so->so_flags1 & SOF1_TC_NET_SERV_TYPE) ? so->so_netsvctype : so->so_traffic_class, \ inp->inp_flowhash os_log(udp_log_handle, UDP_LOG_CONNECTION_FMT, UDP_LOG_CONNECTION_ARGS); #undef UDP_LOG_CONNECTION_FMT #undef UDP_LOG_CONNECTION_ARGS } __attribute__((noinline)) void udp_log_bind(struct inpcb *inp, int error) { if (inp == NULL || inp->inp_socket == NULL) { return; } /* Do not log too much */ if (udp_log_is_rate_limited()) { return; } /* Allow logging of bind errors regardless of the port */ if (error == 0) { if (udp_log_bind_anon_port == 0 && (inp->inp_flags & INP_ANONPORT) != 0) { return; } if (udp_log_port_allowed(inp) == false) { return; } } udp_log_common(inp, "bind", error); } __attribute__((noinline)) void udp_log_connect(struct inpcb *inp, int error) { if (inp == NULL || inp->inp_socket == NULL) { return; } /* Do not log too much */ if (udp_log_is_rate_limited()) { return; } /* Allow logging of connection errors regardless of the port */ if (error == 0 && udp_log_port_allowed(inp) == false) { return; } udp_log_common(inp, "connect", error); } __attribute__((noinline)) void udp_log_connection_summary(struct inpcb *inp) { struct socket *so; struct ifnet *ifp; clock_sec_t duration_secs = 0; clock_usec_t duration_microsecs = 0; clock_sec_t connection_secs = 0; clock_usec_t connection_microsecs = 0; char laddr_buf[ADDRESS_STR_LEN]; char faddr_buf[ADDRESS_STR_LEN]; in_port_t local_port; in_port_t foreign_port; if (inp == NULL || inp->inp_socket == NULL) { return; } if ((inp->inp_flags2 & INP2_LOGGING_ENABLED) == 0) { return; } /* Make sure the summary is logged once */ if (inp->inp_flags2 & INP2_LOGGED_SUMMARY) { return; } inp->inp_flags2 |= INP2_LOGGED_SUMMARY; inp->inp_flags2 &= ~INP2_LOGGING_ENABLED; /* UDP can be reconnected so clear the destination flags */ inp->inp_log_flags = 0; /* Do not log too much */ if (udp_log_is_rate_limited()) { return; } so = inp->inp_socket; local_port = inp->inp_lport; foreign_port = inp->inp_fport; /* * inp_start_timestamp is when the UDP socket was open. * * inp_connect_timestamp is when the connection started on */ uint64_t now = mach_continuous_time(); if (inp->inp_start_timestamp > 0) { uint64_t duration = now - inp->inp_start_timestamp; absolutetime_to_microtime(duration, &duration_secs, &duration_microsecs); } if (inp->inp_connect_timestamp > 0) { uint64_t duration = now - inp->inp_connect_timestamp; absolutetime_to_microtime(duration, &connection_secs, &connection_microsecs); } ifp = inp->inp_last_outifp != NULL ? inp->inp_last_outifp : inp->inp_boundifp != NULL ? inp->inp_boundifp : NULL; inp_log_addresses(inp, laddr_buf, sizeof(laddr_buf), faddr_buf, sizeof(faddr_buf)); /* * Need to use 2 log messages because the size of the summary */ #define UDP_LOG_CONNECTION_SUMMARY_FMT \ "udp_connection_summary " \ UDP_LOG_COMMON_PCB_FMT \ "Duration: %lu.%03d sec " \ "Conn_Time: %lu.%03d sec " \ "bytes in/out: %llu/%llu " \ "pkts in/out: %llu/%llu " \ "rxnospace pkts/bytes: %llu/%llu " \ "so_error: %d " \ "svc/tc: %u " \ "flow: 0x%x " \ "flowctl: %lluus (%llux) " #define UDP_LOG_CONNECTION_SUMMARY_ARGS \ UDP_LOG_COMMON_PCB_ARGS, \ duration_secs, duration_microsecs / 1000, \ connection_secs, connection_microsecs / 1000, \ inp->inp_mstat.ms_total.ts_rxbytes, inp->inp_mstat.ms_total.ts_txbytes, \ inp->inp_mstat.ms_total.ts_rxpackets, inp->inp_mstat.ms_total.ts_txpackets, \ so->so_tc_stats[SO_STATS_SBNOSPACE].rxpackets, \ so->so_tc_stats[SO_STATS_SBNOSPACE].rxbytes, \ so->so_error, \ (so->so_flags1 & SOF1_TC_NET_SERV_TYPE) ? so->so_netsvctype : so->so_traffic_class, \ inp->inp_flowhash, \ inp->inp_fadv_total_time, \ inp->inp_fadv_cnt os_log(udp_log_handle, UDP_LOG_CONNECTION_SUMMARY_FMT, UDP_LOG_CONNECTION_SUMMARY_ARGS); #undef UDP_LOG_CONNECTION_SUMMARY_FMT #undef UDP_LOG_CONNECTION_SUMMARY_ARGS } __attribute__((noinline)) void udp_log_message(const char *func_name, int line_no, struct inpcb *inp, const char *format, ...) { struct socket *so; struct ifnet *ifp; char laddr_buf[ADDRESS_STR_LEN]; char faddr_buf[ADDRESS_STR_LEN]; in_port_t local_port; in_port_t foreign_port; char message[256]; if (inp == NULL || inp->inp_socket == NULL) { return; } /* Do not log too much */ if (udp_log_is_rate_limited()) { return; } if (!udp_log_port_allowed(inp)) { return; } so = inp->inp_socket; local_port = inp->inp_lport; foreign_port = inp->inp_fport; ifp = inp->inp_last_outifp != NULL ? inp->inp_last_outifp : inp->inp_boundifp != NULL ? inp->inp_boundifp : NULL; inp_log_addresses(inp, laddr_buf, sizeof(laddr_buf), faddr_buf, sizeof(faddr_buf)); va_list ap; va_start(ap, format); vsnprintf(message, sizeof(message), format, ap); va_end(ap); #define UDP_LOG_MESSAGE_FMT \ "udp (%s:%d): " \ UDP_LOG_COMMON_PCB_FMT \ "%s" #define UDP_LOG_MESSAGE_ARGS \ func_name, line_no, \ UDP_LOG_COMMON_PCB_ARGS, \ message os_log(udp_log_handle, UDP_LOG_MESSAGE_FMT, UDP_LOG_MESSAGE_ARGS); #undef UDP_LOG_MESSAGE_FMT #undef UDP_LOG_MESSAGE_ARGS } static bool udp_log_pkt_addresses(void *hdr, bool outgoing, char *__sized_by(lbuflen) lbuf, socklen_t lbuflen, char *__sized_by(fbuflen) fbuf, socklen_t fbuflen) { bool isipv6; isipv6 = (((struct ip *)hdr)->ip_v == 6); if (isipv6) { struct ip6_hdr *ip6 = (struct ip6_hdr *)hdr; struct in6_addr src_addr6 = ip6->ip6_src; struct in6_addr dst_addr6 = ip6->ip6_dst; #pragma clang diagnostic push #pragma clang diagnostic ignored "-Waddress-of-packed-member" if (memcmp(&src_addr6, &in6addr_loopback, sizeof(struct in6_addr)) == 0 || memcmp(&dst_addr6, &in6addr_loopback, sizeof(struct in6_addr)) == 0) { if (!(udp_log_enable_flags & ULEF_DST_LOOPBACK)) { return false; } } #pragma clang diagnostic pop if (IN6_IS_ADDR_LINKLOCAL(&src_addr6)) { src_addr6.s6_addr16[1] = 0; } if (IN6_IS_ADDR_LINKLOCAL(&dst_addr6)) { dst_addr6.s6_addr16[1] = 0; } if (inp_log_privacy != 0) { strlcpy(lbuf, "<IPv6-redacted>", lbuflen); strlcpy(fbuf, "<IPv6-redacted>", fbuflen); } else if (outgoing) { inet_ntop(AF_INET6, &src_addr6, lbuf, lbuflen); inet_ntop(AF_INET6, &dst_addr6, fbuf, fbuflen); } else { inet_ntop(AF_INET6, &dst_addr6, lbuf, lbuflen); inet_ntop(AF_INET6, &src_addr6, fbuf, fbuflen); } } else { struct ip *ip = (struct ip *)hdr; if (ntohl(ip->ip_src.s_addr) == INADDR_LOOPBACK || ntohl(ip->ip_dst.s_addr) == INADDR_LOOPBACK) { if (!(udp_log_enable_flags & ULEF_DST_LOOPBACK)) { return false; } } if (inp_log_privacy != 0) { strlcpy(lbuf, "<IPv4-redacted>", lbuflen); strlcpy(fbuf, "<IPv4-redacted>", fbuflen); } else if (outgoing) { inet_ntop(AF_INET, (void *)&ip->ip_src.s_addr, lbuf, lbuflen); inet_ntop(AF_INET, (void *)&ip->ip_dst.s_addr, fbuf, fbuflen); } else { inet_ntop(AF_INET, (void *)&ip->ip_dst.s_addr, lbuf, lbuflen); inet_ntop(AF_INET, (void *)&ip->ip_src.s_addr, fbuf, fbuflen); } } return true; } __attribute__((noinline)) void udp_log_drop_pcb(void *hdr, struct udphdr *uh, struct inpcb *inp, bool outgoing, const char *reason) { struct socket *so; struct ifnet *ifp; char laddr_buf[ADDRESS_STR_LEN]; char faddr_buf[ADDRESS_STR_LEN]; in_port_t local_port; in_port_t foreign_port; const char *direction = ""; if (inp == NULL) { return; } so = inp->inp_socket; if (so == NULL) { return; } /* Do not log common drops after the connection termination is logged */ if ((inp->inp_flags2 & INP2_LOGGED_SUMMARY) && ((so->so_state & SS_NOFDREF) || (so->so_flags & SOF_DEFUNCT) || (so->so_state & SS_CANTRCVMORE))) { return; } /* Do not log too much */ if (udp_log_is_rate_limited()) { return; } /* Use the packet addresses when in the data path */ if (hdr != NULL && uh != NULL) { if (outgoing) { local_port = uh->uh_sport; foreign_port = uh->uh_dport; direction = "outgoing "; } else { local_port = uh->uh_dport; foreign_port = uh->uh_sport; direction = "incoming "; } (void) udp_log_pkt_addresses(hdr, outgoing, laddr_buf, sizeof(laddr_buf), faddr_buf, sizeof(faddr_buf)); } else { local_port = inp->inp_lport; foreign_port = inp->inp_fport; inp_log_addresses(inp, laddr_buf, sizeof(laddr_buf), faddr_buf, sizeof(faddr_buf)); } ifp = inp->inp_last_outifp != NULL ? inp->inp_last_outifp : inp->inp_boundifp != NULL ? inp->inp_boundifp : NULL; #define UDP_LOG_DROP_PCB_FMT \ "udp drop %s" \ UDP_LOG_COMMON_PCB_FMT \ "so_error: %d " \ "reason: %s" #define UDP_LOG_DROP_PCB_ARGS \ direction, \ UDP_LOG_COMMON_PCB_ARGS, \ so->so_error, \ reason os_log(udp_log_handle, UDP_LOG_DROP_PCB_FMT, UDP_LOG_DROP_PCB_ARGS); #undef UDP_LOG_DROP_PCB_FMT #undef UDP_LOG_DROP_PCB_ARGS } |