NetBSD-Bugs archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: bin/39233: OpenSSH fails to initialize tun(4) tunnels correctly



The following reply was made to PR bin/39233; it has been noted by GNATS.

From: Taylor R Campbell <campbell%mumble.net@localhost>
To: gnats-bugs%NetBSD.org@localhost
Cc: gnats-admin%netbsd.org@localhost, netbsd-bugs%netbsd.org@localhost, 
christos%zoulas.com@localhost
Subject: Re: bin/39233: OpenSSH fails to initialize tun(4) tunnels correctly
Date: Tue, 16 Sep 2008 19:26:38 -0400

 This is a multi-part message in MIME format.
 --=_hd56iJmxf5ahDF81HRbAgCxJgXvYAdCd
 
 Attached is a patch that slightly adapts of the code one finds in
 portable OpenSSH's openbsd-compat/port-tun.c into misc.c.  I have
 lightly tested both point-to-point and bridge tunnels on a machine
 running NetBSD 4.0_STABLE talking with a machine running OpenBSD 4.3,
 and the patch applies to both netbsd-4 and HEAD.  The only difference
 from my last patch, really, is that there are no confused cpp feature
 conditionals and no auxiliary routines.
 
 --=_hd56iJmxf5ahDF81HRbAgCxJgXvYAdCd
 Content-Type: text/plain; charset="iso-8859-1"; name="ssh-tun"
 Content-Transfer-Encoding: quoted-printable
 Content-Disposition: attachment; filename="ssh-tun.patch"
 
 --- misc.c     15 Jun 2008 01:35:37 +0000      1.21
 +++ misc.c     16 Sep 2008 22:25:22 +0000=09
 @@ -33,6 +33,7 @@
  #include <sys/param.h>
 =20
  #include <net/if.h>
 +#include <net/if_tun.h>
  #include <netinet/in.h>
  #include <netinet/tcp.h>
 =20
 @@ -641,15 +642,20 @@
  {
        struct ifreq ifr;
        char name[100];
 -      int fd =3D -1, sock;
 +      int fd =3D -1, sock, flag;
 +      const char *tunbase =3D "tun";
 +
 +      if (mode =3D=3D SSH_TUNMODE_ETHERNET)
 +              tunbase =3D "tap";
 =20
        /* Open the tunnel device */
        if (tun <=3D SSH_TUNID_MAX) {
 -              snprintf(name, sizeof(name), "/dev/tun%d", tun);
 +              snprintf(name, sizeof(name), "/dev/%s%d", tunbase, tun);
                fd =3D open(name, O_RDWR);
        } else if (tun =3D=3D SSH_TUNID_ANY) {
                for (tun =3D 100; tun >=3D 0; tun--) {
 -                      snprintf(name, sizeof(name), "/dev/tun%d", tun);
 +                      snprintf(name, sizeof(name), "/dev/%s%d",
 +                          tunbase, tun);
                        if ((fd =3D open(name, O_RDWR)) >=3D 0)
                                break;
                }
 @@ -663,26 +669,24 @@
                return (-1);
        }
 =20
 +      /* Turn on tunnel headers */
 +      flag =3D 1;
 +      if (mode !=3D SSH_TUNMODE_ETHERNET &&
 +          ioctl(fd, TUNSIFHEAD, &flag) =3D=3D -1) {
 +              debug("%s: ioctl(%d, TUNSIFHEAD, 1): %s", __func__, fd,
 +                  strerror(errno));
 +              close(fd);
 +      }
 +
        debug("%s: %s mode %d fd %d", __func__, name, mode, fd);
 =20
        /* Set the tunnel device operation mode */
 -      snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "tun%d", tun);
 +      snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s%d", tunbase, tun);
        if ((sock =3D socket(PF_UNIX, SOCK_STREAM, 0)) =3D=3D -1)
                goto failed;
 =20
        if (ioctl(sock, SIOCGIFFLAGS, &ifr) =3D=3D -1)
                goto failed;
 -
 -      /* Set interface mode */
 -      ifr.ifr_flags &=3D ~IFF_UP;
 -      if (mode =3D=3D SSH_TUNMODE_ETHERNET)
 -              ifr.ifr_flags |=3D IFF_LINK0;
 -      else
 -              ifr.ifr_flags &=3D ~IFF_LINK0;
 -      if (ioctl(sock, SIOCSIFFLAGS, &ifr) =3D=3D -1)
 -              goto failed;
 -
 -      /* Bring interface up */
        ifr.ifr_flags |=3D IFF_UP;
        if (ioctl(sock, SIOCSIFFLAGS, &ifr) =3D=3D -1)
                goto failed;
 
 --=_hd56iJmxf5ahDF81HRbAgCxJgXvYAdCd--
 


Home | Main Index | Thread Index | Old Index