2 * Copyright (c) 2013-2014 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
24 #ifndef __CONTENT_FILTER_H__
25 #define __CONTENT_FILTER_H__
27 #include <sys/param.h>
28 #include <sys/types.h>
29 #include <sys/_types/_timeval64.h>
30 #include <sys/socket.h>
31 #include <sys/syslog.h>
32 #include <netinet/in.h>
35 #ifdef BSD_KERNEL_PRIVATE
37 #include <sys/socketvar.h>
38 #endif /* BSD_KERNEL_PRIVATE */
45 * Kernel control name for an instance of a Content Filter
46 * Use CTLIOCGINFO to find out the corresponding kernel control id
47 * to be set in the sc_id field of sockaddr_ctl for connect(2)
48 * Note: the sc_unit is ephemeral
50 #define CONTENT_FILTER_CONTROL_NAME "com.apple.content-filter"
53 * CFIL_OPT_NECP_CONTROL_UNIT
54 * To set or get the NECP filter control unit for the kernel control socket
55 * The option level is SYSPROTO_CONTROL
57 #define CFIL_OPT_NECP_CONTROL_UNIT 1 /* uint32_t */
60 * How many filter may be active simultaneously
62 #define CFIL_MAX_FILTER_COUNT 2
67 * Event messages flow from kernel to user space while action
68 * messages flow in the reverse direction.
69 * A message in entirely represented by a packet sent or received
70 * on a Content Filter kernel control socket.
72 #define CFM_TYPE_EVENT 1 /* message from kernel */
73 #define CFM_TYPE_ACTION 2 /* message to kernel */
76 * Operations associated with events from kernel
78 #define CFM_OP_SOCKET_ATTACHED 1 /* a socket has been attached */
79 #define CFM_OP_SOCKET_CLOSED 2 /* a socket is being closed */
80 #define CFM_OP_DATA_OUT 3 /* data being sent */
81 #define CFM_OP_DATA_IN 4 /* data being received */
82 #define CFM_OP_DISCONNECT_OUT 5 /* no more outgoing data */
83 #define CFM_OP_DISCONNECT_IN 6 /* no more incoming data */
86 * Operations associated with action from filter to kernel
88 #define CFM_OP_DATA_UPDATE 16 /* update pass or peek offsets */
89 #define CFM_OP_DROP 17 /* shutdown socket, no more data */
92 * Opaque socket identifier
94 typedef uint64_t cfil_sock_id_t
;
96 #define CFIL_SOCK_ID_NONE UINT64_MAX
101 * Header common to all messages
103 struct cfil_msg_hdr
{
104 uint32_t cfm_len
; /* total length */
105 uint32_t cfm_version
;
108 cfil_sock_id_t cfm_sock_id
;
111 #define CFM_VERSION_CURRENT 1
114 * struct cfil_msg_sock_attached
116 * Information about a new socket being attached to the content filter
118 * Action: No reply is expected as this does not block the creation of the
119 * TCP/IP but timely action must be taken to avoid user noticeable delays.
121 * Valid Types: CFM_TYPE_EVENT
123 * Valid Op: CFM_OP_SOCKET_ATTACHED
125 struct cfil_msg_sock_attached
{
126 struct cfil_msg_hdr cfs_msghdr
;
127 int cfs_sock_family
; /* e.g. PF_INET */
128 int cfs_sock_type
; /* e.g. SOCK_STREAM */
129 int cfs_sock_protocol
; /* e.g. IPPROTO_TCP */
130 int cfs_unused
; /* padding */
138 * struct cfil_msg_data_event
140 * Event for the content fiter to act on a span of data
141 * A data span is described by a pair of offsets over the cumulative
142 * number of bytes sent or received on the socket.
144 * Action: The event must be acted upon but the filter may buffer
145 * data spans until it has enough content to make a decision.
146 * The action must be timely to avoid user noticeable delays.
148 * Valid Type: CFM_TYPE_EVENT
150 * Valid Ops: CFM_OP_DATA_OUT, CFM_OP_DATA_IN
152 struct cfil_msg_data_event
{
153 struct cfil_msg_hdr cfd_msghdr
;
154 union sockaddr_in_4_6 cfc_src
;
155 union sockaddr_in_4_6 cfc_dst
;
156 uint64_t cfd_start_offset
;
157 uint64_t cfd_end_offset
;
158 /* Actual content data immediatly follows */
162 * struct cfil_msg_action
164 * Valid Type: CFM_TYPE_ACTION
166 * Valid Ops: CFM_OP_DATA_UPDATE, CFM_OP_DROP
168 * For CFM_OP_DATA_UPDATE:
170 * cfa_in_pass_offset and cfa_out_pass_offset indicates how much data is
171 * allowed to pass. A zero value does not modify the corresponding pass offset.
173 * cfa_in_peek_offset and cfa_out_peek_offset lets the filter specify how much
174 * data it needs to make a decision: the kernel will deliver data up to that
175 * offset (if less than cfa_pass_offset it is ignored). Use CFM_MAX_OFFSET
176 * if you don't value the corresponding peek offset to be updated.
178 struct cfil_msg_action
{
179 struct cfil_msg_hdr cfa_msghdr
;
180 uint64_t cfa_in_pass_offset
;
181 uint64_t cfa_in_peek_offset
;
182 uint64_t cfa_out_pass_offset
;
183 uint64_t cfa_out_peek_offset
;
186 #define CFM_MAX_OFFSET UINT64_MAX
189 * Statistics retrieved via sysctl(3)
191 struct cfil_filter_stat
{
193 uint32_t cfs_filter_id
;
195 uint32_t cfs_sock_count
;
196 uint32_t cfs_necp_control_unit
;
199 struct cfil_entry_stat
{
201 uint32_t ces_filter_id
;
203 uint32_t ces_necp_control_unit
;
204 struct timeval64 ces_last_event
;
205 struct timeval64 ces_last_action
;
206 struct cfe_buf_stat
{
207 uint64_t cbs_pending_first
;
208 uint64_t cbs_pending_last
;
209 uint64_t cbs_ctl_first
;
210 uint64_t cbs_ctl_last
;
211 uint64_t cbs_pass_offset
;
212 uint64_t cbs_peek_offset
;
217 struct cfil_sock_stat
{
221 int cfs_sock_protocol
;
222 cfil_sock_id_t cfs_sock_id
;
228 struct cfi_buf_stat
{
229 uint64_t cbs_pending_first
;
230 uint64_t cbs_pending_last
;
231 uint64_t cbs_pass_offset
;
232 uint64_t cbs_inject_q_len
;
234 struct cfil_entry_stat ces_entries
[CFIL_MAX_FILTER_COUNT
];
241 int32_t cfs_ctl_connect_ok
;
242 int32_t cfs_ctl_connect_fail
;
243 int32_t cfs_ctl_disconnect_ok
;
244 int32_t cfs_ctl_disconnect_fail
;
245 int32_t cfs_ctl_send_ok
;
246 int32_t cfs_ctl_send_bad
;
247 int32_t cfs_ctl_rcvd_ok
;
248 int32_t cfs_ctl_rcvd_bad
;
249 int32_t cfs_ctl_rcvd_flow_lift
;
250 int32_t cfs_ctl_action_data_update
;
251 int32_t cfs_ctl_action_drop
;
252 int32_t cfs_ctl_action_bad_op
;
253 int32_t cfs_ctl_action_bad_len
;
255 int32_t cfs_sock_id_not_found
;
257 int32_t cfs_cfi_alloc_ok
;
258 int32_t cfs_cfi_alloc_fail
;
260 int32_t cfs_sock_userspace_only
;
261 int32_t cfs_sock_attach_in_vain
;
262 int32_t cfs_sock_attach_already
;
263 int32_t cfs_sock_attach_no_mem
;
264 int32_t cfs_sock_attach_failed
;
265 int32_t cfs_sock_attached
;
266 int32_t cfs_sock_detached
;
268 int32_t cfs_attach_event_ok
;
269 int32_t cfs_attach_event_flow_control
;
270 int32_t cfs_attach_event_fail
;
272 int32_t cfs_closed_event_ok
;
273 int32_t cfs_closed_event_flow_control
;
274 int32_t cfs_closed_event_fail
;
276 int32_t cfs_data_event_ok
;
277 int32_t cfs_data_event_flow_control
;
278 int32_t cfs_data_event_fail
;
280 int32_t cfs_disconnect_in_event_ok
;
281 int32_t cfs_disconnect_out_event_ok
;
282 int32_t cfs_disconnect_event_flow_control
;
283 int32_t cfs_disconnect_event_fail
;
285 int32_t cfs_ctl_q_not_started
;
287 int32_t cfs_close_wait
;
288 int32_t cfs_close_wait_timeout
;
290 int32_t cfs_flush_in_drop
;
291 int32_t cfs_flush_out_drop
;
292 int32_t cfs_flush_in_close
;
293 int32_t cfs_flush_out_close
;
294 int32_t cfs_flush_in_free
;
295 int32_t cfs_flush_out_free
;
297 int32_t cfs_inject_q_nomem
;
298 int32_t cfs_inject_q_nobufs
;
299 int32_t cfs_inject_q_detached
;
300 int32_t cfs_inject_q_in_fail
;
301 int32_t cfs_inject_q_out_fail
;
303 int32_t cfs_inject_q_in_retry
;
304 int32_t cfs_inject_q_out_retry
;
306 int32_t cfs_data_in_control
;
307 int32_t cfs_data_in_oob
;
308 int32_t cfs_data_out_control
;
309 int32_t cfs_data_out_oob
;
311 int64_t cfs_ctl_q_in_enqueued
__attribute__((aligned(8)));
312 int64_t cfs_ctl_q_out_enqueued
__attribute__((aligned(8)));
313 int64_t cfs_ctl_q_in_peeked
__attribute__((aligned(8)));
314 int64_t cfs_ctl_q_out_peeked
__attribute__((aligned(8)));
316 int64_t cfs_pending_q_in_enqueued
__attribute__((aligned(8)));
317 int64_t cfs_pending_q_out_enqueued
__attribute__((aligned(8)));
319 int64_t cfs_inject_q_in_enqueued
__attribute__((aligned(8)));
320 int64_t cfs_inject_q_out_enqueued
__attribute__((aligned(8)));
321 int64_t cfs_inject_q_in_passed
__attribute__((aligned(8)));
322 int64_t cfs_inject_q_out_passed
__attribute__((aligned(8)));
327 #ifdef BSD_KERNEL_PRIVATE
329 #define M_SKIPCFIL M_PROTO5
331 extern int cfil_log_level
;
333 #define CFIL_LOG(level, fmt, ...) \
335 if (cfil_log_level >= level) \
336 printf("%s:%d " fmt "\n",\
337 __FUNCTION__, __LINE__, ##__VA_ARGS__); \
341 extern void cfil_init(void);
343 extern errno_t
cfil_sock_attach(struct socket
*so
);
344 extern errno_t
cfil_sock_detach(struct socket
*so
);
346 extern int cfil_sock_data_out(struct socket
*so
, struct sockaddr
*to
,
347 struct mbuf
*data
, struct mbuf
*control
,
349 extern int cfil_sock_data_in(struct socket
*so
, struct sockaddr
*from
,
350 struct mbuf
*data
, struct mbuf
*control
,
353 extern int cfil_sock_shutdown(struct socket
*so
, int *how
);
354 extern void cfil_sock_is_closed(struct socket
*so
);
355 extern void cfil_sock_notify_shutdown(struct socket
*so
, int how
);
356 extern void cfil_sock_close_wait(struct socket
*so
);
358 extern boolean_t
cfil_sock_data_pending(struct sockbuf
*sb
);
359 extern int cfil_sock_data_space(struct sockbuf
*sb
);
360 extern void cfil_sock_buf_update(struct sockbuf
*sb
);
362 extern cfil_sock_id_t
cfil_sock_id_from_socket(struct socket
*so
);
366 #endif /* BSD_KERNEL_PRIVATE */
368 #endif /* __CONTENT_FILTER_H__ */