title: How to solve ioctl operation not permitted?
body: <p>I want to open a virtual interface tun, but when I use the function ioctl (ioctl(fd,TUNSETIFF,&amp;ifr)), it always returns <em>ioctl operation not permitted</em>.</p>
  
  <p>In case 1, ioctl is pass (it uses fd-socket, ioctl-SIOCGIFFLAGS):</p>
  
  <pre><code>jint fd = socket(AF_INET, SOCK_DGRAM, 0);
  __android_log_print(ANDROID_LOG_INFO, "MYPROG", "fd = %d, %s", fd, strerror(errno));
  
  
  memset(&amp;ifr, 0, sizeof(ifr));
  ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
  
  char tunnel[] = "tun";
  strncpy(ifr.ifr_name, tunnel, IFNAMSIZ);
  
  if (ioctl(fd, SIOCGIFFLAGS, (void *)&amp;ifr)&lt;0) {
      __android_log_print(ANDROID_LOG_INFO, "MYPROG", "errno = %d, %s", errno, strerror(errno));
      close(fd);
      exit(1);
  }
  
  return fd;
  </code></pre>
  
  <p>it show
  <a href="http://imgur.com/OWslnDa" rel="nofollow">http://imgur.com/OWslnDa</a></p>
  
  <p>But in case2, ioctl returns -1 (<em>operation not permitted</em>) (it uses fd-open, ioctl-TUNSETIFF).</p>
  
  <pre><code>jint fd = open("/dev/net/tun", O_RDWR | O_NONBLOCK);
  __android_log_print(ANDROID_LOG_INFO, "MYPROG", "fd = %d, %s", fd, strerror(errno));
  
  memset(&amp;ifr, 0, sizeof(ifr));
  ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
  
  char tunnel[]="tun";
  strncpy(ifr.ifr_name, tunnel, IFNAMSIZ);
  
  if (ioctl(fd,TUNSETIFF, (void *)&amp;ifr)&lt;0) {
      __android_log_print(ANDROID_LOG_INFO, "MYPROG", "errno = %d, %s", errno, strerror(errno));
      close(fd);
      exit(1);
  }
  
  return fd;
  </code></pre>
  
  <p>It shows</p>
  
  <p><a href="http://imgur.com/r9tNNiv" rel="nofollow">http://imgur.com/r9tNNiv</a></p>
  
  <p>How do I pass <code>ioctl</code> in case 2?</p>