Debian Codegraph @

src/tun_darwin.c

raw symbols SCIP
1 /* librist. Copyright © 2026 SipRadius LLC. All right reserved.
2 * Author: Sergio Ammirata, Ph.D. <[email protected]>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7 #ifdef __APPLE__
8 #include <TargetConditionals.h>
9 #endif
10
11 /* utun sockets and <sys/kern_control.h> / <net/if_utun.h> only exist in the
12 * macOS SDK. iOS / tvOS / watchOS / visionOS expose __APPLE__ but do not ship
13 * those headers, so narrow the guard to plain macOS. */
14 #if defined(__APPLE__) && TARGET_OS_OSX
15
16 #include "librist/tun.h"
17
18 #include <errno.h>
19 #include <limits.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <sys/ioctl.h>
25 #include <sys/socket.h>
26 #include <sys/kern_control.h>
27 #include <sys/sys_domain.h>
28 #include <net/if.h>
29 #include <net/if_utun.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
32 #include <net/route.h>
33
34 /*
35 * macOS utun devices deliver/expect a 4-byte protocol header (AF family)
36 * before each IP packet. We handle this transparently in rist_tun_read/rist_tun_write.
37 */
38 #define UTUN_HEADER_SIZE 4
39 #define TUN_DARWIN_BUFSIZE (65536 + UTUN_HEADER_SIZE)
40
41 int rist_tun_open(const char *requested_name, char *actual_name, size_t name_len)
42 {
43 int fd = socket(PF_SYSTEM, SOCK_DGRAM, SYSPROTO_CONTROL);
44 if (fd < 0) {
45 perror("socket PF_SYSTEM");
46 return -1;
47 }
48
49 struct ctl_info ci;
50 memset(&ci, 0, sizeof(ci));
51 strncpy(ci.ctl_name, UTUN_CONTROL_NAME, sizeof(ci.ctl_name) - 1);
52
53 if (ioctl(fd, CTLIOCGINFO, &ci) < 0) {
54 perror("ioctl CTLIOCGINFO");
55 close(fd);
56 return -1;
57 }
58
59 struct sockaddr_ctl sc;
60 memset(&sc, 0, sizeof(sc));
61 sc.sc_len = sizeof(sc);
62 sc.sc_family = AF_SYSTEM;
63 sc.ss_sysaddr = AF_SYS_CONTROL;
64 sc.sc_id = ci.ctl_id;
65
66 /*
67 * sc_unit maps to utunN where N = sc_unit - 1.
68 * Unit 0 = let the kernel pick.
69 */
70 unsigned int unit = 0;
71 if (requested_name && requested_name[0]) {
72 if (strncmp(requested_name, "utun", 4) == 0) {
73 const char *suffix = requested_name + 4;
74 char *endptr = NULL;
75 errno = 0;
76 unsigned long parsed = strtoul(suffix, &endptr, 10);
77 if (suffix[0] != '\0' && errno == 0 &&
78 endptr != suffix && *endptr == '\0' &&
79 parsed < (UINT_MAX - 1)) {
80 unit = (unsigned int)parsed + 1;
81 }
82 }
83 }
84 sc.sc_unit = unit;
85
86 if (connect(fd, (struct sockaddr *)&sc, sizeof(sc)) < 0) {
87 perror("connect utun");
88 close(fd);
89 return -1;
90 }
91
92 if (actual_name && name_len > 0) {
93 socklen_t optlen = (socklen_t)name_len;
94 if (getsockopt(fd, SYSPROTO_CONTROL, UTUN_OPT_IFNAME, actual_name, &optlen) < 0) {
95 perror("getsockopt UTUN_OPT_IFNAME");
96 close(fd);
97 return -1;
98 }
99 }
100
101 return fd;
102 }
103
104 void rist_tun_close(int fd)
105 {
106 if (fd >= 0)
107 close(fd);
108 }
109
110 int rist_tun_read(int fd, uint8_t *buf, size_t len)
111 {
112 uint8_t tmp[TUN_DARWIN_BUFSIZE];
113 size_t readlen = len + UTUN_HEADER_SIZE;
114 if (readlen > sizeof(tmp))
115 readlen = sizeof(tmp);
116
117 int nread = (int)read(fd, tmp, readlen);
118 if (nread <= UTUN_HEADER_SIZE)
119 return nread < 0 ? -1 : 0;
120
121 int payload_len = nread - UTUN_HEADER_SIZE;
122 if ((size_t)payload_len > len)
123 payload_len = (int)len;
124
125 memcpy(buf, tmp + UTUN_HEADER_SIZE, payload_len);
126 return payload_len;
127 }
128
129 int rist_tun_write(int fd, const uint8_t *buf, size_t len)
130 {
131 uint8_t tmp[TUN_DARWIN_BUFSIZE];
132 if (len > sizeof(tmp) - UTUN_HEADER_SIZE)
133 return -1;
134
135 uint32_t af;
136 if (len > 0 && (buf[0] >> 4) == 6)
137 af = htonl(AF_INET6);
138 else
139 af = htonl(AF_INET);
140
141 memcpy(tmp, &af, UTUN_HEADER_SIZE);
142 memcpy(tmp + UTUN_HEADER_SIZE, buf, len);
143
144 int written = (int)write(fd, tmp, len + UTUN_HEADER_SIZE);
145 if (written <= UTUN_HEADER_SIZE)
146 return written < 0 ? -1 : 0;
147
148 return written - UTUN_HEADER_SIZE;
149 }
150
151 int rist_tun_set_ip(const char *dev, const char *ip, int prefix_len)
152 {
153 if (prefix_len < 0 || prefix_len > 32) {
154 fprintf(stderr, "Invalid prefix length: %d\n", prefix_len);
155 return -1;
156 }
157 struct ifaliasreq ifra;
158 memset(&ifra, 0, sizeof(ifra));
159 strncpy(ifra.ifra_name, dev, IFNAMSIZ - 1);
160
161 struct sockaddr_in *addr = (struct sockaddr_in *)&ifra.ifra_addr;
162 addr->sin_len = sizeof(*addr);
163 addr->sin_family = AF_INET;
164 if (inet_pton(AF_INET, ip, &addr->sin_addr) != 1) {
165 fprintf(stderr, "Invalid IP address: %s\n", ip);
166 return -1;
167 }
168
169 struct sockaddr_in *mask = (struct sockaddr_in *)&ifra.ifra_mask;
170 mask->sin_len = sizeof(*mask);
171 mask->sin_family = AF_INET;
172 if (prefix_len == 0)
173 mask->sin_addr.s_addr = 0;
174 else if (prefix_len == 32)
175 mask->sin_addr.s_addr = htonl(0xFFFFFFFFu);
176 else
177 mask->sin_addr.s_addr = htonl(~((1U << (32 - prefix_len)) - 1));
178
179 struct sockaddr_in *dst = (struct sockaddr_in *)&ifra.ifra_broadaddr;
180 dst->sin_len = sizeof(*dst);
181 dst->sin_family = AF_INET;
182 dst->sin_addr = addr->sin_addr;
183
184 int s = socket(AF_INET, SOCK_DGRAM, 0);
185 if (s < 0) {
186 perror("socket AF_INET");
187 return -1;
188 }
189
190 if (ioctl(s, SIOCAIFADDR, &ifra) < 0) {
191 perror("ioctl SIOCAIFADDR");
192 close(s);
193 return -1;
194 }
195
196 close(s);
197 return 0;
198 }
199
200 int rist_tun_set_mtu(const char *dev, int mtu)
201 {
202 int s = socket(AF_INET, SOCK_DGRAM, 0);
203 if (s < 0) {
204 perror("socket AF_INET");
205 return -1;
206 }
207
208 struct ifreq ifr;
209 memset(&ifr, 0, sizeof(ifr));
210 strncpy(ifr.ifr_name, dev, IFNAMSIZ - 1);
211 ifr.ifr_mtu = mtu;
212
213 if (ioctl(s, SIOCSIFMTU, &ifr) < 0) {
214 perror("ioctl SIOCSIFMTU");
215 close(s);
216 return -1;
217 }
218
219 close(s);
220 return 0;
221 }
222
223 int rist_tun_bring_up(const char *dev)
224 {
225 int s = socket(AF_INET, SOCK_DGRAM, 0);
226 if (s < 0) {
227 perror("socket AF_INET");
228 return -1;
229 }
230
231 struct ifreq ifr;
232 memset(&ifr, 0, sizeof(ifr));
233 strncpy(ifr.ifr_name, dev, IFNAMSIZ - 1);
234
235 if (ioctl(s, SIOCGIFFLAGS, &ifr) < 0) {
236 perror("ioctl SIOCGIFFLAGS");
237 close(s);
238 return -1;
239 }
240
241 ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
242
243 if (ioctl(s, SIOCSIFFLAGS, &ifr) < 0) {
244 perror("ioctl SIOCSIFFLAGS");
245 close(s);
246 return -1;
247 }
248
249 close(s);
250 return 0;
251 }
252
253 #elif defined(__APPLE__)
254
255 /* Embedded Apple platforms (iOS, tvOS, watchOS, visionOS) do not ship
256 * <sys/kern_control.h> / <net/if_utun.h> and therefore cannot provide a
257 * real utun implementation. Provide stub entry points with the same
258 * signatures so that rist.c / rist-common.c still link cleanly. All
259 * calls fail with -1, matching tun_win.c's behaviour on platforms
260 * without native TUN support. */
261
262 #include "librist/tun.h"
263 #include <stdio.h>
264
265 int rist_tun_open(const char *requested_name, char *actual_name, size_t name_len)
266 {
267 (void)requested_name;
268 (void)actual_name;
269 (void)name_len;
270 fprintf(stderr, "librist: TUN is not supported on this Apple platform\n");
271 return -1;
272 }
273
274 void rist_tun_close(int fd)
275 {
276 (void)fd;
277 }
278
279 int rist_tun_read(int fd, uint8_t *buf, size_t len)
280 {
281 (void)fd;
282 (void)buf;
283 (void)len;
284 return -1;
285 }
286
287 int rist_tun_write(int fd, const uint8_t *buf, size_t len)
288 {
289 (void)fd;
290 (void)buf;
291 (void)len;
292 return -1;
293 }
294
295 int rist_tun_set_ip(const char *dev, const char *ip, int prefix_len)
296 {
297 (void)dev;
298 (void)ip;
299 (void)prefix_len;
300 return -1;
301 }
302
303 int rist_tun_set_mtu(const char *dev, int mtu)
304 {
305 (void)dev;
306 (void)mtu;
307 return -1;
308 }
309
310 int rist_tun_bring_up(const char *dev)
311 {
312 (void)dev;
313 return -1;
314 }
315
316 #endif /* defined(__APPLE__) && TARGET_OS_OSX / embedded Apple */