1 /*
2 * Copyright (c) 2022-2024 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 #include <netinet/in_var.h>
30 #include <netinet/inp_log.h>
31
32 SYSCTL_NODE(_net_inet_ip, OID_AUTO, log, CTLFLAG_RW | CTLFLAG_LOCKED, 0,
33 "TCP/IP + UDP logs");
34
35 #if (DEVELOPMENT || DEBUG)
36 #define INP_LOG_PRIVACY_DEFAULT 0
37 #else
38 #define INP_LOG_PRIVACY_DEFAULT 1
39 #endif /* (DEVELOPMENT || DEBUG) */
40
41 int inp_log_privacy = INP_LOG_PRIVACY_DEFAULT;
42 SYSCTL_INT(_net_inet_ip_log, OID_AUTO, privacy,
43 CTLFLAG_RW | CTLFLAG_LOCKED, &inp_log_privacy, 0, "");
44
45 void
inp_log_addresses(struct inpcb * inp,char * __sized_by (lbuflen)lbuf,socklen_t lbuflen,char * __sized_by (fbuflen)fbuf,socklen_t fbuflen)46 inp_log_addresses(struct inpcb *inp, char *__sized_by(lbuflen) lbuf,
47 socklen_t lbuflen, char *__sized_by(fbuflen) fbuf,
48 socklen_t fbuflen)
49 {
50 /*
51 * Ugly but %{private} does not work in the kernel version of os_log()
52 */
53 if (inp_log_privacy != 0) {
54 if (inp->inp_vflag & INP_IPV6) {
55 strlcpy(lbuf, "<IPv6-redacted>", lbuflen);
56 strlcpy(fbuf, "<IPv6-redacted>", fbuflen);
57 } else {
58 strlcpy(lbuf, "<IPv4-redacted>", lbuflen);
59 strlcpy(fbuf, "<IPv4-redacted>", fbuflen);
60 }
61 } else if (inp->inp_vflag & INP_IPV6) {
62 struct in6_addr addr6;
63
64 if (IN6_IS_ADDR_LINKLOCAL(&inp->in6p_laddr)) {
65 addr6 = inp->in6p_laddr;
66 addr6.s6_addr16[1] = 0;
67 inet_ntop(AF_INET6, (void *)&addr6, lbuf, lbuflen);
68 } else {
69 inet_ntop(AF_INET6, (void *)&inp->in6p_laddr, lbuf, lbuflen);
70 }
71
72 if (IN6_IS_ADDR_LINKLOCAL(&inp->in6p_faddr)) {
73 addr6 = inp->in6p_faddr;
74 addr6.s6_addr16[1] = 0;
75 inet_ntop(AF_INET6, (void *)&addr6, fbuf, fbuflen);
76 } else {
77 inet_ntop(AF_INET6, (void *)&inp->in6p_faddr, fbuf, fbuflen);
78 }
79 } else {
80 inet_ntop(AF_INET, (void *)&inp->inp_laddr.s_addr, lbuf, lbuflen);
81 inet_ntop(AF_INET, (void *)&inp->inp_faddr.s_addr, fbuf, fbuflen);
82 }
83 }
84