1*94d3b452SApple OSS Distributions /*
2*94d3b452SApple OSS Distributions * Copyright (c) 2023 Apple Inc. All rights reserved.
3*94d3b452SApple OSS Distributions *
4*94d3b452SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*94d3b452SApple OSS Distributions *
6*94d3b452SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*94d3b452SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*94d3b452SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*94d3b452SApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*94d3b452SApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*94d3b452SApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*94d3b452SApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*94d3b452SApple OSS Distributions * terms of an Apple operating system software license agreement.
14*94d3b452SApple OSS Distributions *
15*94d3b452SApple OSS Distributions * Please obtain a copy of the License at
16*94d3b452SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*94d3b452SApple OSS Distributions *
18*94d3b452SApple OSS Distributions * The Original Code and all software distributed under the License are
19*94d3b452SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*94d3b452SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*94d3b452SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*94d3b452SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*94d3b452SApple OSS Distributions * Please see the License for the specific language governing rights and
24*94d3b452SApple OSS Distributions * limitations under the License.
25*94d3b452SApple OSS Distributions *
26*94d3b452SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*94d3b452SApple OSS Distributions */
28*94d3b452SApple OSS Distributions
29*94d3b452SApple OSS Distributions #include <darwintest.h>
30*94d3b452SApple OSS Distributions
31*94d3b452SApple OSS Distributions #include <sys/ioctl.h>
32*94d3b452SApple OSS Distributions
33*94d3b452SApple OSS Distributions #include <net/bpf.h>
34*94d3b452SApple OSS Distributions
35*94d3b452SApple OSS Distributions #include "bpflib.h"
36*94d3b452SApple OSS Distributions
37*94d3b452SApple OSS Distributions T_GLOBAL_META(
38*94d3b452SApple OSS Distributions T_META_NAMESPACE("xnu.net"),
39*94d3b452SApple OSS Distributions T_META_ASROOT(true),
40*94d3b452SApple OSS Distributions T_META_RADAR_COMPONENT_NAME("xnu"),
41*94d3b452SApple OSS Distributions T_META_RADAR_COMPONENT_VERSION("networking"),
42*94d3b452SApple OSS Distributions T_META_CHECK_LEAKS(false));
43*94d3b452SApple OSS Distributions
44*94d3b452SApple OSS Distributions #ifdef BIOCGDIRECTION
45*94d3b452SApple OSS Distributions
46*94d3b452SApple OSS Distributions static const char *
str_bpf_direction(u_int direction)47*94d3b452SApple OSS Distributions str_bpf_direction(u_int direction)
48*94d3b452SApple OSS Distributions {
49*94d3b452SApple OSS Distributions switch (direction) {
50*94d3b452SApple OSS Distributions case BPF_D_NONE:
51*94d3b452SApple OSS Distributions return "BPF_D_NONE";
52*94d3b452SApple OSS Distributions case BPF_D_IN:
53*94d3b452SApple OSS Distributions return "BPF_D_IN";
54*94d3b452SApple OSS Distributions case BPF_D_OUT:
55*94d3b452SApple OSS Distributions return "BPF_D_OUT";
56*94d3b452SApple OSS Distributions case BPF_D_INOUT:
57*94d3b452SApple OSS Distributions return "BPF_D_INOUT";
58*94d3b452SApple OSS Distributions default:
59*94d3b452SApple OSS Distributions break;
60*94d3b452SApple OSS Distributions }
61*94d3b452SApple OSS Distributions return "<invalid>";
62*94d3b452SApple OSS Distributions }
63*94d3b452SApple OSS Distributions
64*94d3b452SApple OSS Distributions static void
test_set_direction(int fd,u_int direction)65*94d3b452SApple OSS Distributions test_set_direction(int fd, u_int direction)
66*94d3b452SApple OSS Distributions {
67*94d3b452SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(bpf_set_direction(fd, direction),
68*94d3b452SApple OSS Distributions "bpf_set_direction(%d): %u (%s)", fd, direction, str_bpf_direction(direction));
69*94d3b452SApple OSS Distributions
70*94d3b452SApple OSS Distributions u_int get_direction = (u_int)(-2);
71*94d3b452SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(bpf_get_direction(fd, &get_direction),
72*94d3b452SApple OSS Distributions "bpf_get_direction(%d): %u (%s)", fd, get_direction, str_bpf_direction(get_direction));
73*94d3b452SApple OSS Distributions T_ASSERT_EQ(get_direction, direction, "get_direction %d == direction %d", get_direction, direction);
74*94d3b452SApple OSS Distributions }
75*94d3b452SApple OSS Distributions
76*94d3b452SApple OSS Distributions T_DECL(bpf_direction, "test BPF set and grep direction")
77*94d3b452SApple OSS Distributions {
78*94d3b452SApple OSS Distributions int fd = bpf_new();
79*94d3b452SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(fd, "bpf open fd %d", fd);
80*94d3b452SApple OSS Distributions
81*94d3b452SApple OSS Distributions u_int direction = (u_int)(-2); /* an invalid value */
82*94d3b452SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(bpf_get_direction(fd, &direction),
83*94d3b452SApple OSS Distributions "bpf_get_direction(%d): %u (%s)", fd, direction, str_bpf_direction(direction));
84*94d3b452SApple OSS Distributions T_ASSERT_EQ(direction, BPF_D_INOUT, "initial direction not BPF_D_INOUT");
85*94d3b452SApple OSS Distributions
86*94d3b452SApple OSS Distributions test_set_direction(fd, BPF_D_INOUT);
87*94d3b452SApple OSS Distributions test_set_direction(fd, BPF_D_IN);
88*94d3b452SApple OSS Distributions test_set_direction(fd, BPF_D_OUT);
89*94d3b452SApple OSS Distributions test_set_direction(fd, BPF_D_NONE);
90*94d3b452SApple OSS Distributions
91*94d3b452SApple OSS Distributions direction = 10;
92*94d3b452SApple OSS Distributions T_EXPECT_POSIX_FAILURE(bpf_set_direction(fd, direction), EINVAL,
93*94d3b452SApple OSS Distributions "bpf_set_direction(%d): %u (%s)", fd, direction, str_bpf_direction(direction));
94*94d3b452SApple OSS Distributions }
95*94d3b452SApple OSS Distributions
96*94d3b452SApple OSS Distributions #else /* BIOCSETDIRECTION */
97*94d3b452SApple OSS Distributions
98*94d3b452SApple OSS Distributions T_DECL(bpf_direction, "test BPF set and grep direction")
99*94d3b452SApple OSS Distributions {
100*94d3b452SApple OSS Distributions T_SKIP("BIOCSETDIRECTION is not defined");
101*94d3b452SApple OSS Distributions }
102*94d3b452SApple OSS Distributions
103*94d3b452SApple OSS Distributions #endif /* BIOCSETDIRECTION */
104*94d3b452SApple OSS Distributions
105*94d3b452SApple OSS Distributions T_DECL(bpf_seesent, "test BIOCGSEESENT and BIOCSSEESENT")
106*94d3b452SApple OSS Distributions {
107*94d3b452SApple OSS Distributions int fd = bpf_new();
108*94d3b452SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(fd, "bpf open fd %d", fd);
109*94d3b452SApple OSS Distributions
110*94d3b452SApple OSS Distributions u_int get_see_sent = (u_int) - 1;
111*94d3b452SApple OSS Distributions
112*94d3b452SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ioctl(fd, BIOCGSEESENT, &get_see_sent), "BIOCGSEESENT");;
113*94d3b452SApple OSS Distributions T_LOG("get_see_sent %u", get_see_sent);
114*94d3b452SApple OSS Distributions
115*94d3b452SApple OSS Distributions u_int set_see_sent = get_see_sent == 0 ? 1 : 0;
116*94d3b452SApple OSS Distributions
117*94d3b452SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ioctl(fd, BIOCSSEESENT, &set_see_sent), "BIOCSSEESENT");;
118*94d3b452SApple OSS Distributions T_LOG("set_see_sent %u", set_see_sent);
119*94d3b452SApple OSS Distributions
120*94d3b452SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ioctl(fd, BIOCGSEESENT, &get_see_sent), "BIOCGSEESENT");;
121*94d3b452SApple OSS Distributions T_LOG("get_see_sent %u", set_see_sent);
122*94d3b452SApple OSS Distributions
123*94d3b452SApple OSS Distributions T_ASSERT_EQ(get_see_sent, set_see_sent, "get_see_sent == set_see_sent");
124*94d3b452SApple OSS Distributions
125*94d3b452SApple OSS Distributions set_see_sent = get_see_sent == 0 ? 1 : 0;
126*94d3b452SApple OSS Distributions
127*94d3b452SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ioctl(fd, BIOCSSEESENT, &set_see_sent), "BIOCSSEESENT");;
128*94d3b452SApple OSS Distributions T_LOG("set_see_sent %u", set_see_sent);
129*94d3b452SApple OSS Distributions
130*94d3b452SApple OSS Distributions T_ASSERT_POSIX_SUCCESS(ioctl(fd, BIOCGSEESENT, &get_see_sent), "BIOCGSEESENT");;
131*94d3b452SApple OSS Distributions T_LOG("get_see_sent %u", get_see_sent);
132*94d3b452SApple OSS Distributions
133*94d3b452SApple OSS Distributions T_ASSERT_EQ(get_see_sent, set_see_sent, "get_see_sent == set_see_sent");
134*94d3b452SApple OSS Distributions }
135