Loading...
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 | /* * Copyright (c) 2013-2014 Apple Inc. All rights reserved. * * @APPLE_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. 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_LICENSE_HEADER_END@ */ #ifndef __CONTENT_FILTER_H__ #define __CONTENT_FILTER_H__ #include <sys/param.h> #include <sys/types.h> #include <sys/_types/_timeval64.h> #include <sys/socket.h> #include <sys/syslog.h> #include <netinet/in.h> #include <stdint.h> #ifdef BSD_KERNEL_PRIVATE #include <sys/mbuf.h> #include <sys/socketvar.h> #endif /* BSD_KERNEL_PRIVATE */ __BEGIN_DECLS #ifdef PRIVATE /* * Kernel control name for an instance of a Content Filter * Use CTLIOCGINFO to find out the corresponding kernel control id * to be set in the sc_id field of sockaddr_ctl for connect(2) * Note: the sc_unit is ephemeral */ #define CONTENT_FILTER_CONTROL_NAME "com.apple.content-filter" /* * CFIL_OPT_NECP_CONTROL_UNIT * To set or get the NECP filter control unit for the kernel control socket * The option level is SYSPROTO_CONTROL */ #define CFIL_OPT_NECP_CONTROL_UNIT 1 /* uint32_t */ /* * How many filter may be active simultaneously */ #define CFIL_MAX_FILTER_COUNT 2 /* * Types of messages * * Event messages flow from kernel to user space while action * messages flow in the reverse direction. * A message in entirely represented by a packet sent or received * on a Content Filter kernel control socket. */ #define CFM_TYPE_EVENT 1 /* message from kernel */ #define CFM_TYPE_ACTION 2 /* message to kernel */ /* * Operations associated with events from kernel */ #define CFM_OP_SOCKET_ATTACHED 1 /* a socket has been attached */ #define CFM_OP_SOCKET_CLOSED 2 /* a socket is being closed */ #define CFM_OP_DATA_OUT 3 /* data being sent */ #define CFM_OP_DATA_IN 4 /* data being received */ #define CFM_OP_DISCONNECT_OUT 5 /* no more outgoing data */ #define CFM_OP_DISCONNECT_IN 6 /* no more incoming data */ /* * Operations associated with action from filter to kernel */ #define CFM_OP_DATA_UPDATE 16 /* update pass or peek offsets */ #define CFM_OP_DROP 17 /* shutdown socket, no more data */ /* * Opaque socket identifier */ typedef uint64_t cfil_sock_id_t; #define CFIL_SOCK_ID_NONE UINT64_MAX /* * struct cfil_msg_hdr * * Header common to all messages */ struct cfil_msg_hdr { uint32_t cfm_len; /* total length */ uint32_t cfm_version; uint32_t cfm_type; uint32_t cfm_op; cfil_sock_id_t cfm_sock_id; }; #define CFM_VERSION_CURRENT 1 /* * struct cfil_msg_sock_attached * * Information about a new socket being attached to the content filter * * Action: No reply is expected as this does not block the creation of the * TCP/IP but timely action must be taken to avoid user noticeable delays. * * Valid Types: CFM_TYPE_EVENT * * Valid Op: CFM_OP_SOCKET_ATTACHED */ struct cfil_msg_sock_attached { struct cfil_msg_hdr cfs_msghdr; int cfs_sock_family; /* e.g. PF_INET */ int cfs_sock_type; /* e.g. SOCK_STREAM */ int cfs_sock_protocol; /* e.g. IPPROTO_TCP */ int cfs_unused; /* padding */ pid_t cfs_pid; pid_t cfs_e_pid; uuid_t cfs_uuid; uuid_t cfs_e_uuid; }; /* * struct cfil_msg_data_event * * Event for the content fiter to act on a span of data * A data span is described by a pair of offsets over the cumulative * number of bytes sent or received on the socket. * * Action: The event must be acted upon but the filter may buffer * data spans until it has enough content to make a decision. * The action must be timely to avoid user noticeable delays. * * Valid Type: CFM_TYPE_EVENT * * Valid Ops: CFM_OP_DATA_OUT, CFM_OP_DATA_IN */ struct cfil_msg_data_event { struct cfil_msg_hdr cfd_msghdr; union sockaddr_in_4_6 cfc_src; union sockaddr_in_4_6 cfc_dst; uint64_t cfd_start_offset; uint64_t cfd_end_offset; /* Actual content data immediatly follows */ }; /* * struct cfil_msg_action * * Valid Type: CFM_TYPE_ACTION * * Valid Ops: CFM_OP_DATA_UPDATE, CFM_OP_DROP * * For CFM_OP_DATA_UPDATE: * * cfa_in_pass_offset and cfa_out_pass_offset indicates how much data is * allowed to pass. A zero value does not modify the corresponding pass offset. * * cfa_in_peek_offset and cfa_out_peek_offset lets the filter specify how much * data it needs to make a decision: the kernel will deliver data up to that * offset (if less than cfa_pass_offset it is ignored). Use CFM_MAX_OFFSET * if you don't value the corresponding peek offset to be updated. */ struct cfil_msg_action { struct cfil_msg_hdr cfa_msghdr; uint64_t cfa_in_pass_offset; uint64_t cfa_in_peek_offset; uint64_t cfa_out_pass_offset; uint64_t cfa_out_peek_offset; }; #define CFM_MAX_OFFSET UINT64_MAX /* * Statistics retrieved via sysctl(3) */ struct cfil_filter_stat { uint32_t cfs_len; uint32_t cfs_filter_id; uint32_t cfs_flags; uint32_t cfs_sock_count; uint32_t cfs_necp_control_unit; }; struct cfil_entry_stat { uint32_t ces_len; uint32_t ces_filter_id; uint32_t ces_flags; uint32_t ces_necp_control_unit; struct timeval64 ces_last_event; struct timeval64 ces_last_action; struct cfe_buf_stat { uint64_t cbs_pending_first; uint64_t cbs_pending_last; uint64_t cbs_ctl_first; uint64_t cbs_ctl_last; uint64_t cbs_pass_offset; uint64_t cbs_peek_offset; uint64_t cbs_peeked; } ces_snd, ces_rcv; }; struct cfil_sock_stat { uint32_t cfs_len; int cfs_sock_family; int cfs_sock_type; int cfs_sock_protocol; cfil_sock_id_t cfs_sock_id; uint64_t cfs_flags; pid_t cfs_pid; pid_t cfs_e_pid; uuid_t cfs_uuid; uuid_t cfs_e_uuid; struct cfi_buf_stat { uint64_t cbs_pending_first; uint64_t cbs_pending_last; uint64_t cbs_pass_offset; uint64_t cbs_inject_q_len; } cfs_snd, cfs_rcv; struct cfil_entry_stat ces_entries[CFIL_MAX_FILTER_COUNT]; }; /* * Global statistics */ struct cfil_stats { int32_t cfs_ctl_connect_ok; int32_t cfs_ctl_connect_fail; int32_t cfs_ctl_disconnect_ok; int32_t cfs_ctl_disconnect_fail; int32_t cfs_ctl_send_ok; int32_t cfs_ctl_send_bad; int32_t cfs_ctl_rcvd_ok; int32_t cfs_ctl_rcvd_bad; int32_t cfs_ctl_rcvd_flow_lift; int32_t cfs_ctl_action_data_update; int32_t cfs_ctl_action_drop; int32_t cfs_ctl_action_bad_op; int32_t cfs_ctl_action_bad_len; int32_t cfs_sock_id_not_found; int32_t cfs_cfi_alloc_ok; int32_t cfs_cfi_alloc_fail; int32_t cfs_sock_userspace_only; int32_t cfs_sock_attach_in_vain; int32_t cfs_sock_attach_already; int32_t cfs_sock_attach_no_mem; int32_t cfs_sock_attach_failed; int32_t cfs_sock_attached; int32_t cfs_sock_detached; int32_t cfs_attach_event_ok; int32_t cfs_attach_event_flow_control; int32_t cfs_attach_event_fail; int32_t cfs_closed_event_ok; int32_t cfs_closed_event_flow_control; int32_t cfs_closed_event_fail; int32_t cfs_data_event_ok; int32_t cfs_data_event_flow_control; int32_t cfs_data_event_fail; int32_t cfs_disconnect_in_event_ok; int32_t cfs_disconnect_out_event_ok; int32_t cfs_disconnect_event_flow_control; int32_t cfs_disconnect_event_fail; int32_t cfs_ctl_q_not_started; int32_t cfs_close_wait; int32_t cfs_close_wait_timeout; int32_t cfs_flush_in_drop; int32_t cfs_flush_out_drop; int32_t cfs_flush_in_close; int32_t cfs_flush_out_close; int32_t cfs_flush_in_free; int32_t cfs_flush_out_free; int32_t cfs_inject_q_nomem; int32_t cfs_inject_q_nobufs; int32_t cfs_inject_q_detached; int32_t cfs_inject_q_in_fail; int32_t cfs_inject_q_out_fail; int32_t cfs_inject_q_in_retry; int32_t cfs_inject_q_out_retry; int32_t cfs_data_in_control; int32_t cfs_data_in_oob; int32_t cfs_data_out_control; int32_t cfs_data_out_oob; int64_t cfs_ctl_q_in_enqueued __attribute__((aligned(8))); int64_t cfs_ctl_q_out_enqueued __attribute__((aligned(8))); int64_t cfs_ctl_q_in_peeked __attribute__((aligned(8))); int64_t cfs_ctl_q_out_peeked __attribute__((aligned(8))); int64_t cfs_pending_q_in_enqueued __attribute__((aligned(8))); int64_t cfs_pending_q_out_enqueued __attribute__((aligned(8))); int64_t cfs_inject_q_in_enqueued __attribute__((aligned(8))); int64_t cfs_inject_q_out_enqueued __attribute__((aligned(8))); int64_t cfs_inject_q_in_passed __attribute__((aligned(8))); int64_t cfs_inject_q_out_passed __attribute__((aligned(8))); }; #endif /* PRIVATE */ #ifdef BSD_KERNEL_PRIVATE #define M_SKIPCFIL M_PROTO5 extern int cfil_log_level; #define CFIL_LOG(level, fmt, ...) \ do { \ if (cfil_log_level >= level) \ printf("%s:%d " fmt "\n",\ __FUNCTION__, __LINE__, ##__VA_ARGS__); \ } while (0) extern void cfil_init(void); extern errno_t cfil_sock_attach(struct socket *so); extern errno_t cfil_sock_detach(struct socket *so); extern int cfil_sock_data_out(struct socket *so, struct sockaddr *to, struct mbuf *data, struct mbuf *control, uint32_t flags); extern int cfil_sock_data_in(struct socket *so, struct sockaddr *from, struct mbuf *data, struct mbuf *control, uint32_t flags); extern int cfil_sock_shutdown(struct socket *so, int *how); extern void cfil_sock_is_closed(struct socket *so); extern void cfil_sock_notify_shutdown(struct socket *so, int how); extern void cfil_sock_close_wait(struct socket *so); extern boolean_t cfil_sock_data_pending(struct sockbuf *sb); extern int cfil_sock_data_space(struct sockbuf *sb); extern void cfil_sock_buf_update(struct sockbuf *sb); extern cfil_sock_id_t cfil_sock_id_from_socket(struct socket *so); __END_DECLS #endif /* BSD_KERNEL_PRIVATE */ #endif /* __CONTENT_FILTER_H__ */ |