1 1.3 riastrad /* $NetBSD: wg_user.c,v 1.3 2020/08/27 02:51:15 riastradh Exp $ */ 2 1.1 riastrad 3 1.1 riastrad /* 4 1.1 riastrad * Copyright (C) Ryota Ozaki <ozaki.ryota (at) gmail.com> 5 1.1 riastrad * All rights reserved. 6 1.1 riastrad * 7 1.1 riastrad * Based on wg_user.c by Antti Kantee. 8 1.1 riastrad * 9 1.1 riastrad * Redistribution and use in source and binary forms, with or without 10 1.1 riastrad * modification, are permitted provided that the following conditions 11 1.1 riastrad * are met: 12 1.1 riastrad * 1. Redistributions of source code must retain the above copyright 13 1.1 riastrad * notice, this list of conditions and the following disclaimer. 14 1.1 riastrad * 2. Redistributions in binary form must reproduce the above copyright 15 1.1 riastrad * notice, this list of conditions and the following disclaimer in the 16 1.1 riastrad * documentation and/or other materials provided with the distribution. 17 1.1 riastrad * 18 1.1 riastrad * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 19 1.1 riastrad * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 20 1.1 riastrad * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 21 1.1 riastrad * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 1.1 riastrad * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 1.1 riastrad * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 24 1.1 riastrad * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 1.1 riastrad * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 1.1 riastrad * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 1.1 riastrad * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 1.1 riastrad * SUCH DAMAGE. 29 1.1 riastrad */ 30 1.1 riastrad 31 1.1 riastrad #include <sys/cdefs.h> 32 1.3 riastrad __KERNEL_RCSID(0, "$NetBSD: wg_user.c,v 1.3 2020/08/27 02:51:15 riastradh Exp $"); 33 1.1 riastrad 34 1.1 riastrad #ifndef _KERNEL 35 1.1 riastrad #include <sys/types.h> 36 1.1 riastrad #include <sys/ioctl.h> 37 1.1 riastrad #include <sys/uio.h> 38 1.1 riastrad #include <sys/socket.h> 39 1.1 riastrad #include <sys/param.h> 40 1.1 riastrad 41 1.1 riastrad #include <net/if.h> 42 1.1 riastrad #include <net/if_tun.h> 43 1.1 riastrad 44 1.1 riastrad #include <netinet/in.h> 45 1.1 riastrad 46 1.1 riastrad #include <assert.h> 47 1.1 riastrad #include <errno.h> 48 1.1 riastrad #include <fcntl.h> 49 1.1 riastrad #include <inttypes.h> 50 1.1 riastrad #include <poll.h> 51 1.1 riastrad #include <pthread.h> 52 1.1 riastrad #include <stdio.h> 53 1.1 riastrad #include <stdlib.h> 54 1.1 riastrad #include <string.h> 55 1.1 riastrad #include <unistd.h> 56 1.1 riastrad 57 1.1 riastrad #include <rump/rumpuser_component.h> 58 1.1 riastrad 59 1.1 riastrad #include "wg_user.h" 60 1.1 riastrad 61 1.1 riastrad struct wg_user { 62 1.1 riastrad struct wg_softc *wgu_sc; 63 1.1 riastrad int wgu_devnum; 64 1.1 riastrad char wgu_tun_name[IFNAMSIZ]; 65 1.1 riastrad 66 1.1 riastrad int wgu_fd; 67 1.1 riastrad int wgu_sock4; 68 1.1 riastrad int wgu_sock6; 69 1.1 riastrad int wgu_pipe[2]; 70 1.1 riastrad pthread_t wgu_rcvthr; 71 1.1 riastrad 72 1.1 riastrad int wgu_dying; 73 1.1 riastrad 74 1.1 riastrad char wgu_rcvbuf[9018]; /* jumbo frame max len */ 75 1.1 riastrad }; 76 1.1 riastrad 77 1.1 riastrad static int 78 1.1 riastrad open_tun(const char *tun_name) 79 1.1 riastrad { 80 1.1 riastrad char tun_path[MAXPATHLEN]; 81 1.1 riastrad int n, fd, error; 82 1.1 riastrad 83 1.1 riastrad n = snprintf(tun_path, sizeof(tun_path), "/dev/%s", tun_name); 84 1.1 riastrad if (n == MAXPATHLEN) 85 1.1 riastrad return E2BIG; 86 1.1 riastrad 87 1.1 riastrad fd = open(tun_path, O_RDWR); 88 1.1 riastrad if (fd == -1) { 89 1.1 riastrad fprintf(stderr, "%s: can't open %s: %s\n", 90 1.1 riastrad __func__, tun_name, strerror(errno)); 91 1.1 riastrad } 92 1.1 riastrad 93 1.1 riastrad int i = 1; 94 1.1 riastrad error = ioctl(fd, TUNSLMODE, &i); 95 1.1 riastrad if (error == -1) { 96 1.1 riastrad close(fd); 97 1.1 riastrad fd = -1; 98 1.1 riastrad } 99 1.1 riastrad 100 1.1 riastrad return fd; 101 1.1 riastrad } 102 1.1 riastrad 103 1.1 riastrad static void 104 1.1 riastrad close_tun(struct wg_user *wgu) 105 1.1 riastrad { 106 1.1 riastrad int s; 107 1.1 riastrad struct ifreq ifr = {}; 108 1.1 riastrad 109 1.1 riastrad close(wgu->wgu_fd); 110 1.1 riastrad 111 1.1 riastrad s = socket(AF_INET, SOCK_DGRAM, 0); 112 1.1 riastrad if (s == -1) 113 1.1 riastrad return; /* XXX */ 114 1.1 riastrad strcpy(ifr.ifr_name, wgu->wgu_tun_name); 115 1.1 riastrad (void)ioctl(s, SIOCIFDESTROY, &ifr); 116 1.1 riastrad close(s); 117 1.1 riastrad } 118 1.1 riastrad 119 1.1 riastrad static void * 120 1.1 riastrad wg_user_rcvthread(void *aaargh) 121 1.1 riastrad { 122 1.1 riastrad struct wg_user *wgu = aaargh; 123 1.1 riastrad struct pollfd pfd[4]; 124 1.1 riastrad ssize_t nn = 0; 125 1.1 riastrad int prv; 126 1.1 riastrad 127 1.1 riastrad rumpuser_component_kthread(); 128 1.1 riastrad 129 1.1 riastrad pfd[0].fd = wgu->wgu_fd; 130 1.1 riastrad pfd[0].events = POLLIN; 131 1.1 riastrad pfd[1].fd = wgu->wgu_pipe[0]; 132 1.1 riastrad pfd[1].events = POLLIN; 133 1.1 riastrad pfd[2].fd = wgu->wgu_sock4; 134 1.1 riastrad pfd[2].events = POLLIN; 135 1.1 riastrad pfd[3].fd = wgu->wgu_sock6; 136 1.1 riastrad pfd[3].events = POLLIN; 137 1.1 riastrad 138 1.1 riastrad while (!wgu->wgu_dying) { 139 1.1 riastrad struct iovec iov[2]; 140 1.1 riastrad 141 1.1 riastrad prv = poll(pfd, 4, -1); 142 1.1 riastrad if (prv == 0) 143 1.1 riastrad continue; 144 1.1 riastrad if (prv == -1) { 145 1.1 riastrad /* XXX */ 146 1.1 riastrad fprintf(stderr, "%s: poll error: %d\n", 147 1.1 riastrad wgu->wgu_tun_name, errno); 148 1.1 riastrad sleep(1); 149 1.1 riastrad continue; 150 1.1 riastrad } 151 1.1 riastrad if (pfd[1].revents & POLLIN) 152 1.1 riastrad continue; 153 1.1 riastrad 154 1.1 riastrad /* Receive user packets from tun */ 155 1.1 riastrad if (pfd[0].revents & POLLIN) { 156 1.1 riastrad nn = read(wgu->wgu_fd, wgu->wgu_rcvbuf, sizeof(wgu->wgu_rcvbuf)); 157 1.1 riastrad if (nn == -1 && errno == EAGAIN) 158 1.1 riastrad continue; 159 1.1 riastrad 160 1.1 riastrad if (nn < 1) { 161 1.1 riastrad /* XXX */ 162 1.1 riastrad fprintf(stderr, "%s: receive failed\n", 163 1.1 riastrad wgu->wgu_tun_name); 164 1.1 riastrad sleep(1); 165 1.1 riastrad continue; 166 1.1 riastrad } 167 1.1 riastrad 168 1.1 riastrad iov[0].iov_base = wgu->wgu_rcvbuf; 169 1.1 riastrad iov[0].iov_len = ((struct sockaddr *)wgu->wgu_rcvbuf)->sa_len; 170 1.1 riastrad 171 1.1 riastrad iov[1].iov_base = (char *)wgu->wgu_rcvbuf + iov[0].iov_len; 172 1.1 riastrad iov[1].iov_len = nn - iov[0].iov_len; 173 1.1 riastrad 174 1.1 riastrad rumpuser_component_schedule(NULL); 175 1.1 riastrad rumpkern_wg_recv_user(wgu->wgu_sc, iov, 2); 176 1.1 riastrad rumpuser_component_unschedule(); 177 1.1 riastrad } 178 1.1 riastrad 179 1.1 riastrad /* Receive wg UDP/IPv4 packets from a peer */ 180 1.1 riastrad if (pfd[2].revents & POLLIN) { 181 1.1 riastrad struct sockaddr_in sin; 182 1.1 riastrad socklen_t len = sizeof(sin); 183 1.1 riastrad nn = recvfrom(wgu->wgu_sock4, wgu->wgu_rcvbuf, 184 1.1 riastrad sizeof(wgu->wgu_rcvbuf), 0, (struct sockaddr *)&sin, 185 1.1 riastrad &len); 186 1.1 riastrad if (nn == -1 && errno == EAGAIN) 187 1.1 riastrad continue; 188 1.1 riastrad if (len != sizeof(sin)) 189 1.1 riastrad continue; 190 1.1 riastrad iov[0].iov_base = &sin; 191 1.1 riastrad iov[0].iov_len = sin.sin_len; 192 1.1 riastrad 193 1.1 riastrad iov[1].iov_base = wgu->wgu_rcvbuf; 194 1.1 riastrad iov[1].iov_len = nn; 195 1.1 riastrad 196 1.1 riastrad rumpuser_component_schedule(NULL); 197 1.1 riastrad rumpkern_wg_recv_peer(wgu->wgu_sc, iov, 2); 198 1.1 riastrad rumpuser_component_unschedule(); 199 1.1 riastrad } 200 1.1 riastrad 201 1.1 riastrad /* Receive wg UDP/IPv6 packets from a peer */ 202 1.1 riastrad if (pfd[3].revents & POLLIN) { 203 1.1 riastrad struct sockaddr_in6 sin6; 204 1.1 riastrad socklen_t len = sizeof(sin6); 205 1.1 riastrad nn = recvfrom(wgu->wgu_sock6, wgu->wgu_rcvbuf, 206 1.1 riastrad sizeof(wgu->wgu_rcvbuf), 0, (struct sockaddr *)&sin6, 207 1.1 riastrad &len); 208 1.1 riastrad if (nn == -1 && errno == EAGAIN) 209 1.1 riastrad continue; 210 1.1 riastrad if (len != sizeof(sin6)) 211 1.1 riastrad continue; 212 1.1 riastrad iov[0].iov_base = &sin6; 213 1.1 riastrad iov[0].iov_len = sin6.sin6_len; 214 1.1 riastrad 215 1.1 riastrad iov[1].iov_base = wgu->wgu_rcvbuf; 216 1.1 riastrad iov[1].iov_len = nn; 217 1.1 riastrad 218 1.1 riastrad rumpuser_component_schedule(NULL); 219 1.1 riastrad rumpkern_wg_recv_peer(wgu->wgu_sc, iov, 2); 220 1.1 riastrad rumpuser_component_unschedule(); 221 1.1 riastrad } 222 1.1 riastrad } 223 1.1 riastrad 224 1.1 riastrad assert(wgu->wgu_dying); 225 1.1 riastrad 226 1.1 riastrad rumpuser_component_kthread_release(); 227 1.1 riastrad return NULL; 228 1.1 riastrad } 229 1.1 riastrad 230 1.1 riastrad int 231 1.1 riastrad rumpuser_wg_create(const char *tun_name, struct wg_softc *wg, 232 1.1 riastrad struct wg_user **wgup) 233 1.1 riastrad { 234 1.1 riastrad struct wg_user *wgu = NULL; 235 1.1 riastrad void *cookie; 236 1.1 riastrad int rv; 237 1.1 riastrad 238 1.1 riastrad cookie = rumpuser_component_unschedule(); 239 1.1 riastrad 240 1.1 riastrad wgu = malloc(sizeof(*wgu)); 241 1.1 riastrad if (wgu == NULL) { 242 1.1 riastrad rv = errno; 243 1.1 riastrad goto oerr1; 244 1.1 riastrad } 245 1.1 riastrad 246 1.2 riastrad if (strlcpy(wgu->wgu_tun_name, tun_name, sizeof(wgu->wgu_tun_name)) 247 1.2 riastrad >= sizeof(wgu->wgu_tun_name)) { 248 1.2 riastrad rv = EINVAL; 249 1.2 riastrad goto oerr2; 250 1.2 riastrad } 251 1.2 riastrad wgu->wgu_sc = wg; 252 1.2 riastrad 253 1.1 riastrad wgu->wgu_fd = open_tun(tun_name); 254 1.1 riastrad if (wgu->wgu_fd == -1) { 255 1.1 riastrad rv = errno; 256 1.1 riastrad goto oerr2; 257 1.1 riastrad } 258 1.1 riastrad 259 1.1 riastrad if (pipe(wgu->wgu_pipe) == -1) { 260 1.1 riastrad rv = errno; 261 1.1 riastrad goto oerr3; 262 1.1 riastrad } 263 1.1 riastrad 264 1.1 riastrad wgu->wgu_sock4 = socket(AF_INET, SOCK_DGRAM, 0); 265 1.1 riastrad wgu->wgu_sock6 = socket(AF_INET6, SOCK_DGRAM, 0); 266 1.1 riastrad if (wgu->wgu_sock4 == -1 || wgu->wgu_sock6 == -1) { 267 1.1 riastrad rv = errno; 268 1.1 riastrad goto oerr4; 269 1.1 riastrad } 270 1.1 riastrad 271 1.1 riastrad rv = pthread_create(&wgu->wgu_rcvthr, NULL, wg_user_rcvthread, wgu); 272 1.1 riastrad if (rv != 0) 273 1.1 riastrad goto oerr5; 274 1.1 riastrad 275 1.1 riastrad rumpuser_component_schedule(cookie); 276 1.1 riastrad *wgup = wgu; 277 1.1 riastrad return 0; 278 1.1 riastrad 279 1.1 riastrad oerr5: 280 1.1 riastrad if (wgu->wgu_sock4 != -1) 281 1.1 riastrad close(wgu->wgu_sock4); 282 1.1 riastrad if (wgu->wgu_sock6 != -1) 283 1.1 riastrad close(wgu->wgu_sock6); 284 1.3 riastrad oerr4: 285 1.3 riastrad close(wgu->wgu_pipe[0]); 286 1.3 riastrad close(wgu->wgu_pipe[1]); 287 1.1 riastrad oerr3: 288 1.1 riastrad close_tun(wgu); 289 1.1 riastrad oerr2: 290 1.1 riastrad free(wgu); 291 1.1 riastrad oerr1: 292 1.1 riastrad rumpuser_component_schedule(cookie); 293 1.1 riastrad return rumpuser_component_errtrans(rv); 294 1.1 riastrad } 295 1.1 riastrad 296 1.1 riastrad /* 297 1.1 riastrad * Send decrypted packets to users via a tun. 298 1.1 riastrad */ 299 1.1 riastrad void 300 1.1 riastrad rumpuser_wg_send_user(struct wg_user *wgu, struct iovec *iov, size_t iovlen) 301 1.1 riastrad { 302 1.1 riastrad void *cookie = rumpuser_component_unschedule(); 303 1.1 riastrad ssize_t idontcare __attribute__((__unused__)); 304 1.1 riastrad 305 1.1 riastrad /* 306 1.1 riastrad * no need to check for return value; packets may be dropped 307 1.1 riastrad * 308 1.1 riastrad * ... sorry, I spoke too soon. We need to check it because 309 1.1 riastrad * apparently gcc reinvented const poisoning and it's very 310 1.1 riastrad * hard to say "thanks, I know I'm not using the result, 311 1.1 riastrad * but please STFU and let's get on with something useful". 312 1.1 riastrad * So let's trick gcc into letting us share the compiler 313 1.1 riastrad * experience. 314 1.1 riastrad */ 315 1.1 riastrad idontcare = writev(wgu->wgu_fd, iov, iovlen); 316 1.1 riastrad 317 1.1 riastrad rumpuser_component_schedule(cookie); 318 1.1 riastrad } 319 1.1 riastrad 320 1.1 riastrad /* 321 1.1 riastrad * Send wg messages to a peer. 322 1.1 riastrad */ 323 1.1 riastrad int 324 1.1 riastrad rumpuser_wg_send_peer(struct wg_user *wgu, struct sockaddr *sa, 325 1.1 riastrad struct iovec *iov, size_t iovlen) 326 1.1 riastrad { 327 1.1 riastrad void *cookie = rumpuser_component_unschedule(); 328 1.1 riastrad int s, error = 0; 329 1.1 riastrad size_t i; 330 1.1 riastrad ssize_t sent; 331 1.1 riastrad 332 1.1 riastrad if (sa->sa_family == AF_INET) 333 1.1 riastrad s = wgu->wgu_sock4; 334 1.1 riastrad else 335 1.1 riastrad s = wgu->wgu_sock6; 336 1.1 riastrad 337 1.1 riastrad for (i = 0; i < iovlen; i++) { 338 1.1 riastrad sent = sendto(s, iov[i].iov_base, iov[i].iov_len, 0, sa, 339 1.1 riastrad sa->sa_len); 340 1.1 riastrad if (sent == -1 || (size_t)sent != iov[i].iov_len) { 341 1.1 riastrad error = errno; 342 1.1 riastrad break; 343 1.1 riastrad } 344 1.1 riastrad } 345 1.1 riastrad 346 1.1 riastrad rumpuser_component_schedule(cookie); 347 1.1 riastrad 348 1.1 riastrad return error; 349 1.1 riastrad } 350 1.1 riastrad 351 1.1 riastrad int 352 1.1 riastrad rumpuser_wg_ioctl(struct wg_user *wgu, u_long cmd, void *data, int af) 353 1.1 riastrad { 354 1.1 riastrad void *cookie = rumpuser_component_unschedule(); 355 1.1 riastrad int s, error; 356 1.1 riastrad 357 1.1 riastrad s = socket(af, SOCK_DGRAM, 0); 358 1.1 riastrad if (s == -1) 359 1.1 riastrad return errno; 360 1.1 riastrad error = ioctl(s, cmd, data); 361 1.1 riastrad close(s); 362 1.1 riastrad 363 1.1 riastrad rumpuser_component_schedule(cookie); 364 1.1 riastrad 365 1.1 riastrad return error == -1 ? errno : 0; 366 1.1 riastrad } 367 1.1 riastrad 368 1.1 riastrad int 369 1.1 riastrad rumpuser_wg_sock_bind(struct wg_user *wgu, const uint16_t port) 370 1.1 riastrad { 371 1.1 riastrad int error; 372 1.1 riastrad struct sockaddr_in sin; 373 1.1 riastrad struct sockaddr_in6 sin6; 374 1.1 riastrad 375 1.1 riastrad memset(&sin, 0, sizeof(sin)); 376 1.1 riastrad sin.sin_family = AF_INET; 377 1.1 riastrad sin.sin_len = sizeof(sin); 378 1.1 riastrad sin.sin_addr.s_addr = INADDR_ANY; 379 1.1 riastrad sin.sin_port = htons(port); 380 1.1 riastrad 381 1.1 riastrad error = bind(wgu->wgu_sock4, (struct sockaddr *)&sin, sizeof(sin)); 382 1.1 riastrad if (error == -1) 383 1.1 riastrad return errno; 384 1.1 riastrad 385 1.1 riastrad memset(&sin6, 0, sizeof(sin6)); 386 1.1 riastrad sin6.sin6_family = AF_INET6; 387 1.1 riastrad sin6.sin6_len = sizeof(sin6); 388 1.1 riastrad sin6.sin6_addr = in6addr_any; 389 1.1 riastrad sin6.sin6_port = htons(port); 390 1.1 riastrad 391 1.1 riastrad error = bind(wgu->wgu_sock6, (struct sockaddr *)&sin6, sizeof(sin6)); 392 1.1 riastrad if (error == -1) 393 1.1 riastrad return errno; 394 1.1 riastrad 395 1.1 riastrad return 0; 396 1.1 riastrad } 397 1.1 riastrad 398 1.1 riastrad void 399 1.1 riastrad rumpuser_wg_destroy(struct wg_user *wgu) 400 1.1 riastrad { 401 1.1 riastrad void *cookie = rumpuser_component_unschedule(); 402 1.1 riastrad 403 1.1 riastrad wgu->wgu_dying = 1; 404 1.1 riastrad if (write(wgu->wgu_pipe[1], 405 1.1 riastrad &wgu->wgu_dying, sizeof(wgu->wgu_dying)) == -1) { 406 1.1 riastrad /* 407 1.1 riastrad * this is here mostly to avoid a compiler warning 408 1.1 riastrad * about ignoring the return value of write() 409 1.1 riastrad */ 410 1.1 riastrad fprintf(stderr, "%s: failed to signal thread\n", 411 1.1 riastrad wgu->wgu_tun_name); 412 1.1 riastrad } 413 1.1 riastrad pthread_join(wgu->wgu_rcvthr, NULL); 414 1.1 riastrad close_tun(wgu); 415 1.1 riastrad close(wgu->wgu_pipe[0]); 416 1.1 riastrad close(wgu->wgu_pipe[1]); 417 1.1 riastrad free(wgu); 418 1.1 riastrad 419 1.1 riastrad rumpuser_component_schedule(cookie); 420 1.1 riastrad } 421 1.1 riastrad 422 1.1 riastrad char * 423 1.1 riastrad rumpuser_wg_get_tunname(struct wg_user *wgu) 424 1.1 riastrad { 425 1.1 riastrad 426 1.1 riastrad return wgu->wgu_tun_name; 427 1.1 riastrad } 428 1.1 riastrad #endif 429