+#ifdef __sun__
+static int link_proto(int unit_nr, const char *devname, uint64_t flags)
+{
+ int ip_fd, mux_id, tun2_fd;
+ struct lifreq ifr;
+
+ tun2_fd = open("/dev/tun", O_RDWR);
+ if (tun2_fd < 0) {
+ perror(_("Could not open /dev/tun for plumbing"));
+ return -EIO;
+ }
+ if (ioctl(tun2_fd, I_PUSH, "ip") < 0) {
+ perror(_("Can't push IP"));
+ close(tun2_fd);
+ return -EIO;
+ }
+
+ sprintf(ifr.lifr_name, "tun%d", unit_nr);
+ ifr.lifr_ppa = unit_nr;
+ ifr.lifr_flags = flags;
+
+ if (ioctl(tun2_fd, SIOCSLIFNAME, &ifr) < 0) {
+ perror(_("Can't set ifname"));
+ close(tun2_fd);
+ return -1;
+ }
+
+ ip_fd = open(devname, O_RDWR);
+ if (ip_fd < 0) {
+ fprintf(stderr, _("Can't open %s: %s"), devname,
+ strerror(errno));
+ close(tun2_fd);
+ return -1;
+ }
+
+ mux_id = ioctl(ip_fd, I_LINK, tun2_fd);
+ if (mux_id < 0) {
+ fprintf(stderr, _("Can't plumb %s for IPv%d: %s\n"),
+ ifr.lifr_name, (flags == IFF_IPV4) ? 4 : 6,
+ strerror(errno));
+ close(tun2_fd);
+ close(ip_fd);
+ return -1;
+ }
+
+ close(tun2_fd);
+
+ return ip_fd;
+}
+#endif
+
+#ifdef SIOCIFCREATE
+static int bsd_open_tun(char *tun_name)
+{
+ int fd;
+ int s;
+ struct ifreq ifr;
+
+ fd = open(tun_name, O_RDWR);
+ if (fd >= 0) {
+ return fd;
+
+ s = socket(AF_INET, SOCK_DGRAM, 0);
+ if (s < 0)
+ return -1;
+
+ memset(&ifr, 0, sizeof(ifr));
+ strncpy(ifr.ifr_name, tun_name + 5, sizeof(ifr.ifr_name) - 1);
+ if (!ioctl(s, SIOCIFCREATE, &ifr))
+ fd = open(tun_name, O_RDWR);
+
+ close(s);
+ }
+ return fd;
+}
+#else
+#define bsd_open_tun(tun_name) open(tun_name, O_RDWR)
+#endif
+
+static int os_setup_tun(struct openconnect_info *vpninfo)
+{
+ int tun_fd = -1;
+
+#ifdef IFF_TUN /* Linux */
+ struct ifreq ifr;
+ int tunerr;
+
+ tun_fd = open("/dev/net/tun", O_RDWR);
+ if (tun_fd < 0) {
+ /* Android has /dev/tun instead of /dev/net/tun
+ Since other systems might have too, just try it
+ as a fallback instead of using ifdef __ANDROID__ */
+ tunerr = errno;
+ tun_fd = open("/dev/tun", O_RDWR);
+ }
+ if (tun_fd < 0) {
+ /* If the error on /dev/tun is ENOENT, that's boring.
+ Use the error we got on /dev/net/tun instead */
+ if (errno != ENOENT)
+ tunerr = errno;
+
+ vpn_progress(vpninfo, PRG_ERR,
+ _("Failed to open tun device: %s\n"),
+ strerror(tunerr));
+ exit(1);
+ }
+ memset(&ifr, 0, sizeof(ifr));
+ ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
+ if (vpninfo->ifname)
+ strncpy(ifr.ifr_name, vpninfo->ifname,
+ sizeof(ifr.ifr_name) - 1);
+ if (ioctl(tun_fd, TUNSETIFF, (void *) &ifr) < 0) {
+ vpn_progress(vpninfo, PRG_ERR,
+ _("TUNSETIFF failed: %s\n"),
+ strerror(errno));
+ exit(1);
+ }
+ if (!vpninfo->ifname)
+ vpninfo->ifname = strdup(ifr.ifr_name);
+#elif defined (__sun__)
+ static char tun_name[80];
+ int unit_nr;
+
+ tun_fd = open("/dev/tun", O_RDWR);
+ if (tun_fd < 0) {
+ perror(_("open /dev/tun"));
+ return -EIO;
+ }
+
+ unit_nr = ioctl(tun_fd, TUNNEWPPA, -1);
+ if (unit_nr < 0) {
+ perror(_("Failed to create new tun"));
+ close(tun_fd);
+ return -EIO;
+ }
+
+ if (ioctl(tun_fd, I_SRDOPT, RMSGD) < 0) {
+ perror(_("Failed to put tun file descriptor into message-discard mode"));
+ close(tun_fd);
+ return -EIO;
+ }
+
+ sprintf(tun_name, "tun%d", unit_nr);
+ vpninfo->ifname = strdup(tun_name);
+
+ vpninfo->ip_fd = link_proto(unit_nr, "/dev/udp", IFF_IPV4);
+ if (vpninfo->ip_fd < 0) {
+ close(tun_fd);
+ return -EIO;
+ }
+
+ if (vpninfo->vpn_addr6) {
+ vpninfo->ip6_fd = link_proto(unit_nr, "/dev/udp6", IFF_IPV6);
+ if (vpninfo->ip6_fd < 0) {
+ close(tun_fd);
+ close(vpninfo->ip_fd);
+ vpninfo->ip_fd = -1;
+ return -EIO;
+ }
+ } else
+ vpninfo->ip6_fd = -1;
+
+#else /* BSD et al have /dev/tun$x devices */
+ static char tun_name[80];
+ int i;
+
+ if (vpninfo->ifname) {
+ char *endp = NULL;
+ if (strncmp(vpninfo->ifname, "tun", 3) ||
+ ((void)strtol(vpninfo->ifname + 3, &endp, 10), !endp) ||
+ *endp) {
+ vpn_progress(vpninfo, PRG_ERR,
+ _("Invalid interface name '%s'; must match 'tun%%d'\n"),
+ vpninfo->ifname);
+ return -EINVAL;
+ }
+ snprintf(tun_name, sizeof(tun_name),
+ "/dev/%s", vpninfo->ifname);
+ tun_fd = bsd_open_tun(tun_name);
+ if (tun_fd < 0) {
+ int err = errno;
+ vpn_progress(vpninfo, PRG_ERR,
+ _("Cannot open '%s': %s\n"),
+ tun_name, strerror(err));
+ return -EINVAL;
+ }
+ }
+#ifdef HAVE_FDEVNAME_R
+ /* We don't have to iterate over the possible devices; on FreeBSD
+ at least, opening /dev/tun will give us the next available
+ device. */
+ if (tun_fd < 0) {
+ tun_fd = open("/dev/tun", O_RDWR);
+ if (tun_fd >= 0) {
+ if (!fdevname_r(tun_fd, tun_name, sizeof(tun_name)) ||
+ strncmp(tun_name, "tun", 3)) {
+ close(tun_fd);
+ tun_fd = -1;
+ } else
+ vpninfo->ifname = strdup(tun_name);
+ }
+ }
+#endif
+ if (tun_fd < 0) {
+ for (i = 0; i < 255; i++) {
+ sprintf(tun_name, "/dev/tun%d", i);
+ tun_fd = bsd_open_tun(tun_name);
+ if (tun_fd >= 0)
+ break;
+ }
+ if (tun_fd < 0) {
+ perror(_("open tun"));
+ exit(1);
+ }
+ vpninfo->ifname = strdup(tun_name + 5);
+ }
+#ifdef TUNSIFHEAD
+ i = 1;
+ if (ioctl(tun_fd, TUNSIFHEAD, &i) < 0) {
+ perror(_("TUNSIFHEAD"));
+ exit(1);
+ }
+#endif
+#endif
+ return tun_fd;
+}