1*bbb1b6f9SApple OSS Distributions /*
2*bbb1b6f9SApple OSS Distributions * Copyright (c) 2024 Apple Inc. All rights reserved.
3*bbb1b6f9SApple OSS Distributions *
4*bbb1b6f9SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*bbb1b6f9SApple OSS Distributions *
6*bbb1b6f9SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*bbb1b6f9SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*bbb1b6f9SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*bbb1b6f9SApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*bbb1b6f9SApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*bbb1b6f9SApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*bbb1b6f9SApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*bbb1b6f9SApple OSS Distributions * terms of an Apple operating system software license agreement.
14*bbb1b6f9SApple OSS Distributions *
15*bbb1b6f9SApple OSS Distributions * Please obtain a copy of the License at
16*bbb1b6f9SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*bbb1b6f9SApple OSS Distributions *
18*bbb1b6f9SApple OSS Distributions * The Original Code and all software distributed under the License are
19*bbb1b6f9SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*bbb1b6f9SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*bbb1b6f9SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*bbb1b6f9SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*bbb1b6f9SApple OSS Distributions * Please see the License for the specific language governing rights and
24*bbb1b6f9SApple OSS Distributions * limitations under the License.
25*bbb1b6f9SApple OSS Distributions *
26*bbb1b6f9SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*bbb1b6f9SApple OSS Distributions */
28*bbb1b6f9SApple OSS Distributions
29*bbb1b6f9SApple OSS Distributions #include "kpi_interface.h"
30*bbb1b6f9SApple OSS Distributions #include <stddef.h>
31*bbb1b6f9SApple OSS Distributions
32*bbb1b6f9SApple OSS Distributions #include <net/dlil.h>
33*bbb1b6f9SApple OSS Distributions
34*bbb1b6f9SApple OSS Distributions #include <sys/param.h>
35*bbb1b6f9SApple OSS Distributions #include <sys/systm.h>
36*bbb1b6f9SApple OSS Distributions #include <sys/kernel.h>
37*bbb1b6f9SApple OSS Distributions #include <sys/malloc.h>
38*bbb1b6f9SApple OSS Distributions #include <sys/mbuf.h>
39*bbb1b6f9SApple OSS Distributions #include <sys/socket.h>
40*bbb1b6f9SApple OSS Distributions #include <sys/domain.h>
41*bbb1b6f9SApple OSS Distributions #include <sys/user.h>
42*bbb1b6f9SApple OSS Distributions
43*bbb1b6f9SApple OSS Distributions #include <kern/assert.h>
44*bbb1b6f9SApple OSS Distributions #include <kern/task.h>
45*bbb1b6f9SApple OSS Distributions #include <kern/thread.h>
46*bbb1b6f9SApple OSS Distributions #include <kern/sched_prim.h>
47*bbb1b6f9SApple OSS Distributions #include <kern/locks.h>
48*bbb1b6f9SApple OSS Distributions #include <kern/zalloc.h>
49*bbb1b6f9SApple OSS Distributions
50*bbb1b6f9SApple OSS Distributions struct net_thread_marks { };
51*bbb1b6f9SApple OSS Distributions static const struct net_thread_marks net_thread_marks_base = { };
52*bbb1b6f9SApple OSS Distributions
53*bbb1b6f9SApple OSS Distributions __private_extern__ const net_thread_marks_t net_thread_marks_none =
54*bbb1b6f9SApple OSS Distributions &net_thread_marks_base;
55*bbb1b6f9SApple OSS Distributions
56*bbb1b6f9SApple OSS Distributions __private_extern__ net_thread_marks_t
net_thread_marks_push(u_int32_t push)57*bbb1b6f9SApple OSS Distributions net_thread_marks_push(u_int32_t push)
58*bbb1b6f9SApple OSS Distributions {
59*bbb1b6f9SApple OSS Distributions static const char *__unsafe_indexable const base = (const void*__single)&net_thread_marks_base;
60*bbb1b6f9SApple OSS Distributions u_int32_t pop = 0;
61*bbb1b6f9SApple OSS Distributions
62*bbb1b6f9SApple OSS Distributions if (push != 0) {
63*bbb1b6f9SApple OSS Distributions struct uthread *uth = current_uthread();
64*bbb1b6f9SApple OSS Distributions
65*bbb1b6f9SApple OSS Distributions pop = push & ~uth->uu_network_marks;
66*bbb1b6f9SApple OSS Distributions if (pop != 0) {
67*bbb1b6f9SApple OSS Distributions uth->uu_network_marks |= pop;
68*bbb1b6f9SApple OSS Distributions }
69*bbb1b6f9SApple OSS Distributions }
70*bbb1b6f9SApple OSS Distributions
71*bbb1b6f9SApple OSS Distributions return __unsafe_forge_single(net_thread_marks_t, (base + pop));
72*bbb1b6f9SApple OSS Distributions }
73*bbb1b6f9SApple OSS Distributions
74*bbb1b6f9SApple OSS Distributions __private_extern__ net_thread_marks_t
net_thread_unmarks_push(u_int32_t unpush)75*bbb1b6f9SApple OSS Distributions net_thread_unmarks_push(u_int32_t unpush)
76*bbb1b6f9SApple OSS Distributions {
77*bbb1b6f9SApple OSS Distributions static const char *__unsafe_indexable const base = (const void*__single)&net_thread_marks_base;
78*bbb1b6f9SApple OSS Distributions u_int32_t unpop = 0;
79*bbb1b6f9SApple OSS Distributions
80*bbb1b6f9SApple OSS Distributions if (unpush != 0) {
81*bbb1b6f9SApple OSS Distributions struct uthread *uth = current_uthread();
82*bbb1b6f9SApple OSS Distributions
83*bbb1b6f9SApple OSS Distributions unpop = unpush & uth->uu_network_marks;
84*bbb1b6f9SApple OSS Distributions if (unpop != 0) {
85*bbb1b6f9SApple OSS Distributions uth->uu_network_marks &= ~unpop;
86*bbb1b6f9SApple OSS Distributions }
87*bbb1b6f9SApple OSS Distributions }
88*bbb1b6f9SApple OSS Distributions
89*bbb1b6f9SApple OSS Distributions return __unsafe_forge_single(net_thread_marks_t, (base + unpop));
90*bbb1b6f9SApple OSS Distributions }
91*bbb1b6f9SApple OSS Distributions
92*bbb1b6f9SApple OSS Distributions __private_extern__ void
net_thread_marks_pop(net_thread_marks_t popx)93*bbb1b6f9SApple OSS Distributions net_thread_marks_pop(net_thread_marks_t popx)
94*bbb1b6f9SApple OSS Distributions {
95*bbb1b6f9SApple OSS Distributions static const char *__unsafe_indexable const base = (const void*__single)&net_thread_marks_base;
96*bbb1b6f9SApple OSS Distributions const ptrdiff_t pop = (const char *)popx - (const char *)base;
97*bbb1b6f9SApple OSS Distributions if (pop != 0) {
98*bbb1b6f9SApple OSS Distributions static const ptrdiff_t ones = (ptrdiff_t)(u_int32_t)~0U;
99*bbb1b6f9SApple OSS Distributions struct uthread *uth = current_uthread();
100*bbb1b6f9SApple OSS Distributions
101*bbb1b6f9SApple OSS Distributions VERIFY((pop & ones) == pop);
102*bbb1b6f9SApple OSS Distributions VERIFY((ptrdiff_t)(uth->uu_network_marks & pop) == pop);
103*bbb1b6f9SApple OSS Distributions uth->uu_network_marks &= ~pop;
104*bbb1b6f9SApple OSS Distributions }
105*bbb1b6f9SApple OSS Distributions }
106*bbb1b6f9SApple OSS Distributions
107*bbb1b6f9SApple OSS Distributions __private_extern__ void
net_thread_unmarks_pop(net_thread_marks_t unpopx)108*bbb1b6f9SApple OSS Distributions net_thread_unmarks_pop(net_thread_marks_t unpopx)
109*bbb1b6f9SApple OSS Distributions {
110*bbb1b6f9SApple OSS Distributions static const char *__unsafe_indexable const base = (const void*__single)&net_thread_marks_base;
111*bbb1b6f9SApple OSS Distributions ptrdiff_t unpop = (const char *)unpopx - (const char *)base;
112*bbb1b6f9SApple OSS Distributions
113*bbb1b6f9SApple OSS Distributions if (unpop != 0) {
114*bbb1b6f9SApple OSS Distributions static const ptrdiff_t ones = (ptrdiff_t)(u_int32_t)~0U;
115*bbb1b6f9SApple OSS Distributions struct uthread *uth = current_uthread();
116*bbb1b6f9SApple OSS Distributions
117*bbb1b6f9SApple OSS Distributions VERIFY((unpop & ones) == unpop);
118*bbb1b6f9SApple OSS Distributions VERIFY((ptrdiff_t)(uth->uu_network_marks & unpop) == 0);
119*bbb1b6f9SApple OSS Distributions uth->uu_network_marks |= (u_int32_t)unpop;
120*bbb1b6f9SApple OSS Distributions }
121*bbb1b6f9SApple OSS Distributions }
122*bbb1b6f9SApple OSS Distributions
123*bbb1b6f9SApple OSS Distributions
124*bbb1b6f9SApple OSS Distributions __private_extern__ u_int32_t
net_thread_is_marked(u_int32_t check)125*bbb1b6f9SApple OSS Distributions net_thread_is_marked(u_int32_t check)
126*bbb1b6f9SApple OSS Distributions {
127*bbb1b6f9SApple OSS Distributions if (check != 0) {
128*bbb1b6f9SApple OSS Distributions struct uthread *uth = current_uthread();
129*bbb1b6f9SApple OSS Distributions return uth->uu_network_marks & check;
130*bbb1b6f9SApple OSS Distributions } else {
131*bbb1b6f9SApple OSS Distributions return 0;
132*bbb1b6f9SApple OSS Distributions }
133*bbb1b6f9SApple OSS Distributions }
134*bbb1b6f9SApple OSS Distributions
135*bbb1b6f9SApple OSS Distributions __private_extern__ u_int32_t
net_thread_is_unmarked(u_int32_t check)136*bbb1b6f9SApple OSS Distributions net_thread_is_unmarked(u_int32_t check)
137*bbb1b6f9SApple OSS Distributions {
138*bbb1b6f9SApple OSS Distributions if (check != 0) {
139*bbb1b6f9SApple OSS Distributions struct uthread *uth = current_uthread();
140*bbb1b6f9SApple OSS Distributions return ~uth->uu_network_marks & check;
141*bbb1b6f9SApple OSS Distributions } else {
142*bbb1b6f9SApple OSS Distributions return 0;
143*bbb1b6f9SApple OSS Distributions }
144*bbb1b6f9SApple OSS Distributions }
145