title: Android AOSP SIOCADDRT ioctl call fail: Network is unreachable
body: <p>AOSP: Android Pie (android-9.0.0_r16)</p>
  
  <p>Build: PQ1A.181105.017.A1</p>
  
  <p>Device: Pixel 2</p>
  
  <p>I am trying to connect cellular modem using dhcptool (renamed as dhcpdbg) with Android 9 Pie in RNDIS mode. The same setup worked fine for Previous versions of android but for Pie it fails on ioctl(3, SIOCADDRT, 0x7ffb9b8030). It says "Network is Unreachable". </p>
  
  <p>I have checked the port i.e usb0 in ifconfig, it enumerates correctly but the dhcpdbg call fails and the network is not pingable.</p>
  
  <p>Sepolicy on device is in permissive mode. Moreover, there are no permission or other errors in dmesg log.
  Note: By default RNDIS drivers are not configured in Android Pie kernel so we enabled RNDIS mode in kernel and integrated changed kernel in AOSP source.</p>
  
  <p>Here are some logs from "strace dhcpdbg usb0"</p>
  
  <pre><code>ioctl(3, SIOCGIFFLAGS, {ifr_name="usb0", ifr_flags=IFF_UP|IFF_BROADCAST|IFF_RUNNING|IFF_MULTICAST}) = 0
  ioctl(3, SIOCSIFFLAGS, {ifr_name="usb0", ifr_flags=IFF_UP|IFF_BROADCAST|IFF_RUNNING|IFF_MULTICAST}) = 0
  ioctl(3, SIOCSIFADDR, {ifr_name="usb0", ifr_addr={sa_family=AF_INET, sin_port=htons(0), sin_addr=inet_addr("192.168.1.100")}}) = 0
  ioctl(3, SIOCSIFNETMASK, {ifr_name="usb0", ifr_netmask={sa_family=AF_INET, sin_port=htons(0), sin_addr=inet_addr("255.255.255.0")}}) = 0
  ioctl(3, SIOCADDRT, 0x7ffb9b8030)       = -1 ENETUNREACH (Network is unreachable)
  close(3)                                = 0
  getuid()                                = 0
  writev(5, [{iov_base="\0\355\25o\6X\\=\267\254%", iov_len=11}, {iov_base="\3", iov_len=1}, {iov_base="DHCP\0", iov_len=5}, {iov_base="failed to set default         route 192."..., iov_len=65}], 4) = 82
  getuid()                                = 0
  writev(6, [{iov_base="lY\0\0\0\355\25", iov_len=7}, {iov_base="\0\355\25o\6X\\=\267\254%", iov_len=11}, {iov_base="\3", iov_len=1}, {iov_base="DHCP\0",     iov_len=5}, {iov_base="failed to set default route 192ioctl(3, SIOCADDRT, 0x7ffb9b8030)."..., iov_len=65}], 5) = 89
  write(2, "dhcpdbg: ", 9dhcpdbg: )                = 9
  write(2, "dhcptool usb0: do_dhcp failed", 29dhcptool usb0: do_dhcp failed) = 29
  write(2, ": ", 2: )                       = 2
  write(2, "Network is unreachable\n", 23Network is unreachable
  ) = 23
  mprotect(0x6ffbc65000, 4096, PROT_READ|PROT_WRITE) = 0
  mprotect(0x6ffbc65000, 4096, PROT_READ) = 0
  mprotect(0x6ffbc65000, 4096, PROT_READ|PROT_WRITE) = 0
  mprotect(0x6ffbc65000, 4096, PROT_READ) = 0
  mprotect(0x6ffbc65000, 4096, PROT_READ|PROT_WRITE) = 0
  mprotect(0x6ffbc65000, 4096, PROT_READ) = 0
  munmap(0x6ffbc65000, 4096)              = 0
  exit_group(101)                         = ?
  +++ exited with 101 +++
  </code></pre>
  
  <p>Below are some output of the networking commands:</p>
  
  <p>ifconfig:</p>
  
  <pre><code>usb0      Link encap:Ethernet  HWaddr 02:06:06:15:00:0b  Driver rndis_host
        inet addr:192.168.1.100  Bcast:192.168.1.255  Mask:255.255.255.0 
        UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
        RX packets:4 errors:0 dropped:0 overruns:0 frame:0 
        TX packets:80 errors:0 dropped:0 overruns:0 carrier:0 
        collisions:0 txqueuelen:1000 
        RX bytes:1312 TX bytes:25155 
  </code></pre>
  
  <p>ip add show:</p>
  
  <pre><code>28: usb0: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; mtu 1500 qdisc pfifo_fast state UNKNOWN group default qlen 1000
      link/ether 02:06:06:15:00:0b brd ff:ff:ff:ff:ff:ff
      inet 192.168.1.100/24 brd 192.168.1.255 scope global usb0
      valid_lft forever preferred_lft forever
  </code></pre>
  
  <p>ip route show:</p>
  
  <pre><code>192.168.1.0/24 dev usb0 proto kernel scope link src 192.168.1.100 
  192.168.43.0/24 dev wlan0 proto kernel scope link src 192.168.43.222 
  </code></pre>
  
  <p>Kernel IP routing table:</p>
  
  <pre><code>Destination Gateway     Genmask     Flags   MSS Window  irtt Iface
  192.168.1.0     0.0.0.0         255.255.255.0   U         0 0          0 usb0
  192.168.43.0    0.0.0.0         255.255.255.0   U         0 0          0 wlan0
  </code></pre>
  
  <p>ping 8.8.8.8:</p>
  
  <pre><code>connect: Network is unreachable
  </code></pre>
  
  <p>This dhcptool tool is working fine on all earlier android versions till Android 8.
  Below is the working strace log from Android 8 dhcptool running on same usb0 port. </p>
  
  <pre><code>ioctl(3, SIOCGIFFLAGS, {ifr_name="usb0", ifr_flags=IFF_UP|IFF_BROADCAST|IFF_RUNNING|IFF_MULTICAST}) = 0
  ioctl(3, SIOCSIFFLAGS, {ifr_name="usb0", ifr_flags=IFF_UP|IFF_BROADCAST|IFF_RUNNING|IFF_MULTICAST}) = 0
  ioctl(3, SIOCSIFADDR, {ifr_name="usb0", ifr_addr={sa_family=AF_INET, sin_port=htons(0), sin_addr=inet_addr("192.168.1.100")}}) = 0
  ioctl(3, SIOCSIFNETMASK, {ifr_name="usb0", ifr_netmask={sa_family=AF_INET, sin_port=htons(0), sin_addr=inet_addr("255.255.255.0")}}) = 0
  ioctl(3, SIOCADDRT, 0x7fd2bb6cd0)       = 0
  close(3)                                = 0
  socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0) = 3
  connect(3, {sa_family=AF_UNIX, sun_path="/dev/socket/property_service"}, 31) = 0
  writev(3, [{iov_base="\1\0\2\0", iov_len=4}, {iov_base="\r\0\0\0", iov_len=4}, {iov_base="net.usb0.dns1", iov_len=13}, {iov_base="\v\0\0\0", iov_len=4}, {iov_base="192.168.1.1", iov_len=11}], 5) = 36
  recvfrom(3, "\0\0\0\0", 4, MSG_WAITALL, NULL, NULL) = 4
  close(3)                                = 0
  socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0) = 3
  connect(3, {sa_family=AF_UNIX, sun_path="/dev/socket/property_service"}, 31) = 0
  writev(3, [{iov_base="\1\0\2\0", iov_len=4}, {iov_base="\r\0\0\0", iov_len=4}, {iov_base="net.usb0.dns2", iov_len=13}, {iov_base="\0\0\0\0", iov_len=4}], 4) = 25
  recvfrom(3, "\0\0\0\0", 4, MSG_WAITALL, NULL, NULL) = 4
  close(3)                                = 0
  write(2, "dhcptool: ", 10dhcptool: )              = 10
  write(2, "IP assignment is for debug purpo"..., 40IP assignment is for debug purposes ONLY) = 40
  write(2, ": ", 2: )                       = 2
  write(2, "Success\n", 8Success
  )                = 8
  mprotect(0x7729153000, 4096, PROT_READ|PROT_WRITE) = 0
  mprotect(0x7729153000, 4096, PROT_READ) = 0
  mprotect(0x7729153000, 4096, PROT_READ|PROT_WRITE) = 0
  mprotect(0x7729153000, 4096, PROT_READ) = 0
  munmap(0x7729153000, 4096)              = 0
  exit_group(0)                           = ?
  +++ exited with 0 +++
  bullhead:/ # ping 8.8.8.8                                                                                                                                                                                   
  PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
  64 bytes from 8.8.8.8: icmp_seq=1 ttl=114 time=131 ms
  64 bytes from 8.8.8.8: icmp_seq=2 ttl=114 time=58.4 ms
  ^C
  --- 8.8.8.8 ping statistics ---
  2 packets transmitted, 2 received, 0% packet loss, time 1001ms
  rtt min/avg/max/mdev = 58.496/95.044/131.592/36.548 ms
  bullhead:/ # 
  </code></pre>