1 /*
2 * Copyright (c) 2018-2023 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
30 #ifndef _NETINET_TCP_LOG_H_
31 #define _NETINET_TCP_LOG_H_
32
33 #ifdef PRIVATE
34
35 #define TCP_ENABLE_FLAG_LIST \
36 X(TLEF_CONNECTION, 0x00000001, connection) \
37 X(TLEF_RTT, 0x00000002, rtt) \
38 X(TLEF_KEEP_ALIVE, 0x00000004, ka) \
39 X(TLEF_LOG, 0x00000008, log) \
40 X(TLEF_DST_LOOPBACK, 0x00000010, loop) \
41 X(TLEF_DST_LOCAL, 0x00000020, local) \
42 X(TLEF_DST_GW, 0x00000040, gw) \
43 X(TLEF_THF_SYN, 0x00000100, syn) \
44 X(TLEF_THF_FIN, 0x00000200, fin) \
45 X(TLEF_THF_RST, 0x00000400, rst) \
46 X(TLEF_DROP_NECP, 0x00001000, dropnecp) \
47 X(TLEF_DROP_PCB, 0x00002000, droppcb) \
48 X(TLEF_DROP_PKT, 0x00004000, droppkt) \
49 X(TLEF_FSW_FLOW, 0x00008000, fswflow) \
50 X(TLEF_STATE, 0x00010000, state) \
51 X(TLEF_SYN_RXMT, 0x00020000, synrxmt) \
52 X(TLEF_OUTPUT, 0x00040000, output) \
53 X(TLEF_BIND, 0x00080000, bind)
54
55 /*
56 * Flag values for tcp_log_enabled
57 */
58 enum {
59 #define X(name, value, ...) name = value,
60 TCP_ENABLE_FLAG_LIST
61 #undef X
62 };
63
64 #endif /* PRIVATE */
65
66 #ifdef BSD_KERNEL_PRIVATE
67
68 #include <sys/param.h>
69 #include <sys/systm.h>
70
71 #include <netinet/in.h>
72 #include <netinet/in_systm.h>
73 #include <netinet/in_pcb.h>
74 #include <netinet6/in6_pcb.h>
75
76 #include <netinet/tcp.h>
77 #include <netinet/tcp_var.h>
78 #include <netinet6/tcp6_var.h>
79
80 #include <netinet/inp_log.h>
81
82 #include <net/net_log_common.h>
83
84 #include <os/log.h>
85
86 #include <stdbool.h>
87
88 extern os_log_t tcp_mpkl_log_object;
89 extern uint32_t tcp_log_enable_flags;
90 extern uint16_t tcp_log_port;
91
92 #define TLEF_MASK_DST (TLEF_DST_LOOPBACK | TLEF_DST_LOCAL | TLEF_DST_GW)
93
94 extern void tcp_log_connection_summary(const char *func_name, int line_no, struct tcpcb *tp);
95 extern void tcp_log_th_flags(void *hdr, struct tcphdr *th, struct tcpcb *tp, bool outgoing, struct ifnet *ifp);
96 extern void tcp_log_connection(struct tcpcb *tp, const char *event, int error);
97 extern void tcp_log_listen(struct tcpcb *tp, int error);
98 extern void tcp_log_drop_pcb(void *hdr, struct tcphdr *th, struct tcpcb *tp, bool outgoing, const char *reason);
99 extern void tcp_log_drop_pkt(void *hdr, struct tcphdr *th, struct ifnet *ifp, const char *reason);
100 extern void tcp_log_rtt_info(const char *func_name, int line_no, struct tcpcb *tp);
101 extern void tcp_log_rt_rtt(const char *func_name, int line_no, struct tcpcb *tp, struct rtentry *rt);
102 extern void tcp_log_rtt_change(const char *func_name, int line_no, struct tcpcb *tp, int old_srtt, int old_rttvar);
103 extern void tcp_log_keepalive(const char *func_name, int line_no, struct tcpcb *tp, int32_t idle_time);
104 extern void tcp_log_message(const char *func_name, int line_no, struct tcpcb *tp, const char *format, ...) __printflike(4, 5);
105 extern void tcp_log_fsw_flow(const char *func_name, int line_no, struct tcpcb *tp, const char *format, ...) __printflike(4, 5);
106 extern void tcp_log_state_change(const char *func_name, int line_no, struct tcpcb *tp, int new_state);
107 extern void tcp_log_output(const char *func_name, int line_no, struct tcpcb *tp, const char *format, ...) __printflike(4, 5);
108 extern void tcp_log_bind(struct inpcb *inp, const char *event, int error);
109
110
111 #define IN6_IS_ADDR_V4MAPPED_LOOPBACK(a) \
112 ((*(const __uint32_t *)(const void *)(&(a)->s6_addr[0]) == 0) && \
113 (*(const __uint32_t *)(const void *)(&(a)->s6_addr[4]) == 0) && \
114 (*(const __uint32_t *)(const void *)(&(a)->s6_addr[8]) == ntohl(0x0000ffff)) && \
115 (*(const __uint32_t *)(const void *)(&(a)->s6_addr[12]) == ntohl(INADDR_LOOPBACK)))
116
117 static inline bool
tcp_is_log_enabled(struct tcpcb * tp,uint32_t req_flags)118 tcp_is_log_enabled(struct tcpcb *tp, uint32_t req_flags)
119 {
120 struct inpcb *inp;
121
122 if (tp == NULL || tp->t_inpcb == NULL) {
123 return false;
124 }
125 inp = tp->t_inpcb;
126 if (tcp_log_port > 0 &&
127 ntohs(inp->inp_lport) != tcp_log_port &&
128 ntohs(tp->t_inpcb->inp_fport) != tcp_log_port) {
129 return false;
130 }
131
132 /*
133 * First find out the kind of destination
134 */
135 if ((inp->inp_log_flags & TLEF_MASK_DST) == 0) {
136 if ((inp->inp_vflag & INP_IPV6) != 0) {
137 if (IN6_IS_ADDR_LOOPBACK(&tp->t_inpcb->in6p_laddr) ||
138 IN6_IS_ADDR_LOOPBACK(&tp->t_inpcb->in6p_faddr) ||
139 IN6_IS_ADDR_V4MAPPED_LOOPBACK(&tp->t_inpcb->in6p_laddr) ||
140 IN6_IS_ADDR_V4MAPPED_LOOPBACK(&tp->t_inpcb->in6p_faddr)) {
141 inp->inp_log_flags = TLEF_DST_LOOPBACK;
142 } else if (!IN6_IS_ADDR_UNSPECIFIED(&tp->t_inpcb->in6p_laddr) ||
143 !IN6_IS_ADDR_UNSPECIFIED(&tp->t_inpcb->in6p_faddr)) {
144 if (tp->t_flags & TF_LOCAL) {
145 inp->inp_log_flags |= TLEF_DST_LOCAL;
146 } else {
147 inp->inp_log_flags |= TLEF_DST_GW;
148 }
149 }
150 } else {
151 if (ntohl(inp->inp_laddr.s_addr) == INADDR_LOOPBACK ||
152 ntohl(inp->inp_faddr.s_addr) == INADDR_LOOPBACK) {
153 inp->inp_log_flags |= TLEF_DST_LOOPBACK;
154 } else if (tp->t_flags & TF_LOCAL) {
155 inp->inp_log_flags |= TLEF_DST_LOCAL;
156 } else if (ntohl(inp->inp_laddr.s_addr) != INADDR_ANY) {
157 inp->inp_log_flags |= TLEF_DST_GW;
158 }
159 }
160 if (tp->t_state == TCPS_LISTEN && (inp->inp_log_flags & TLEF_MASK_DST) == 0) {
161 /*
162 * Unspecified address means all scopes
163 */
164 inp->inp_log_flags = TLEF_DST_LOOPBACK | TLEF_DST_LOCAL | TLEF_DST_GW;
165 }
166 }
167 /*
168 * Check separately the destination flags that are per TCP connection
169 * and the other functional flags that are global
170 */
171 return (inp->inp_log_flags & tcp_log_enable_flags & TLEF_MASK_DST) &&
172 (tcp_log_enable_flags & (req_flags & ~TLEF_MASK_DST));
173 }
174
175 #undef IN6_IS_ADDR_V4MAPPED_LOOPBACK
176
177 static inline bool
tcp_log_summary_needed(struct tcpcb * tp)178 tcp_log_summary_needed(struct tcpcb *tp)
179 {
180 struct inpcb *inp;
181
182 if (tp == NULL || tp->t_inpcb == NULL) {
183 return false;
184 }
185 inp = tp->t_inpcb;
186
187 return (inp->inp_flags2 & INP2_LOGGING_ENABLED) ? true: false;
188 }
189
190 #define TCP_LOG_RTT_INFO(tp) if (tcp_is_log_enabled(tp, TLEF_RTT)) \
191 tcp_log_rtt_info(__func__, __LINE__, (tp))
192
193 #define TCP_LOG_RTM_RTT(tp, rt) if (tcp_is_log_enabled(tp, TLEF_RTT)) \
194 tcp_log_rt_rtt(__func__, __LINE__, (tp), (rt))
195
196 #define TCP_LOG_RTT_CHANGE(tp, old_srtt, old_rttvar) if (tcp_is_log_enabled(tp, TLEF_RTT)) \
197 tcp_log_rtt_change(__func__, __LINE__, (tp), (old_srtt), (old_rttvar))
198
199 #define TCP_LOG_KEEP_ALIVE(tp, idle_time) if (tcp_is_log_enabled(tp, TLEF_KEEP_ALIVE)) \
200 tcp_log_keepalive(__func__, __LINE__, (tp), (idle_time))
201
202 #define TCP_LOG_CONNECT(tp, outgoing, error) if (tcp_is_log_enabled(tp, TLEF_CONNECTION)) \
203 tcp_log_connection((tp), __unsafe_forge_null_terminated(const char *, ((outgoing) ? "connect outgoing" : "connect incoming")), (error))
204
205 #define TCP_LOG_CONNECTED(tp, error) if (tcp_is_log_enabled(tp, TLEF_CONNECTION)) \
206 tcp_log_connection((tp), "connected", (error))
207
208 #define TCP_LOG_LISTEN(tp, error) if (tcp_is_log_enabled(tp, TLEF_CONNECTION)) \
209 tcp_log_listen((tp), (error))
210
211 #define TCP_LOG_ACCEPT(tp, error) if (tcp_is_log_enabled(tp, TLEF_CONNECTION)) \
212 tcp_log_connection((tp), "accept", (error))
213
214 #define TCP_LOG_CONNECTION_SUMMARY(tp) if (tcp_log_summary_needed(tp)) \
215 tcp_log_connection_summary(__func__, __LINE__, (tp))
216
217 #define TCP_LOG_DROP_NECP(hdr, th, tp, outgoing) if (tcp_is_log_enabled(tp, TLEF_DROP_NECP)) \
218 tcp_log_drop_pcb((hdr), (th), (tp), (outgoing), "NECP")
219
220 #define TCP_LOG_DROP_PCB(hdr, th, tp, outgoing, reason) if (tcp_is_log_enabled(tp, TLEF_DROP_PCB)) \
221 tcp_log_drop_pcb((hdr), (th), (tp), (outgoing), reason)
222
223 #define TCP_LOG_TH_FLAGS(hdr, th, tp, outgoing, ifp) \
224 if ((th) != NULL && ((th)->th_flags & (TH_SYN|TH_FIN|TH_RST))) \
225 tcp_log_th_flags((hdr), (th), (tp), (outgoing), (ifp))
226
227 #define TCP_LOG_DROP_PKT(hdr, th, ifp, reason) \
228 if ((th) != NULL && ((th->th_flags) & (TH_SYN|TH_FIN|TH_RST)) && \
229 (tcp_log_enable_flags & TLEF_DROP_PKT)) \
230 tcp_log_drop_pkt((hdr), (th), (ifp), (reason))
231
232 #define TCP_LOG_FSW_FLOW(tp, format, ...) if (tcp_is_log_enabled(tp, TLEF_FSW_FLOW)) \
233 tcp_log_fsw_flow(__func__, __LINE__, (tp), format, ##__VA_ARGS__)
234
235 #define TCP_LOG(tp, format, ...) if (tcp_is_log_enabled(tp, TLEF_LOG)) \
236 tcp_log_message(__func__, __LINE__, tp, format, ## __VA_ARGS__)
237
238 #define TCP_LOG_STATE(tp, new_state) if (tcp_is_log_enabled(tp, TLEF_STATE)) \
239 tcp_log_state_change(__func__, __LINE__, (tp), (new_state))
240
241 #define TCP_LOG_OUTPUT(tp, format, ...) if (tcp_is_log_enabled(tp, TLEF_OUTPUT)) \
242 tcp_log_output(__func__, __LINE__, tp, format, ## __VA_ARGS__)
243
244 #define TCP_LOG_BIND(tp, error) if (tcp_is_log_enabled(tp, TLEF_BIND)) \
245 tcp_log_connection((tp), "bind", (error))
246
247 #endif /* BSD_KERNEL_PRIVATE */
248
249 #endif /* _NETINET_TCP_LOG_H_ */
250