xref: /xnu-11417.121.6/bsd/netinet/udp_log.h (revision a1e26a70f38d1d7daa7b49b258e2f8538ad81650)
1 /*
2  * Copyright (c) 2023-2024 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_CONNECT,         0x00000001, connect)    \
36 	X(ULEF_BIND,            0x00000002, bind)       \
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 #include <netinet/udp.h>
60 
61 extern uint32_t udp_log_enable_flags;
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, int error);
66 extern void udp_log_connect(struct inpcb *inp, int error);
67 extern void udp_log_connection_summary(struct inpcb *inp);
68 extern void udp_log_message(const char *func_name, int line_no, struct inpcb *inp, const char *format, ...) __printflike(4, 5);
69 extern void udp_log_drop_pcb(void *hdr, struct udphdr *uh, struct inpcb *inp, bool outgoing, const char *format);
70 
71 static inline bool
udp_is_log_enabled(struct inpcb * inp,uint32_t req_flags)72 udp_is_log_enabled(struct inpcb *inp, uint32_t req_flags)
73 {
74 	if (inp == NULL) {
75 		return false;
76 	}
77 	/*
78 	 * First find out the kind of destination
79 	 */
80 	if (inp->inp_log_flags == 0) {
81 		if (inp->inp_vflag & INP_IPV6) {
82 			if (IN6_IS_ADDR_LOOPBACK(&inp->in6p_laddr) ||
83 			    IN6_IS_ADDR_LOOPBACK(&inp->in6p_faddr)) {
84 				inp->inp_log_flags |= ULEF_DST_LOOPBACK;
85 			}
86 		} else {
87 			if (ntohl(inp->inp_laddr.s_addr) == INADDR_LOOPBACK ||
88 			    ntohl(inp->inp_faddr.s_addr) == INADDR_LOOPBACK) {
89 				inp->inp_log_flags |= ULEF_DST_LOOPBACK;
90 			}
91 		}
92 		/* We only check for loopback */
93 		if (inp->inp_log_flags == 0) {
94 			inp->inp_log_flags = ULEF_DST_LOCAL | ULEF_DST_GW;
95 		}
96 	}
97 	/*
98 	 * Check separately the destination flags that are per TCP connection
99 	 * and the other functional flags that are global
100 	 */
101 	if ((inp->inp_log_flags & udp_log_enable_flags & ULEF_MASK_DST) &&
102 	    (udp_log_enable_flags & (req_flags & ~ULEF_MASK_DST))) {
103 		return true;
104 	}
105 	return false;
106 }
107 
108 #define UDP_LOG_BIND(inp, error) if (udp_is_log_enabled((inp), ULEF_BIND)) \
109     udp_log_bind((inp), (error))
110 
111 #define UDP_LOG_CONNECT(inp, error) if (udp_is_log_enabled((inp), ULEF_CONNECT)) \
112     udp_log_connect((inp), (error))
113 
114 #define UDP_LOG_CONNECTION_SUMMARY(inp) if ((inp) != NULL && ((inp)->inp_flags2 & INP2_LOGGING_ENABLED)) \
115     udp_log_connection_summary((inp))
116 
117 #define UDP_LOG(inp, format, ...) if (udp_is_log_enabled((inp), ULEF_LOG)) \
118     udp_log_message(__func__, __LINE__, (inp), format, ## __VA_ARGS__)
119 
120 #define UDP_LOG_DROP_NECP(hdr, uh, inp, outgoing) if (udp_is_log_enabled(inp, ULEF_DROP_NECP)) \
121     udp_log_drop_pcb((hdr), (uh), (inp), (outgoing), "NECP")
122 
123 #define UDP_LOG_DROP_PCB(hdr, uh, inp, outgoing, reason) if (udp_is_log_enabled(inp, ULEF_DROP_PCB)) \
124     udp_log_drop_pcb((hdr), (uh), (inp), (outgoing), (reason))
125 
126 
127 #endif /* BSD_KERNEL_PRIVATE */
128 
129 #endif /* _NETINET_UDP_LOG_H_ */
130