1 /*
2 * Copyright (c) 2023 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 #include <darwintest.h>
30
31 #include <sys/ioctl.h>
32
33 #include <net/bpf.h>
34
35 #include "bpflib.h"
36
37 T_GLOBAL_META(
38 T_META_NAMESPACE("xnu.net"),
39 T_META_ASROOT(true),
40 T_META_RADAR_COMPONENT_NAME("xnu"),
41 T_META_RADAR_COMPONENT_VERSION("networking"),
42 T_META_CHECK_LEAKS(false));
43
44 #ifdef BIOCGDIRECTION
45
46 static const char *
str_bpf_direction(u_int direction)47 str_bpf_direction(u_int direction)
48 {
49 switch (direction) {
50 case BPF_D_NONE:
51 return "BPF_D_NONE";
52 case BPF_D_IN:
53 return "BPF_D_IN";
54 case BPF_D_OUT:
55 return "BPF_D_OUT";
56 case BPF_D_INOUT:
57 return "BPF_D_INOUT";
58 default:
59 break;
60 }
61 return "<invalid>";
62 }
63
64 static void
test_set_direction(int fd,u_int direction)65 test_set_direction(int fd, u_int direction)
66 {
67 T_ASSERT_POSIX_SUCCESS(bpf_set_direction(fd, direction),
68 "bpf_set_direction(%d): %u (%s)", fd, direction, str_bpf_direction(direction));
69
70 u_int get_direction = (u_int)(-2);
71 T_ASSERT_POSIX_SUCCESS(bpf_get_direction(fd, &get_direction),
72 "bpf_get_direction(%d): %u (%s)", fd, get_direction, str_bpf_direction(get_direction));
73 T_ASSERT_EQ(get_direction, direction, "get_direction %d == direction %d", get_direction, direction);
74 }
75
76 T_DECL(bpf_direction, "test BPF set and grep direction", T_META_TAG_VM_PREFERRED)
77 {
78 int fd = bpf_new();
79 T_ASSERT_POSIX_SUCCESS(fd, "bpf open fd %d", fd);
80
81 u_int direction = (u_int)(-2); /* an invalid value */
82 T_ASSERT_POSIX_SUCCESS(bpf_get_direction(fd, &direction),
83 "bpf_get_direction(%d): %u (%s)", fd, direction, str_bpf_direction(direction));
84 T_ASSERT_EQ(direction, BPF_D_INOUT, "initial direction not BPF_D_INOUT");
85
86 test_set_direction(fd, BPF_D_INOUT);
87 test_set_direction(fd, BPF_D_IN);
88 test_set_direction(fd, BPF_D_OUT);
89 test_set_direction(fd, BPF_D_NONE);
90
91 direction = 10;
92 T_EXPECT_POSIX_FAILURE(bpf_set_direction(fd, direction), EINVAL,
93 "bpf_set_direction(%d): %u (%s)", fd, direction, str_bpf_direction(direction));
94 }
95
96 #else /* BIOCSETDIRECTION */
97
98 T_DECL(bpf_direction, "test BPF set and grep direction", T_META_TAG_VM_PREFERRED)
99 {
100 T_SKIP("BIOCSETDIRECTION is not defined");
101 }
102
103 #endif /* BIOCSETDIRECTION */
104
105 T_DECL(bpf_seesent, "test BIOCGSEESENT and BIOCSSEESENT", T_META_TAG_VM_PREFERRED)
106 {
107 int fd = bpf_new();
108 T_ASSERT_POSIX_SUCCESS(fd, "bpf open fd %d", fd);
109
110 u_int get_see_sent = (u_int) - 1;
111
112 T_ASSERT_POSIX_SUCCESS(ioctl(fd, BIOCGSEESENT, &get_see_sent), "BIOCGSEESENT");;
113 T_LOG("get_see_sent %u", get_see_sent);
114
115 u_int set_see_sent = get_see_sent == 0 ? 1 : 0;
116
117 T_ASSERT_POSIX_SUCCESS(ioctl(fd, BIOCSSEESENT, &set_see_sent), "BIOCSSEESENT");;
118 T_LOG("set_see_sent %u", set_see_sent);
119
120 T_ASSERT_POSIX_SUCCESS(ioctl(fd, BIOCGSEESENT, &get_see_sent), "BIOCGSEESENT");;
121 T_LOG("get_see_sent %u", set_see_sent);
122
123 T_ASSERT_EQ(get_see_sent, set_see_sent, "get_see_sent == set_see_sent");
124
125 set_see_sent = get_see_sent == 0 ? 1 : 0;
126
127 T_ASSERT_POSIX_SUCCESS(ioctl(fd, BIOCSSEESENT, &set_see_sent), "BIOCSSEESENT");;
128 T_LOG("set_see_sent %u", set_see_sent);
129
130 T_ASSERT_POSIX_SUCCESS(ioctl(fd, BIOCGSEESENT, &get_see_sent), "BIOCGSEESENT");;
131 T_LOG("get_see_sent %u", get_see_sent);
132
133 T_ASSERT_EQ(get_see_sent, set_see_sent, "get_see_sent == set_see_sent");
134 }
135