]>
Commit | Line | Data |
---|---|---|
cb323159 A |
1 | /* |
2 | * Copyright (c) 2018-2019 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 BSD_KERNEL_PRIVATE | |
34 | ||
35 | #include <sys/param.h> | |
36 | #include <sys/systm.h> | |
37 | ||
38 | #include <netinet/in.h> | |
39 | #include <netinet/in_systm.h> | |
40 | #include <netinet/in_pcb.h> | |
41 | #if INET6 | |
42 | #include <netinet6/in6_pcb.h> | |
43 | #endif | |
44 | ||
45 | #include <netinet/tcp.h> | |
46 | #include <netinet/tcp_var.h> | |
47 | #if INET6 | |
48 | #include <netinet6/tcp6_var.h> | |
49 | #endif | |
50 | ||
51 | #include <net/net_log_common.h> | |
52 | ||
53 | #include <os/log.h> | |
54 | ||
55 | #include <stdbool.h> | |
56 | ||
57 | extern os_log_t tcp_mpkl_log_object; | |
58 | extern uint32_t tcp_log_enable_flags; | |
59 | extern uint32_t tcp_log_port; | |
60 | extern int tcp_log_privacy; | |
61 | ||
62 | #define TCP_ENABLE_FLAG_LIST \ | |
63 | X(TLEF_CONNECTION, 0x1, connection) \ | |
64 | X(TLEF_RTT, 0x2, rtt) \ | |
65 | X(TLEF_KEEP_ALIVE, 0x4, ka) \ | |
66 | X(TLEF_DST_LOOPBACK, 0x10, loop) \ | |
67 | X(TLEF_DST_LOCAL, 0x20, local) \ | |
68 | X(TLEF_DST_GW, 0x40, gw) \ | |
69 | X(TLEF_THF_SYN, 0x100, syn) \ | |
70 | X(TLEF_THF_FIN, 0x200, fin) \ | |
71 | X(TLEF_THF_RST, 0x400, rst) \ | |
72 | X(TLEF_DROP_NECP, 0x1000, dropnecp) \ | |
73 | X(TLEF_DROP_PCB, 0x2000, droppcb) \ | |
74 | X(TLEF_DROP_PKT, 0x4000, droppkt) \ | |
75 | ||
76 | /* | |
77 | * Flag values for tcp_log_enabled | |
78 | */ | |
79 | enum { | |
80 | #define X(name, value, ...) name = value, | |
81 | TCP_ENABLE_FLAG_LIST | |
82 | #undef X | |
83 | }; | |
84 | ||
85 | #define TLEF_MASK_DST (TLEF_DST_LOOPBACK | TLEF_DST_LOCAL | TLEF_DST_GW) | |
86 | ||
87 | #define TLEF_MASK_THF (TLEF_THF_SYN | TLEF_THF_FIN | TLEF_THF_RST) | |
88 | ||
89 | extern void tcp_log_connection_summary(struct tcpcb *tp); | |
90 | extern void tcp_log_th_flags(void *hdr, struct tcphdr *th, struct tcpcb *tp, bool outgoing, struct ifnet *ifp); | |
91 | extern void tcp_log_connection(struct tcpcb *tp, const char *event, int error); | |
92 | extern void tcp_log_listen(struct tcpcb *tp, int error); | |
93 | extern void tcp_log_drop_pcb(void *hdr, struct tcphdr *th, struct tcpcb *tp, bool outgoing, const char *reason); | |
94 | extern void tcp_log_drop_pkt(void *hdr, struct tcphdr *th, struct ifnet *ifp, const char *reason); | |
95 | extern void tcp_log_rtt_info(const char *func_name, int line_no, struct tcpcb *tp); | |
96 | extern void tcp_log_rt_rtt(const char *func_name, int line_no, struct tcpcb *tp, struct rtentry *rt); | |
97 | extern void tcp_log_rtt_change(const char *func_name, int line_no, struct tcpcb *tp, int old_srtt, int old_rttvar); | |
98 | extern void tcp_log_keepalive(const char *func_name, int line_no, struct tcpcb *tp, int32_t idle_time); | |
99 | extern void tcp_log_message(const char *func_name, int line_no, struct tcpcb *tp, const char *format, ...); | |
100 | ||
101 | ||
102 | static inline bool | |
103 | tcp_is_log_enabled(struct tcpcb *tp, uint32_t req_flags) | |
104 | { | |
105 | if (tp == NULL || tp->t_inpcb == NULL) { | |
106 | return false; | |
107 | } | |
108 | if (tcp_log_port > 0 && tcp_log_port <= IPPORT_HILASTAUTO) { | |
109 | if (ntohs(tp->t_inpcb->inp_lport) != tcp_log_port && | |
110 | ntohs(tp->t_inpcb->inp_fport) != tcp_log_port) { | |
111 | return false; | |
112 | } | |
113 | } | |
114 | /* | |
115 | * First find out the kind of destination | |
116 | */ | |
117 | if (tp->t_log_flags == 0) { | |
118 | if (tp->t_inpcb->inp_vflag & INP_IPV6) { | |
119 | if (IN6_IS_ADDR_LOOPBACK(&tp->t_inpcb->in6p_laddr) || | |
120 | IN6_IS_ADDR_LOOPBACK(&tp->t_inpcb->in6p_faddr)) { | |
121 | tp->t_log_flags |= TLEF_DST_LOOPBACK; | |
122 | } | |
123 | } else { | |
124 | if (ntohl(tp->t_inpcb->inp_laddr.s_addr) == INADDR_LOOPBACK || | |
125 | ntohl(tp->t_inpcb->inp_faddr.s_addr) == INADDR_LOOPBACK) { | |
126 | tp->t_log_flags |= TLEF_DST_LOOPBACK; | |
127 | } | |
128 | } | |
129 | if (tp->t_log_flags == 0) { | |
130 | if (tp->t_flags & TF_LOCAL) { | |
131 | tp->t_log_flags |= TLEF_DST_LOCAL; | |
132 | } else { | |
133 | tp->t_log_flags |= TLEF_DST_GW; | |
134 | } | |
135 | } | |
136 | } | |
137 | /* | |
138 | * Check separately the destination flags that are per TCP connection | |
139 | * and the other functional flags that are global | |
140 | */ | |
141 | return (tp->t_log_flags & tcp_log_enable_flags & TLEF_MASK_DST) && | |
142 | (tcp_log_enable_flags & (req_flags & ~TLEF_MASK_DST)); | |
143 | } | |
144 | ||
145 | #define TCP_LOG_RTT_INFO(tp) if (tcp_is_log_enabled(tp, TLEF_RTT)) \ | |
146 | tcp_log_rtt_info(__func__, __LINE__, (tp)) | |
147 | ||
148 | #define TCP_LOG_RTM_RTT(tp, rt) if (tcp_is_log_enabled(tp, TLEF_RTT)) \ | |
149 | tcp_log_rt_rtt(__func__, __LINE__, (tp), (rt)) | |
150 | ||
151 | #define TCP_LOG_RTT_CHANGE(tp, old_srtt, old_rttvar) if (tcp_is_log_enabled(tp, TLEF_RTT)) \ | |
152 | tcp_log_rtt_change(__func__, __LINE__, (tp), (old_srtt), (old_rttvar)) | |
153 | ||
154 | #define TCP_LOG_KEEP_ALIVE(tp, idle_time) if (tcp_is_log_enabled(tp, TLEF_KEEP_ALIVE)) \ | |
155 | tcp_log_keepalive(__func__, __LINE__, (tp), (idle_time)) | |
156 | ||
157 | #define TCP_LOG_CONNECT(tp, outgoing, error) if (tcp_is_log_enabled(tp, TLEF_CONNECTION)) \ | |
158 | tcp_log_connection((tp), (outgoing) ? "connect outgoing" : "connect incoming", (error)) | |
159 | ||
160 | #define TCP_LOG_LISTEN(tp, error) if (tcp_is_log_enabled(tp, TLEF_CONNECTION)) \ | |
161 | tcp_log_listen((tp), (error)) | |
162 | ||
163 | #define TCP_LOG_ACCEPT(tp, error) if (tcp_is_log_enabled(tp, TLEF_CONNECTION)) \ | |
164 | tcp_log_connection((tp), "accept", (error)) | |
165 | ||
166 | #define TCP_LOG_CONNECTION_SUMMARY(tp) if (tcp_is_log_enabled(tp, TLEF_CONNECTION)) \ | |
167 | tcp_log_connection_summary((tp)) | |
168 | ||
169 | #define TCP_LOG_DROP_NECP(hdr, th, tp, outgoing) if (tcp_is_log_enabled(tp, TLEF_DROP_NECP)) \ | |
170 | tcp_log_drop_pcb((hdr), (th), (tp), (outgoing), "NECP") | |
171 | ||
172 | #define TCP_LOG_DROP_PCB(hdr, th, tp, outgoing, reason) if (tcp_is_log_enabled(tp, TLEF_DROP_PCB)) \ | |
173 | tcp_log_drop_pcb((hdr), (th), (tp), (outgoing), reason) | |
174 | ||
175 | #define TCP_LOG_TH_FLAGS(hdr, th, tp, outgoing, ifp) \ | |
176 | if ((th) != NULL && ((th)->th_flags & (TH_SYN|TH_FIN|TH_RST))) \ | |
177 | tcp_log_th_flags((hdr), (th), (tp), (outgoing), (ifp)) | |
178 | ||
179 | #define TCP_LOG_DROP_PKT(hdr, th, ifp, reason) \ | |
180 | if ((th) != NULL && ((th->th_flags) & (TH_SYN|TH_FIN|TH_RST)) && \ | |
181 | (tcp_log_enable_flags & TLEF_DROP_PKT)) \ | |
182 | tcp_log_drop_pkt((hdr), (th), (ifp), (reason)) | |
183 | ||
184 | #define TCP_LOG(tp, format, ...) \ | |
185 | tcp_log_message(__func__, __LINE__, tp, format, ## __VA_ARGS__) | |
186 | ||
187 | ||
188 | ||
189 | #endif /* BSD_KERNEL_RPIVATE */ | |
190 | ||
191 | #endif /* _NETINET_TCP_LOG_H_ */ |