]> git.saurik.com Git - apple/configd.git/blob - nwi/network_state_information_logging.h
configd-963.tar.gz
[apple/configd.git] / nwi / network_state_information_logging.h
1 /*
2 * Copyright (c) 2017 Apple Inc. All rights reserved.
3 *
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #ifndef _NETWORK_STATE_INFORMATION_LOGGING_H
25 #define _NETWORK_STATE_INFORMATION_LOGGING_H
26
27 #include <Availability.h>
28 #include <TargetConditionals.h>
29 #include <sys/cdefs.h>
30 #include <CoreFoundation/CoreFoundation.h>
31 #include <SystemConfiguration/SystemConfiguration.h>
32 #include <SystemConfiguration/SCPrivate.h>
33
34 #include <network_information.h>
35 #include "network_state_information_priv.c"
36
37 __BEGIN_DECLS
38
39 #ifndef my_log
40 #define my_log(__level, __format, ...) SC_log(__level, __format, ## __VA_ARGS__)
41 #define MY_LOG_DEFINED_LOCALLY
42 #endif // !my_log
43
44 #ifndef my_log_context_type
45 #define my_log_context_type void *
46 #define MY_LOG_CONTEXT_TYPE_DEFINED_LOCALLY
47 #endif // !my_log_context_type
48
49 #ifndef my_log_context_name
50 #define my_log_context_name context
51 #define MY_LOG_CONTEXT_NAME_DEFINED_LOCALLY
52 #endif // !my_log_context_name
53
54 #include "SCNetworkReachabilityLogging.h"
55
56 /*
57 * _nwi_ifstate_flags_str()
58 *
59 * Returns a string representation of the nwi_state flags.
60 * "(IPv4,IPv6,DNS,...)"
61 */
62 static __inline__ void
63 _nwi_ifstate_flags_str(nwi_ifstate_flags flags, char *str, size_t len)
64 {
65 size_t n;
66 nwi_ifstate_flags remaining;
67
68 assert(len >= sizeof("(0x01234567)")); // check min buffer size
69
70 flags &= NWI_IFSTATE_FLAGS_MASK;
71 if (flags == 0) {
72 str[0] = '\0';
73 return;
74 }
75
76 len--; // leave room for the closing paren
77 n = strlcpy(str, "(", len);
78 remaining = flags;
79
80 if ((remaining & NWI_IFSTATE_FLAGS_HAS_IPV4) &&
81 (n < len) && ((len - n) > sizeof("IPv4,"))) {
82 n = strlcat(str, "IPv4,", len);
83 remaining &= ~NWI_IFSTATE_FLAGS_HAS_IPV4;
84 }
85
86 if ((remaining & NWI_IFSTATE_FLAGS_HAS_IPV6) &&
87 (n < len) && ((len - n) > sizeof("IPv6,"))) {
88 n = strlcat(str, "IPv6,", len);
89 remaining &= ~NWI_IFSTATE_FLAGS_HAS_IPV6;
90 }
91
92 if ((remaining & NWI_IFSTATE_FLAGS_HAS_DNS) &&
93 (n < len) && ((len - n) > sizeof("DNS,"))) {
94 n = strlcat(str, "DNS,", len);
95 remaining &= ~NWI_IFSTATE_FLAGS_HAS_DNS;
96 }
97
98 if ((remaining & NWI_IFSTATE_FLAGS_NOT_IN_LIST) &&
99 (n < len) && ((len - n) > sizeof("NOT-IN-LIST,"))) {
100 n = strlcat(str, "NOT-IN-LIST,", len);
101 remaining &= ~NWI_IFSTATE_FLAGS_NOT_IN_LIST;
102 }
103
104 if ((remaining & NWI_IFSTATE_FLAGS_HAS_SIGNATURE) &&
105 (n < len) && ((len - n) > sizeof("SIGNATURE,"))) {
106 n = strlcat(str, "SIGNATURE,", len);
107 remaining &= ~NWI_IFSTATE_FLAGS_HAS_SIGNATURE;
108 }
109
110 if ((remaining & NWI_IFSTATE_FLAGS_NOT_IN_IFLIST) &&
111 (n < len) && ((len - n) > sizeof("NOT-IN-IFLIST,"))) {
112 n = strlcat(str, "NOT-IN-IFLIST,", len);
113 remaining &= ~NWI_IFSTATE_FLAGS_NOT_IN_IFLIST;
114 }
115
116 if (remaining != 0) {
117 if ((n >= len) ||
118 ((len - n) <= sizeof("0x01234567,"))) {
119 // if we don't have enough space, truncate and start over
120 n = strlcpy(str, "(", len);
121 remaining = flags;
122 }
123
124 n += snprintf(str + n, len - n, ",%p", (void *)remaining);
125 }
126
127 if (n-- > 0) {
128 str[n] = ')'; // trailing "," --> ")"
129 }
130
131 return;
132 }
133
134 static __inline__ const char *
135 _nwi_ifstate_rank_str(Rank rank)
136 {
137 const char *str = "???";
138
139 switch (RANK_ASSERTION_MASK(rank)) {
140 case kRankAssertionFirst:
141 str = "First";
142 break;
143 case kRankAssertionDefault:
144 str = "Default";
145 break;
146 case kRankAssertionLast:
147 str = "Last";
148 break;
149 case kRankAssertionNever:
150 str = "Never";
151 break;
152 case kRankAssertionScoped:
153 str = "Scoped";
154 break;
155 default:
156 str = "???";
157 break;
158 }
159
160 return str;
161 }
162
163 static __inline__ void
164 _nwi_ifstate_log(nwi_ifstate_t ifstate, boolean_t debug, my_log_context_type my_log_context_name)
165 {
166 #if defined(MY_LOG_CONTEXT_TYPE_DEFINED_LOCALLY) && defined(MY_LOG_CONTEXT_NAME_DEFINED_LOCALLY)
167 #pragma unused(my_log_context_name)
168 #endif
169 union {
170 const void *bytes;
171 const struct in_addr *ia;
172 const struct in6_addr *ia6;
173 } addr;
174 char addr_str[INET6_ADDRSTRLEN];
175 nwi_ifstate_flags flags_ifstate;
176 char flags_str[100];
177 const char *if_name;
178 SCNetworkReachabilityFlags reach_flags;
179 char reach_str[100];
180 const struct sockaddr *vpn_addr;
181
182 // nwi_ifstate flags
183 flags_ifstate = nwi_ifstate_get_flags(ifstate);
184 if (debug) {
185 flags_ifstate |= NWI_IFSTATE_FLAGS(ifstate->flags);
186 }
187 flags_ifstate &= ~NWI_IFSTATE_FLAGS_HAS_SIGNATURE; // exclude flag ('cause we'll report the signature only if present)
188 _nwi_ifstate_flags_str(flags_ifstate, flags_str, sizeof(flags_str));
189
190 // if_name
191 if_name = nwi_ifstate_get_ifname(ifstate);
192
193 // reachability flags
194 reach_flags = nwi_ifstate_get_reachability_flags(ifstate);
195
196 // IP address
197 addr.bytes = nwi_ifstate_get_address(ifstate);
198 if (inet_ntop(ifstate->af, addr.bytes, addr_str, sizeof(addr_str)) == NULL) {
199 strlcpy(addr_str, "???", sizeof(addr_str));
200 }
201
202 // verbose format (e.g. scutil)
203 my_log(LOG_INFO, " %7s : flags : %p %s",
204 if_name,
205 (void *)flags_ifstate,
206 flags_str);
207
208 my_log(LOG_INFO, " address : %s", addr_str);
209
210 // VPN server address
211 vpn_addr = nwi_ifstate_get_vpn_server(ifstate);
212 if (vpn_addr != NULL) {
213 char vpn_str[INET6_ADDRSTRLEN];
214
215 _SC_sockaddr_to_string(vpn_addr, vpn_str, sizeof(vpn_str));
216 my_log(LOG_INFO, " VPN server : %s", vpn_str);
217 }
218
219 // reachability
220 __SCNetworkReachability_flags_string(reach_flags, TRUE, reach_str, sizeof(reach_str));
221 my_log(LOG_INFO, " reach : %s", reach_str);
222
223 if (debug) {
224 uint64_t generation;
225 Rank rank;
226 uint32_t rank_index;
227 const char *rank_str;
228 const uint8_t *signature;
229 int signature_length;
230
231 // Rank
232 rank = ifstate->rank;
233 rank_str = _nwi_ifstate_rank_str(rank);
234 rank_index = RANK_INDEX_MASK(rank);
235 if (rank_index != kRankIndexMask) {
236 my_log(LOG_INFO, " rank : 0x%08x (%s, %u)", rank, rank_str, rank_index);
237 } else {
238 my_log(LOG_INFO, " rank : 0x%08x (%s, Last)", rank, rank_str);
239 }
240
241 // signature
242 signature = nwi_ifstate_get_signature(ifstate, AF_UNSPEC, &signature_length);
243 if (signature != NULL) {
244 CFDataRef digest;
245
246 digest = CFDataCreate(NULL, signature, CC_SHA1_DIGEST_LENGTH);
247 my_log(LOG_INFO, " signature : %@", digest);
248 CFRelease(digest);
249 }
250
251 // generation
252 generation = nwi_ifstate_get_generation(ifstate);
253 my_log(LOG_INFO, " generation : %llu", generation);
254 }
255
256 return;
257 }
258
259 static __inline__ void
260 _nwi_state_reachability_log(nwi_state_t state, boolean_t debug, int af, my_log_context_type my_log_context_name)
261 {
262 #if defined(MY_LOG_CONTEXT_TYPE_DEFINED_LOCALLY) && defined(MY_LOG_CONTEXT_NAME_DEFINED_LOCALLY)
263 #pragma unused(my_log_context_name)
264 #endif
265 uint32_t flags;
266 char flags_str[100];
267
268 flags = nwi_state_get_reachability_flags(state, af);
269 __SCNetworkReachability_flags_string(flags, TRUE, flags_str, sizeof(flags_str));
270 if (!debug) {
271 my_log(LOG_INFO, "%s", "");
272 }
273 my_log(LOG_INFO, " REACH : flags %s", flags_str);
274
275 return;
276 }
277
278 static __inline__ void
279 _nwi_state_log(nwi_state_t state, boolean_t debug, my_log_context_type my_log_context_name)
280 {
281 #if defined(MY_LOG_CONTEXT_TYPE_DEFINED_LOCALLY) && defined(MY_LOG_CONTEXT_NAME_DEFINED_LOCALLY)
282 #pragma unused(my_log_context_name)
283 #endif
284 unsigned int count;
285 nwi_ifindex_t i;
286 nwi_ifstate_t ifstate;
287
288 if (!debug) {
289 my_log(LOG_INFO, "%s", "Network information");
290 } else {
291 my_log(LOG_INFO,
292 "Network information (generation %llu size=%lu)",
293 nwi_state_get_generation(state),
294 nwi_state_size(state));
295 }
296
297 // report IPv4 state
298 if (!debug) {
299 // show regular interfaces
300 my_log(LOG_INFO, "%s", "");
301 my_log(LOG_INFO, "%s", "IPv4 network interface information");
302 ifstate = nwi_state_get_first_ifstate(state, AF_INET);
303 if (ifstate == NULL) {
304 my_log(LOG_INFO, "%s", " No IPv4 states found");
305 } else {
306 while (ifstate != NULL) {
307 _nwi_ifstate_log(ifstate, debug, my_log_context_name);
308 ifstate = nwi_ifstate_get_next(ifstate, AF_INET);
309 }
310 }
311 } else {
312 my_log(LOG_INFO, "%s", "IPv4 network interface information");
313 if (state->ipv4_count > 0) {
314 // show ALL interfaces
315 for (i = 0, ifstate = nwi_state_ifstate_list(state, AF_INET);
316 i < state->ipv4_count; i++, ifstate++) {
317 _nwi_ifstate_log(ifstate, debug, my_log_context_name);
318 }
319 } else {
320 my_log(LOG_INFO, "%s", " No IPv4 states found");
321 }
322 }
323 _nwi_state_reachability_log(state, debug, AF_INET, my_log_context_name);
324
325 // report IPv6 state
326 if (!debug) {
327 // show regular interfaces
328 my_log(LOG_INFO, "%s", "");
329 my_log(LOG_INFO, "%s", "IPv6 network interface information");
330 ifstate = nwi_state_get_first_ifstate(state, AF_INET6);
331 if (ifstate == NULL) {
332 my_log(LOG_INFO, "%s", " No IPv6 states found\n");
333 } else {
334 while (ifstate != NULL) {
335 _nwi_ifstate_log(ifstate, debug, my_log_context_name);
336 ifstate = nwi_ifstate_get_next(ifstate, AF_INET6);
337 }
338 }
339 } else {
340 my_log(LOG_INFO, "%s", "IPv6 network interface information");
341 if (state->ipv6_count > 0) {
342 // show ALL interfaces
343 for (i = 0, ifstate = nwi_state_ifstate_list(state, AF_INET6);
344 i < state->ipv6_count; i++, ifstate++) {
345 _nwi_ifstate_log(ifstate, debug, my_log_context_name);
346 }
347 } else {
348 my_log(LOG_INFO, "%s", " No IPv6 states found\n");
349 }
350 }
351 _nwi_state_reachability_log(state, debug, AF_INET6, my_log_context_name);
352
353 count = nwi_state_get_interface_names(state, NULL, 0);
354 if (count > 0) {
355 const char *names[count];
356
357 count = nwi_state_get_interface_names(state, names, count);
358 if (count > 0) {
359 char str[count * (IFNAMSIZ + 1)];
360
361 memset(str, 0, sizeof(str));
362 for (unsigned int i = 0; i < count; i++) {
363 if (i != 0) {
364 strlcat(str, " ", sizeof(str));
365 }
366 strlcat(str, names[i], sizeof(str));
367 }
368
369 if (!debug) {
370 my_log(LOG_INFO, "%s", "");
371 }
372 my_log(LOG_INFO, "Network interfaces: %s", str);
373 }
374 }
375
376 return;
377 }
378
379 #ifdef MY_LOG_DEFINED_LOCALLY
380 #undef my_log
381 #undef MY_LOG_DEFINED_LOCALLY
382 #endif // MY_LOG_DEFINED_LOCALLY
383
384 #ifdef MY_LOG_CONTEXT_TYPE_DEFINED_LOCALLY
385 #undef my_log_context_type
386 #undef MY_LOG_CONTEXT_TYPE_DEFINED_LOCALLY
387 #endif // MY_LOG_CONTEXT_TYPE_DEFINED_LOCALLY
388
389 #ifdef MY_LOG_CONTEXT_NAME_DEFINED_LOCALLY
390 #undef my_log_context_name
391 #undef MY_LOG_CONTEXT_NAME_DEFINED_LOCALLY
392 #endif // MY_LOG_CONTEXT_NAME_DEFINED_LOCALLY
393
394 __END_DECLS
395
396 #endif // _NETWORK_STATE_INFORMATION_LOGGING_H