Wednesday, January 16, 2019

networking - How to change the default DHCP IP address range on Android 7.1.2?


As the title says I look for a way to permanently change the default IP address range form 192.168.43.xxx to 192.168.1.xxx.


Reason: On my router some of my devices get a static IP trough DHCP, in the 192.168.1.xxx range. Using the hotspot on the road I would like to have the same setup.


I have tried to change the range following this steps, but it did not work out on both of my devices. They are rooted and Lineage OS is installed. The last answer suggest an easy way to do this, but it is not possible in lineage OS.


Also this post from anroidpolice suggests that it is possible by an app, I look for a solution to do this with adb. I have no access to Google Play, and do not want it.



Answer



NOTE: Root is required.


Default DHCP IP address range is hard-coded (1), you can't change it without rebuilding ROM with modified source code. Or use a little hack.



When you switch on tethering, what happens (at least):



  • hostapd - the daemon which manages access points - is started.

  • Network interfaces are set up, IP address is added to Wi-Fi interface (hard-coded before Android Pie (2, 3), randomized afterwards (4)), and routing table is added (5) for local network (6).

  • dnsmasq - the DHCP/DNS server (up to Pie) - is started with hard-coded commandline arguments (7) (which can be set through /etc/dnsmasq.conf (8) otherwise).


So we can replace /system/bin/dnsmasq with a custom shell script, taking control of the process in between. Rename the original binary to something else:


~# mv /system/bin/dnsmasq /system/bin/dnsmasq.bin

Create script /system/bin/dnsmasq:



#!/system/bin/sh

OLD_SUBNET='192.168.43'
NEW_SUBNET='192.168.1'
WIFI_INTERFACE='wlan0'
LOCAL_TABLE='97'

export PATH=/system/bin

# delete old route, add new

ip route del ${OLD_SUBNET}.0/24 dev ${WIFI_INTERFACE} table $LOCAL_TABLE
ip route add ${NEW_SUBNET}.0/24 dev ${WIFI_INTERFACE} table $LOCAL_TABLE

# set new IP address on Wi-Fi interface
ip address add ${NEW_SUBNET}.1/24 dev $WIFI_INTERFACE

# inject new subnet in hard-coded arguments received from netd
set -- $(printf '%s' "$*" | sed 's/'${OLD_SUBNET}'/'${NEW_SUBNET}'/g')
unset OLD_SUBNET NEW_SUBNET WIFI_INTERFACE LOCAL_TABLE


# execute original binary with new arguments
exec dnsmasq.bin $*

Confirm the name of your Wi-Fi interface (wlan0 usually). Check with ip link or ls /sys/class/net/.


Also confirm your local network routing table is 97: grep local_network /data/misc/net/rt_tables. Android's routing is a mess, getting more complex with every new release. So I'm not sure if this has been persistent or not. Also before making any changes, check your routing policies and tables to figure out what you should put in your script:


~# RULES="$(ip rule | grep -vE 'unreachable|local')"
~# echo "$RULES"
~# for t in $(echo "$RULES" | awk '{print $NF}' | uniq); do ip r s table $t; done

SELinux rules also need to be defined if (all or some) not already defined and if status is enforcing. Use Magisk's suploicy or some other similar tool like sepolicy-inject:



# execute binaries from /system/bin
allow netd system_file dir { read open getattr search }
allow netd system_file file { read gettattr open execute execute_no_trans }

# execute /system/bin/sh
allow netd shell_exec file { read getattr open execute execute_no_trans }

# execute /system/bin/toolbox and its applets
allow netd toolbox_exec file { read gettattr open execute execute_no_trans }


# configure RPDB rules / routing tables
allow netd netd capability { sys_admin }

* not persistent across reboots, use some init.d script or replace /sepolicy in ramdisk


Set permissions on files:


~# chown 0.0 /system/bin/dnsmasq*
~# chmod 0755 /system/bin/dnsmasq*
~# chcon u:object_r:dnsmasq_exec:s0 /system/bin/dnsmasq*

Enjoy!



Another options is to modify the value of config_tether_dhcp_range (9) in Android framework as explained in this answer, but I haven't tested this.


Or you can setup complete tethering from commandline, running your own processes. This answer includes the instructions, though the question is different.


RELATED:



No comments:

Post a Comment

samsung galaxy s 2 - Cannot restore Kies backup after firmware upgrade

I backed up my Samsung Galaxy S2 on Kies before updating to Ice Cream Sandwich. After the upgrade I tried to restore, but the restore fails ...