How to change the default hotspot DHCP IP address range on Android?


Question

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.com 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.

  • dnsmasq - the DHCP/DNS server (up to Pie) - is started with hard-coded commandline arguments (6) (which can be set through /etc/dnsmasq.conf (7) 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=$(awk '$2=="local_network" { print $1 } ' /data/misc/net/rt_tables)

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/ or iw:



WIFI_INTERFACE=$(iw dev | grep -E 'ssid |Interface ' | grep -B1 ssid | awk '$1=="Interface" {
print $2
}
')


Also confirm your local_network (8, 9) routing table (97) is being used for hotspot. 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 }


* Rules are 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:system_file:s0 /system/bin/dnsmasq
~# chcon u:object_r:dnsmasq_exec:s0 /system/bin/dnsmasq.bin


Enjoy!



Another options is to modify the value of config_tether_dhcp_range (10) 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:




Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes