Simplify IP rules for Wi-Fi direct mode
This commit is contained in:
@@ -169,7 +169,7 @@ class HotspotService : Service(), WifiP2pManager.ChannelListener {
|
|||||||
} catch (_: Routing.InterfaceNotFoundException) {
|
} catch (_: Routing.InterfaceNotFoundException) {
|
||||||
startFailure(getString(R.string.exception_interface_not_found))
|
startFailure(getString(R.string.exception_interface_not_found))
|
||||||
return START_NOT_STICKY
|
return START_NOT_STICKY
|
||||||
}.apRule().forward().dnsRedirect(dns)
|
}.rule().forward().dnsRedirect(dns)
|
||||||
if (routing.start()) {
|
if (routing.start()) {
|
||||||
this.routing = routing
|
this.routing = routing
|
||||||
apConfiguration = NetUtils.loadApConfiguration()
|
apConfiguration = NetUtils.loadApConfiguration()
|
||||||
@@ -229,7 +229,8 @@ class HotspotService : Service(), WifiP2pManager.ChannelListener {
|
|||||||
} catch (_: Routing.InterfaceNotFoundException) {
|
} catch (_: Routing.InterfaceNotFoundException) {
|
||||||
startFailure(getString(R.string.exception_interface_not_found), group)
|
startFailure(getString(R.string.exception_interface_not_found), group)
|
||||||
return
|
return
|
||||||
}.p2pRule().forward().dnsRedirect(dns)
|
}.ipForward() // Wi-Fi direct doesn't enable ip_forward
|
||||||
|
.rule().forward().dnsRedirect(dns)
|
||||||
if (routing.start()) {
|
if (routing.start()) {
|
||||||
this.routing = routing
|
this.routing = routing
|
||||||
doStart(group)
|
doStart(group)
|
||||||
|
|||||||
@@ -87,7 +87,7 @@ class MainActivity : AppCompatActivity(), ServiceConnection, Toolbar.OnMenuItemC
|
|||||||
}
|
}
|
||||||
holder.binding.device = device
|
holder.binding.device = device
|
||||||
holder.binding.ipAddress = when (position) {
|
holder.binding.ipAddress = when (position) {
|
||||||
0 -> binder?.service?.routing?.hostAddress?.address?.hostAddress
|
0 -> binder?.service?.routing?.hostAddress
|
||||||
else -> arpCache[device?.deviceAddress]
|
else -> arpCache[device?.deviceAddress]
|
||||||
}
|
}
|
||||||
holder.binding.executePendingBindings()
|
holder.binding.executePendingBindings()
|
||||||
|
|||||||
@@ -13,43 +13,28 @@ class Routing(private val upstream: String, val downstream: String, ownerAddress
|
|||||||
"while iptables -D FORWARD -j vpnhotspot_fwd; do done",
|
"while iptables -D FORWARD -j vpnhotspot_fwd; do done",
|
||||||
"iptables -F vpnhotspot_fwd",
|
"iptables -F vpnhotspot_fwd",
|
||||||
"iptables -X vpnhotspot_fwd",
|
"iptables -X vpnhotspot_fwd",
|
||||||
"while ip rule del lookup 62; do done",
|
|
||||||
"ip route flush table 62",
|
|
||||||
"while ip rule del priority 17999; do done")
|
"while ip rule del priority 17999; do done")
|
||||||
}
|
}
|
||||||
|
|
||||||
class InterfaceNotFoundException : IOException()
|
class InterfaceNotFoundException : IOException()
|
||||||
|
|
||||||
val hostAddress = NetworkInterface.getByName(downstream)?.interfaceAddresses
|
val hostAddress: String = (ownerAddress ?: NetworkInterface.getByName(downstream)?.inetAddresses?.asSequence()
|
||||||
?.singleOrNull { if (ownerAddress == null) it.address is Inet4Address else it.address == ownerAddress }
|
?.singleOrNull { it is Inet4Address } ?: throw InterfaceNotFoundException()).hostAddress
|
||||||
?: throw InterfaceNotFoundException()
|
|
||||||
private val startScript = LinkedList<String>()
|
private val startScript = LinkedList<String>()
|
||||||
private val stopScript = LinkedList<String>()
|
private val stopScript = LinkedList<String>()
|
||||||
|
|
||||||
fun p2pRule(): Routing {
|
fun ipForward(): Routing {
|
||||||
val address = hostAddress.address.address
|
startScript.add("echo 1 >/proc/sys/net/ipv4/ip_forward")
|
||||||
val subnetPrefixLength = hostAddress.networkPrefixLength
|
|
||||||
// clear suffix bits
|
|
||||||
var done = subnetPrefixLength.toInt()
|
|
||||||
while (done < address.size shl 3) {
|
|
||||||
val index = done shr 3
|
|
||||||
address[index] = (address[index].toInt() and (0x7f00 shr (done and 7))).toByte()
|
|
||||||
done = (index + 1) shl 3
|
|
||||||
}
|
|
||||||
startScript.add("echo 1 >/proc/sys/net/ipv4/ip_forward") // Wi-Fi direct doesn't enable ip_forward
|
|
||||||
startScript.add("ip route add default dev $upstream scope link table 62")
|
|
||||||
startScript.add("ip route add ${InetAddress.getByAddress(address).hostAddress}/$subnetPrefixLength dev $downstream scope link table 62")
|
|
||||||
startScript.add("ip route add broadcast 255.255.255.255 dev $downstream scope link table 62")
|
|
||||||
startScript.add("ip rule add iif $downstream lookup 62")
|
|
||||||
// removing each rule may fail if downstream is already removed
|
|
||||||
stopScript.addFirst("ip route flush table 62")
|
|
||||||
stopScript.addFirst("ip rule del iif $downstream lookup 62")
|
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Since Android 5.0, RULE_PRIORITY_TETHERING = 18000.
|
/**
|
||||||
* https://android.googlesource.com/platform/system/netd/+/b9baf26/server/RouteController.cpp#65 */
|
* Since Android 5.0, RULE_PRIORITY_TETHERING = 18000.
|
||||||
fun apRule(): Routing {
|
* This also works for Wi-Fi direct where there's no rule at 18000.
|
||||||
|
*
|
||||||
|
* Source: https://android.googlesource.com/platform/system/netd/+/b9baf26/server/RouteController.cpp#65
|
||||||
|
*/
|
||||||
|
fun rule(): Routing {
|
||||||
startScript.add("ip rule add from all iif $downstream lookup $upstream priority 17999")
|
startScript.add("ip rule add from all iif $downstream lookup $upstream priority 17999")
|
||||||
stopScript.addFirst("ip rule del from all iif $downstream lookup $upstream priority 17999")
|
stopScript.addFirst("ip rule del from all iif $downstream lookup $upstream priority 17999")
|
||||||
return this
|
return this
|
||||||
@@ -67,7 +52,6 @@ class Routing(private val upstream: String, val downstream: String, ownerAddress
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun dnsRedirect(dns: String): Routing {
|
fun dnsRedirect(dns: String): Routing {
|
||||||
val hostAddress = hostAddress.address.hostAddress
|
|
||||||
startScript.add("iptables -t nat -A PREROUTING -i $downstream -p tcp -d $hostAddress --dport 53 -j DNAT --to-destination $dns")
|
startScript.add("iptables -t nat -A PREROUTING -i $downstream -p tcp -d $hostAddress --dport 53 -j DNAT --to-destination $dns")
|
||||||
startScript.add("iptables -t nat -A PREROUTING -i $downstream -p udp -d $hostAddress --dport 53 -j DNAT --to-destination $dns")
|
startScript.add("iptables -t nat -A PREROUTING -i $downstream -p udp -d $hostAddress --dport 53 -j DNAT --to-destination $dns")
|
||||||
stopScript.addFirst("iptables -t nat -D PREROUTING -i $downstream -p tcp -d $hostAddress --dport 53 -j DNAT --to-destination $dns")
|
stopScript.addFirst("iptables -t nat -D PREROUTING -i $downstream -p tcp -d $hostAddress --dport 53 -j DNAT --to-destination $dns")
|
||||||
|
|||||||
Reference in New Issue
Block a user