-
Notifications
You must be signed in to change notification settings - Fork 352
Expand file tree
/
Copy pathudp_log.c
More file actions
451 lines (368 loc) · 12.4 KB
/
udp_log.c
File metadata and controls
451 lines (368 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/*
* Copyright (c) 2023 Apple Inc. All rights reserved.
*
* @APPLE_OSREFERENCE_LICENSE_HEADER_START@
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. The rights granted to you under the License
* may not be used to create, or enable the creation or redistribution of,
* unlawful or unlicensed copies of an Apple operating system, or to
* circumvent, violate, or enable the circumvention or violation of, any
* terms of an Apple operating system software license agreement.
*
* Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPLE_OSREFERENCE_LICENSE_HEADER_END@
*/
#include <sys/param.h>
#include <sys/protosw.h>
#include <sys/systm.h>
#include <sys/sysctl.h>
#include <kern/bits.h>
#include <netinet/ip.h>
#include <netinet/ip6.h>
#include <netinet/inp_log.h>
#include <netinet/in_pcb.h>
#include <netinet/udp_log.h>
#include <netinet/udp_var.h>
SYSCTL_NODE(_net_inet_udp, OID_AUTO, log, CTLFLAG_RW | CTLFLAG_LOCKED, 0,
"UDP");
#if (DEVELOPMENT || DEBUG)
#define UDP_LOG_ENABLE_DEFAULT \
(ULEF_CONNECTION | ULEF_DST_LOCAL | ULEF_DST_GW)
#else /* (DEVELOPMENT || DEBUG) */
#define UDP_LOG_ENABLE_DEFAULT 0
#endif /* (DEVELOPMENT || DEBUG) */
uint32_t udp_log_enable_flags = UDP_LOG_ENABLE_DEFAULT;
SYSCTL_UINT(_net_inet_udp_log, OID_AUTO, enable,
CTLFLAG_RW | CTLFLAG_LOCKED, &udp_log_enable_flags, 0, "");
/*
* The following is a help to describe the values of the flags
*/
#define X(name, value, description, ...) #description ":" #value " "
SYSCTL_STRING(_net_inet_udp_log, OID_AUTO, enable_usage, CTLFLAG_RD | CTLFLAG_LOCKED,
UDP_ENABLE_FLAG_LIST, 0, "");
#undef X
/*
*
*/
static int sysctl_udp_log_port SYSCTL_HANDLER_ARGS;
uint16_t udp_log_local_port_included = 0;
SYSCTL_PROC(_net_inet_udp_log, OID_AUTO, local_port_included,
CTLFLAG_RW | CTLFLAG_LOCKED,
&udp_log_local_port_included, 0, &sysctl_udp_log_port, "UI", "");
uint16_t udp_log_remote_port_included = 0;
SYSCTL_PROC(_net_inet_udp_log, OID_AUTO, remote_port_included,
CTLFLAG_RW | CTLFLAG_LOCKED,
&udp_log_remote_port_included, 0, &sysctl_udp_log_port, "UI", "");
uint16_t udp_log_local_port_excluded = 0;
SYSCTL_PROC(_net_inet_udp_log, OID_AUTO, local_port_excluded,
CTLFLAG_RW | CTLFLAG_LOCKED,
&udp_log_local_port_excluded, 0, &sysctl_udp_log_port, "UI", "");
uint16_t udp_log_remote_port_excluded = 0;
SYSCTL_PROC(_net_inet_udp_log, OID_AUTO, remote_port_excluded,
CTLFLAG_RW | CTLFLAG_LOCKED,
&udp_log_remote_port_excluded, 0, &sysctl_udp_log_port, "UI", "");
#define UDP_LOG_RATE_LIMIT 1000
static unsigned int udp_log_rate_limit = UDP_LOG_RATE_LIMIT;
SYSCTL_UINT(_net_inet_udp_log, OID_AUTO, rate_limit,
CTLFLAG_RW | CTLFLAG_LOCKED, &udp_log_rate_limit, 0, "");
/* 1 minute by default */
#define UDP_LOG_RATE_DURATION 60
static unsigned int udp_log_rate_duration = UDP_LOG_RATE_DURATION;
SYSCTL_UINT(_net_inet_udp_log, OID_AUTO, rate_duration,
CTLFLAG_RW | CTLFLAG_LOCKED, &udp_log_rate_duration, 0, "");
static unsigned long udp_log_rate_max = 0;
SYSCTL_ULONG(_net_inet_udp_log, OID_AUTO, rate_max,
CTLFLAG_RD | CTLFLAG_LOCKED, &udp_log_rate_max, "");
static unsigned long udp_log_rate_exceeded_total = 0;
SYSCTL_ULONG(_net_inet_udp_log, OID_AUTO, rate_exceeded_total,
CTLFLAG_RD | CTLFLAG_LOCKED, &udp_log_rate_exceeded_total, "");
static unsigned long udp_log_rate_current = 0;
SYSCTL_ULONG(_net_inet_udp_log, OID_AUTO, rate_current,
CTLFLAG_RD | CTLFLAG_LOCKED, &udp_log_rate_current, "");
static bool udp_log_rate_exceeded_logged = false;
static uint64_t udp_log_current_period = 0;
#define ADDRESS_STR_LEN (MAX_IPv6_STR_LEN + 6)
#define UDP_LOG_COMMON_FMT \
"[%s:%u<->%s:%u] " \
"interface: %s " \
"(skipped: %lu)\n"
#define UDP_LOG_COMMON_ARGS \
laddr_buf, ntohs(local_port), faddr_buf, ntohs(foreign_port), \
ifp != NULL ? if_name(ifp) : "", \
udp_log_rate_exceeded_total
#define UDP_LOG_COMMON_PCB_FMT \
UDP_LOG_COMMON_FMT \
"so_gencnt: %llu " \
"so_state: 0x%04x " \
"process: %s:%u "
#define UDP_LOG_COMMON_PCB_ARGS \
UDP_LOG_COMMON_ARGS, \
so != NULL ? so->so_gencnt : 0, \
so != NULL ? so->so_state : 0, \
inp->inp_last_proc_name, so->last_pid
/*
* Returns true when above the rate limit
*/
static bool
udp_log_is_rate_limited(void)
{
uint64_t current_net_period = net_uptime();
/* When set to zero it means to reset to default */
if (udp_log_rate_duration == 0) {
udp_log_rate_duration = UDP_LOG_RATE_DURATION;
}
if (udp_log_rate_limit == 0) {
udp_log_rate_duration = UDP_LOG_RATE_LIMIT;
}
if (current_net_period > udp_log_current_period + udp_log_rate_duration) {
if (udp_log_rate_current > udp_log_rate_max) {
udp_log_rate_max = udp_log_rate_current;
}
udp_log_current_period = current_net_period;
udp_log_rate_current = 0;
udp_log_rate_exceeded_logged = false;
}
udp_log_rate_current += 1;
if (udp_log_rate_current > (unsigned long) udp_log_rate_limit) {
udp_log_rate_exceeded_total += 1;
return true;
}
return false;
}
static bool
udp_log_port_allowed(struct inpcb *inp)
{
if (ntohs(inp->inp_lport) == udp_log_local_port_included ||
ntohs(inp->inp_fport) == udp_log_remote_port_included) {
return true;
} else {
return false;
}
if (ntohs(inp->inp_lport) == udp_log_local_port_excluded ||
ntohs(inp->inp_fport) == udp_log_remote_port_excluded) {
return false;
}
return true;
}
__attribute__((noinline))
void
udp_log_bind(struct inpcb *inp, const char *event, int error)
{
struct socket *so;
struct ifnet *ifp;
char laddr_buf[ADDRESS_STR_LEN];
char faddr_buf[ADDRESS_STR_LEN];
in_port_t local_port;
in_port_t foreign_port;
if (inp == NULL || inp->inp_socket == NULL || event == NULL) {
return;
}
/* Do not log too much */
if (udp_log_is_rate_limited()) {
return;
}
if (!udp_log_port_allowed(inp)) {
return;
}
inp->inp_flags2 &= ~INP2_LOGGED_SUMMARY;
inp->inp_flags2 |= INP2_LOGGING_ENABLED;
so = inp->inp_socket;
local_port = inp->inp_lport;
foreign_port = inp->inp_fport;
ifp = inp->inp_last_outifp != NULL ? inp->inp_last_outifp :
inp->inp_boundifp != NULL ? inp->inp_boundifp : NULL;
inp_log_addresses(inp, laddr_buf, sizeof(laddr_buf), faddr_buf, sizeof(faddr_buf));
#define UDP_LOG_BIND_FMT \
"udp %s: " \
UDP_LOG_COMMON_PCB_FMT \
"bytes in/out: %llu/%llu " \
"pkts in/out: %llu/%llu " \
"error: %d " \
"so_error: %d " \
"svc/tc: %u"
#define UDP_LOG_BIND_ARGS \
event, \
UDP_LOG_COMMON_PCB_ARGS, \
inp->inp_stat->rxbytes, inp->inp_stat->txbytes, \
inp->inp_stat->rxpackets, inp->inp_stat->txpackets, \
error, \
so->so_error, \
(so->so_flags1 & SOF1_TC_NET_SERV_TYPE) ? so->so_netsvctype : so->so_traffic_class
os_log(OS_LOG_DEFAULT, UDP_LOG_BIND_FMT,
UDP_LOG_BIND_ARGS);
#undef UDP_LOG_BIND_FMT
#undef UDP_LOG_BIND_ARGS
}
__attribute__((noinline))
void
udp_log_connection(struct inpcb *inp, const char *event, int error)
{
struct socket *so;
struct ifnet *ifp;
char laddr_buf[ADDRESS_STR_LEN];
char faddr_buf[ADDRESS_STR_LEN];
in_port_t local_port;
in_port_t foreign_port;
if (inp == NULL || inp->inp_socket == NULL || event == NULL) {
return;
}
/* Do not log too much */
if (udp_log_is_rate_limited()) {
return;
}
if (!udp_log_port_allowed(inp)) {
return;
}
/* Clear the logging flags as one may reconnect an UDP socket */
inp->inp_flags2 &= ~INP2_LOGGED_SUMMARY;
inp->inp_flags2 |= INP2_LOGGING_ENABLED;
so = inp->inp_socket;
local_port = inp->inp_lport;
foreign_port = inp->inp_fport;
ifp = inp->inp_last_outifp != NULL ? inp->inp_last_outifp :
inp->inp_boundifp != NULL ? inp->inp_boundifp : NULL;
inp_log_addresses(inp, laddr_buf, sizeof(laddr_buf), faddr_buf, sizeof(faddr_buf));
#define UDP_LOG_CONNECT_FMT \
"udp %s: " \
UDP_LOG_COMMON_PCB_FMT \
"bytes in/out: %llu/%llu " \
"pkts in/out: %llu/%llu " \
"error: %d " \
"so_error: %d " \
"svc/tc: %u " \
"flow: 0x%x"
#define UDP_LOG_CONNECT_ARGS \
event, \
UDP_LOG_COMMON_PCB_ARGS, \
inp->inp_stat->rxbytes, inp->inp_stat->txbytes, \
inp->inp_stat->rxpackets, inp->inp_stat->txpackets, \
error, \
so->so_error, \
(so->so_flags1 & SOF1_TC_NET_SERV_TYPE) ? so->so_netsvctype : so->so_traffic_class, \
inp->inp_flowhash
os_log(OS_LOG_DEFAULT, UDP_LOG_CONNECT_FMT,
UDP_LOG_CONNECT_ARGS);
#undef UDP_LOG_CONNECT_FMT
#undef UDP_LOG_CONNECT_ARGS
}
__attribute__((noinline))
void
udp_log_connection_summary(struct inpcb *inp)
{
struct socket *so;
struct ifnet *ifp;
clock_sec_t duration_secs = 0;
clock_usec_t duration_microsecs = 0;
clock_sec_t connection_secs = 0;
clock_usec_t connection_microsecs = 0;
char laddr_buf[ADDRESS_STR_LEN];
char faddr_buf[ADDRESS_STR_LEN];
in_port_t local_port;
in_port_t foreign_port;
if (inp == NULL || inp->inp_socket == NULL) {
return;
}
if ((inp->inp_flags2 & INP2_LOGGING_ENABLED) == 0) {
return;
}
/* Make sure the summary is logged once */
if (inp->inp_flags2 & INP2_LOGGED_SUMMARY) {
return;
}
inp->inp_flags2 |= INP2_LOGGED_SUMMARY;
inp->inp_flags2 &= ~INP2_LOGGING_ENABLED;
/* Do not log too much */
if (udp_log_is_rate_limited()) {
return;
}
so = inp->inp_socket;
local_port = inp->inp_lport;
foreign_port = inp->inp_fport;
/*
* inp_start_timestamp is when the UDP socket was open.
*
* inp_connect_timestamp is when the connection started on
*/
uint64_t now = mach_continuous_time();
if (inp->inp_start_timestamp > 0) {
uint64_t duration = now - inp->inp_start_timestamp;
absolutetime_to_microtime(duration, &duration_secs, &duration_microsecs);
}
if (inp->inp_connect_timestamp > 0) {
uint64_t duration = now - inp->inp_connect_timestamp;
absolutetime_to_microtime(duration, &connection_secs, &connection_microsecs);
}
ifp = inp->inp_last_outifp != NULL ? inp->inp_last_outifp :
inp->inp_boundifp != NULL ? inp->inp_boundifp : NULL;
inp_log_addresses(inp, laddr_buf, sizeof(laddr_buf), faddr_buf, sizeof(faddr_buf));
/*
* Need to use 2 log messages because the size of the summary
*/
#define UDP_LOG_CONNECTION_SUMMARY_FMT \
"udp_connection_summary " \
UDP_LOG_COMMON_PCB_FMT \
"Duration: %lu.%03d sec " \
"Conn_Time: %lu.%03d sec " \
"bytes in/out: %llu/%llu " \
"pkts in/out: %llu/%llu " \
"rxnospace pkts/bytes: %llu/%llu " \
"so_error: %d " \
"svc/tc: %u " \
"flow: 0x%x"
#define UDP_LOG_CONNECTION_SUMMARY_ARGS \
UDP_LOG_COMMON_PCB_ARGS, \
duration_secs, duration_microsecs / 1000, \
connection_secs, connection_microsecs / 1000, \
inp->inp_stat->rxbytes, inp->inp_stat->txbytes, \
inp->inp_stat->rxpackets, inp->inp_stat->txpackets, \
so->so_tc_stats[SO_STATS_SBNOSPACE].rxpackets, \
so->so_tc_stats[SO_STATS_SBNOSPACE].rxbytes, \
so->so_error, \
(so->so_flags1 & SOF1_TC_NET_SERV_TYPE) ? so->so_netsvctype : so->so_traffic_class, \
inp->inp_flowhash
os_log(OS_LOG_DEFAULT, UDP_LOG_CONNECTION_SUMMARY_FMT,
UDP_LOG_CONNECTION_SUMMARY_ARGS);
#undef UDP_LOG_CONNECTION_SUMMARY_FMT
#undef UDP_LOG_CONNECTION_SUMMARY_ARGS
#define UDP_LOG_CONNECTION_SUMMARY_FMT \
"udp_connection_summary " \
UDP_LOG_COMMON_PCB_FMT \
"flowctl: %lluus (%llux) "
#define UDP_LOG_CONNECTION_SUMMARY_ARGS \
UDP_LOG_COMMON_PCB_ARGS, \
inp->inp_fadv_total_time, \
inp->inp_fadv_cnt
os_log(OS_LOG_DEFAULT, UDP_LOG_CONNECTION_SUMMARY_FMT,
UDP_LOG_CONNECTION_SUMMARY_ARGS);
#undef UDP_LOG_CONNECTION_SUMMARY_FMT
#undef UDP_LOG_CONNECTION_SUMMARY_ARGS
}
static int
sysctl_udp_log_port SYSCTL_HANDLER_ARGS
{
#pragma unused(arg1, arg2)
int error;
int new_value = *(int *)oidp->oid_arg1;
error = sysctl_handle_int(oidp, &new_value, 0, req);
if (error != 0) {
return error;
}
if (new_value < 0 || new_value > UINT16_MAX) {
return EINVAL;
}
*(uint16_t *)oidp->oid_arg1 = (uint16_t)new_value;
return 0;
}