+
+static int task(const char *desc) {
+ pid_t pid, pidr;
+ int status;
+
+ pid= fork();
+ if (pid == (pid_t)-1) sysfatal("fork for task");
+ if (!pid) return 1;
+
+ for (;;) {
+ pidr= waitpid(pid,&status,0);
+ if (pidr!=(pid_t)-1) break;
+ if (errno==EINTR) continue;
+ sysfatal("waitpid for task");
+ }
+ assert(pidr==pid);
+
+ if (WIFEXITED(status)) {
+ if (WEXITSTATUS(status))
+ fatal("userv-ipif service: %s exited with error exit status %d\n",
+ desc, WEXITSTATUS(status));
+ } else if (WIFSIGNALED(status)) {
+ fatal("userv-ipif service: %s died due to signal %s%s\n",
+ desc, strsignal(WTERMSIG(status)),
+ WCOREDUMP(status) ? " (core dumped)" : "");
+ } else {
+ fatal("userv-ipif service: %s unexpectedly terminated"
+ " with unknown status code %d\n", desc, status);
+ }
+
+ return 0;
+}
+
+static void createif(void) {
+ static const char ifnamepat[]= "userv%d";
+ struct ifreq ifr;
+ int r;
+
+ memset(&ifr,0,sizeof(ifr));
+ ifr.ifr_flags= IFF_TUN | IFF_NO_PI;
+
+ assert(sizeof(ifr.ifr_name) >= sizeof(ifnamepat));
+ strcpy(ifr.ifr_name, ifnamepat);
+
+ tunfd= open("/dev/net/tun", O_RDWR);
+ if (!tunfd) sysfatal("open /dev/net/tun");
+
+ r= fcntl(tunfd, F_GETFD);
+ if (r==-1) sysfatal("fcntl(tunfd,F_GETFD)");
+ r= fcntl(tunfd, F_SETFD, r|FD_CLOEXEC);
+ if (r==-1) sysfatal("fcntl(tunfd,F_SETFD,|FD_CLOEXEC)");
+
+ r= ioctl(tunfd, TUNSETIFF, (void*)&ifr);
+ if (r) sysfatal("ioctl TUNSETIFF");
+
+ /* ifr.ifr_name might not be null-terminated. crazy abi. */
+ ifname= malloc(sizeof(ifr.ifr_name)+1);
+ if (!ifname) sysfatal("malloc for interface name");
+ memcpy(ifname, ifr.ifr_name, sizeof(ifr.ifr_name));
+ ifname[sizeof(ifr.ifr_name)]= 0;
+}
+
+static void netconfigure(void) {
+ char mtutxt[100];
+ int i;
+
+ if (task("ifconfig")) {
+ sprintf(mtutxt,"%lu",mtu);
+
+ execlp("ifconfig", "ifconfig", ifname, localtxt,
+ "netmask","255.255.255.255", "pointopoint",peertxt, "-broadcast",
+ "mtu",mtutxt, "up", (char*)0);
+ sysfatal("cannot exec ifconfig");
+ }
+
+ for (i=0; i<nexroutes; i++) {
+ if (task("route")) {
+ execlp("route","route", "add", "-net",exroutes[i].prefixtxt,
+ "netmask",exroutes[i].masktxt,
+ "gw",peertxt, "dev",ifname, (char*)0);
+ sysfatal("cannot exec route (for route)");
+ }
+ }
+}
+
+static void setnonblock(int fd) {
+ int r;
+ r= fcntl(fd,F_GETFL);
+ if (r==-1) sysfatal("fcntl F_GETFL");
+ r= fcntl(fd,F_SETFL, r|O_NONBLOCK);
+ if (r==-1) sysfatal("fcntl F_SETFL O_NONBLOCK");
+}
+
+static void rx_packet(const uint8_t *packet, int len) {
+ if (!len)
+ return;
+ for (;;) {
+ int r= write(tunfd, packet, len);
+ if (r<0) {
+ if (errno==EINTR) continue;
+ if (errno==EAGAIN || errno==ENOMEM) return; /* oh well */
+ sysfatal("error writing packet to tun (transmitting)");
+ }
+ assert(r==len);
+ return;
+ }
+}
+
+static int output_waiting, input_waiting;
+
+#define SLIP_END 0300
+#define SLIP_ESC 0333
+#define SLIP_ESC_END 0334
+#define SLIP_ESC_ESC 0335
+
+static void more_rx_data(uint8_t *input_buf, uint8_t *output_buf) {
+ /* we make slip_data never contain continuation of a packet */
+ /* input_buf is passed as a parameter since it's in copydata's stack frame */
+ static int scanned;
+ static int output_len;
+
+ uint8_t *op= output_buf + output_len;
+ const uint8_t *ip= input_buf + scanned;
+ const uint8_t *ip_end= input_buf + input_waiting;
+ int eaten= 0;
+
+ for (;;) {
+ if (ip>=ip_end) break;
+ uint8_t c= *ip++;
+ if (c==SLIP_END) {
+ rx_packet(output_buf, op-output_buf);
+ op= output_buf;
+ eaten= ip - input_buf;
+ continue;
+ }
+ if (c==SLIP_ESC) {
+ if (ip>=ip_end) { /* rescan this when there's more */ ip--; break; }
+ c= *ip++;
+ if (c==SLIP_ESC_END) c=SLIP_END;
+ else if (c==SLIP_ESC_ESC) c=SLIP_ESC;
+ else fatal("unexpected byte 0%o after SLIP_ESC",c);
+ }
+ if (op == output_buf+mtu)
+ fatal("SLIP packet exceeds mtu");
+ *op++= c;
+ }
+
+ output_len= op - output_buf;
+ scanned= ip - input_buf;
+
+ input_waiting -= eaten;
+ memmove(input_buf, input_buf+eaten, input_waiting);
+ scanned -= eaten;
+}
+
+static void tx_packet(uint8_t *output_buf, const uint8_t *ip, int inlen) {
+ /* output_buf is passed as a parameter since it's in copydata's stack frame */
+ assert(!output_waiting);
+ uint8_t *op= output_buf;
+
+ *op++= SLIP_END;
+ while (inlen-- >0) {
+ uint8_t c= *ip++;
+ if (c==SLIP_END) { *op++= SLIP_ESC; *op++= SLIP_ESC_END; }
+ else if (c==SLIP_ESC) { *op++= SLIP_ESC; *op++= SLIP_ESC_ESC; }
+ else *op++= c;
+ }
+ *op++= SLIP_END;
+ assert(op <= output_buf + mtu*2+2);
+
+ output_waiting= op - output_buf;
+}
+
+static void copydata(void) __attribute__((noreturn));
+static void copydata(void) {
+ uint8_t output_buf[mtu*2+2];
+ uint8_t input_buf[mtu*2+2];
+ uint8_t rx_packet_buf[mtu];
+
+ int r, i;
+
+ struct pollfd polls[3];
+ memset(polls, 0, sizeof(polls));
+
+ polls[0].fd= 0; polls[0].events= POLLIN;
+ polls[1].fd= 1;
+ polls[2].fd= tunfd;
+
+ /* We don't do flow control on input packets; instead, we just throw
+ * away ones which the kernel doesn't accept. So we always poll for
+ * those.
+ *
+ * Output packets we buffer, so we poll only as appropriate for those.
+ */
+
+ /* Start by transmitting one END byte to say we're ready. */
+ output_buf[0]= SLIP_END;
+ output_waiting= 1;
+
+ for (;;) {
+ if (output_waiting) {
+ r= write(1, output_buf, output_waiting);
+ if (r<0) {
+ if (errno==EINTR) continue;
+ if (errno!=EAGAIN)
+ sysfatal("error writing SLIP output (packets being received)");
+ } else {
+ assert(r>0);
+ output_waiting -= r;
+ memmove(output_buf, output_buf+r, output_waiting);
+ }
+ }
+ if (output_waiting) {
+ polls[1].events |= POLLOUT;
+ polls[2].events &= ~POLLIN;
+ } else {
+ polls[1].events &= ~POLLOUT;
+ polls[2].events |= POLLIN;
+ }
+ r= poll(polls,3,-1);
+
+ if (r<0) {
+ if (errno==EINTR) continue;
+ sysfatal("poll() failed");
+ }
+ assert(r>0); /* we used an infinite timeout */
+
+ for (i=0; i<sizeof(polls)/sizeof(polls[0]); i++)
+ if (polls[i].revents & ~polls[i].events)
+ fatal("unexpected revents 0x%x for fd=%d",
+ polls[i].revents, polls[i].fd);
+
+ if (polls[0].events & POLLIN) {
+ int want= sizeof(input_buf) - input_waiting;
+ if (want<0) fatal("incoming packet necessarily exceeds MTU");
+ r= read(0, input_buf + input_waiting, want);
+ if (r>0) {
+ input_waiting += r;
+ assert(input_waiting <= sizeof(input_buf));
+ more_rx_data(input_buf, rx_packet_buf);
+ } else if (r==0) {
+ terminate(0);
+ } else {
+ if (!(errno==EINTR || errno==EAGAIN))
+ sysfatal("error reading input SLIP data (packets to transmit)");
+ }
+ }
+
+ /* We handle what would be (polls[1].events & POLLOUT) above,
+ * unconditionally. That eliminates the need to poll in the usual case */
+
+ if (polls[2].events & POLLIN) {
+ uint8_t packet_buf[mtu];
+ r= read(tunfd, packet_buf, mtu);
+ if (r>0) {
+ tx_packet(output_buf, packet_buf, r);
+ } else {
+ assert(r<0);
+ if (!(errno==EAGAIN || errno==EWOULDBLOCK))
+ sysfatal("error reading packet (being transmitted) from tun");
+ }
+ }
+ }
+}
+