^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <sys/wait.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <net_user.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <os.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include "slirp.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) static int slirp_user_init(void *data, void *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) struct slirp_data *pri = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) pri->dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) struct slirp_pre_exec_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) int stdin_fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) int stdout_fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) static void slirp_pre_exec(void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) struct slirp_pre_exec_data *data = arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) if (data->stdin_fd != -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) dup2(data->stdin_fd, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) if (data->stdout_fd != -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) dup2(data->stdout_fd, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) static int slirp_tramp(char **argv, int fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) struct slirp_pre_exec_data pe_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) int pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) pe_data.stdin_fd = fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) pe_data.stdout_fd = fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) pid = run_helper(slirp_pre_exec, &pe_data, argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) return pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) static int slirp_open(void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) struct slirp_data *pri = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) int fds[2], pid, err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) err = os_pipe(fds, 1, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) err = slirp_tramp(pri->argw.argv, fds[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) printk(UM_KERN_ERR "slirp_tramp failed - errno = %d\n", -err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) pid = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) pri->slave = fds[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) pri->slip.pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) pri->slip.esc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) pri->pid = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) return fds[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) close(fds[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) close(fds[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) static void slirp_close(int fd, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) struct slirp_data *pri = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) close(pri->slave);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) pri->slave = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) if (pri->pid<1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) printk(UM_KERN_ERR "slirp_close: no child process to shut "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) "down\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) #if 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) if (kill(pri->pid, SIGHUP)<0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) printk(UM_KERN_ERR "slirp_close: sending hangup to %d failed "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) "(%d)\n", pri->pid, errno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) err = helper_wait(pri->pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) pri->pid = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) int slirp_user_read(int fd, void *buf, int len, struct slirp_data *pri)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) return slip_proto_read(fd, buf, len, &pri->slip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) int slirp_user_write(int fd, void *buf, int len, struct slirp_data *pri)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) return slip_proto_write(fd, buf, len, &pri->slip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) const struct net_user_info slirp_user_info = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) .init = slirp_user_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) .open = slirp_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) .close = slirp_close,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) .remove = NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) .add_address = NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) .delete_address = NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) .mtu = BUF_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) .max_packet = BUF_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) };