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