1/*
2 * Copyright (c) 2022 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
32SYSCTL_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
41int inp_log_privacy = INP_LOG_PRIVACY_DEFAULT;
42SYSCTL_INT(_net_inet_ip_log, OID_AUTO, privacy,
43 CTLFLAG_RW | CTLFLAG_LOCKED, &inp_log_privacy, 0, "");
44
45void
46inp_log_addresses(struct inpcb *inp, char *lbuf, socklen_t lbuflen, char *fbuf, socklen_t fbuflen)
47{
48 /*
49 * Ugly but %{private} does not work in the kernel version of os_log()
50 */
51 if (inp_log_privacy != 0) {
52 if (inp->inp_vflag & INP_IPV6) {
53 strlcpy(dst: lbuf, src: "<IPv6-redacted>", n: lbuflen);
54 strlcpy(dst: fbuf, src: "<IPv6-redacted>", n: fbuflen);
55 } else {
56 strlcpy(dst: lbuf, src: "<IPv4-redacted>", n: lbuflen);
57 strlcpy(dst: fbuf, src: "<IPv4-redacted>", n: fbuflen);
58 }
59 } else if (inp->inp_vflag & INP_IPV6) {
60 struct in6_addr addr6;
61
62 if (IN6_IS_ADDR_LINKLOCAL(&inp->in6p_laddr)) {
63 addr6 = inp->in6p_laddr;
64 addr6.s6_addr16[1] = 0;
65 inet_ntop(AF_INET6, (void *)&addr6, lbuf, lbuflen);
66 } else {
67 inet_ntop(AF_INET6, (void *)&inp->in6p_laddr, lbuf, lbuflen);
68 }
69
70 if (IN6_IS_ADDR_LINKLOCAL(&inp->in6p_faddr)) {
71 addr6 = inp->in6p_faddr;
72 addr6.s6_addr16[1] = 0;
73 inet_ntop(AF_INET6, (void *)&addr6, fbuf, fbuflen);
74 } else {
75 inet_ntop(AF_INET6, (void *)&inp->in6p_faddr, fbuf, fbuflen);
76 }
77 } else {
78 inet_ntop(AF_INET, (void *)&inp->inp_laddr.s_addr, lbuf, lbuflen);
79 inet_ntop(AF_INET, (void *)&inp->inp_faddr.s_addr, fbuf, fbuflen);
80 }
81}
82