1 /*
2 * Copyright (c) 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 #ifndef _NETINET_UDP_LOG_H_
30 #define _NETINET_UDP_LOG_H_
31
32 extern uint32_t udp_log_enable_flags;
33 extern uint32_t udp_log_port;
34
35 #define UDP_ENABLE_FLAG_LIST \
36 X(ULEF_BIND, 0x00000002, bind) \
37 X(ULEF_CONNECTION, 0x00000001, connection) \
38 X(ULEF_LOG, 0x00000008, log) \
39 X(ULEF_DST_LOOPBACK, 0x00000010, loop) \
40 X(ULEF_DST_LOCAL, 0x00000020, local) \
41 X(ULEF_DST_GW, 0x00000040, gw) \
42 X(ULEF_DROP_NECP, 0x00001000, dropnecp) \
43 X(ULEF_DROP_PCB, 0x00002000, droppcb) \
44 X(ULEF_DROP_PKT, 0x00004000, droppkt)
45
46 /*
47 * Flag values for udp_log_enable_flags
48 */
49 enum {
50 #define X(name, value, ...) name = value,
51 UDP_ENABLE_FLAG_LIST
52 #undef X
53 };
54
55 #define ULEF_MASK_DST (ULEF_DST_LOOPBACK | ULEF_DST_LOCAL | ULEF_DST_GW)
56
57 extern void udp_log_bind(struct inpcb *inp, const char *event, int error);
58 extern void udp_log_connection(struct inpcb *inp, const char *event, int error);
59 extern void udp_log_connection_summary(struct inpcb *inp);
60
61 static inline bool
udp_is_log_enabled(struct inpcb * inp,uint32_t req_flags)62 udp_is_log_enabled(struct inpcb *inp, uint32_t req_flags)
63 {
64 if (inp == NULL) {
65 return false;
66 }
67 /*
68 * First find out the kind of destination
69 */
70 if (inp->inp_log_flags == 0) {
71 if (inp->inp_vflag & INP_IPV6) {
72 if (IN6_IS_ADDR_LOOPBACK(&inp->in6p_laddr) ||
73 IN6_IS_ADDR_LOOPBACK(&inp->in6p_faddr)) {
74 inp->inp_log_flags |= ULEF_DST_LOOPBACK;
75 }
76 } else {
77 if (ntohl(inp->inp_laddr.s_addr) == INADDR_LOOPBACK ||
78 ntohl(inp->inp_faddr.s_addr) == INADDR_LOOPBACK) {
79 inp->inp_log_flags |= ULEF_DST_LOOPBACK;
80 }
81 }
82 /* We only check for loopback */
83 if (inp->inp_log_flags == 0) {
84 inp->inp_log_flags |= ULEF_DST_LOCAL | ULEF_DST_GW;
85 }
86 }
87 /*
88 * Check separately the destination flags that are per TCP connection
89 * and the other functional flags that are global
90 */
91 return (inp->inp_log_flags & udp_log_enable_flags & ULEF_MASK_DST) &&
92 (udp_log_enable_flags & (req_flags & ~ULEF_MASK_DST));
93 }
94
95 #define UDP_LOG_BIND(inp, error) if (udp_is_log_enabled(inp, ULEF_BIND)) \
96 udp_log_connection((inp), "bind", (error))
97
98 #define UDP_LOG_CONNECT(inp, error) if (udp_is_log_enabled(inp, ULEF_CONNECTION)) \
99 udp_log_connection((inp), "connect", (error))
100
101 #define UDP_LOG_CONNECTION_SUMMARY(inp) if (udp_is_log_enabled(inp, ULEF_CONNECTION)) \
102 udp_log_connection_summary((inp))
103
104
105 #endif /* _NETINET_UDP_LOG_H_ */
106