Network Routing – How to Choose Between Internet/WiFi and Cellular

dual-homedmobileroutingwireless

I have a decent understanding of networking, but this question popped up in my head randomly and I can't figure it out. For example, if I have an iPhone that is used on a 3G network but is also connected to my home wifi throughout the day, how does an application know whether to send data (whether it be a push notification or something else) to my phone via my cellular IP, or my Wifi IP address? Wouldn't both the cellular network and my home network associate my phone with themselves?

Best Answer

In this scenario, your phone has at least two network interfaces: WLAN and 3G. Each interface has his own IP address and mask. They usually install as two routes on the phones routing table, the locally connected network and a default gateway.

Let's see a simplified example:

Interface: Wifi IP/mask: 192.168.10.10 255.255.255.0 Default gateway: 192.168.10.1 (your home router)

Interface: 3G IP/mask: 10.10.10.10 255.255.255.0 Default gateway: 10.10.10.1 (your next hop to ISP)

Resulting routing table:

Destination Interface Metric

192.168.10.0/24 Wifi 0

0.0.0.0/0 Wifi 20

10.10.10.0 3G 0

0.0.0.0/0 3G 30

So, what happens when your phone wants to send a packet? It checks the destination IP address vs its own routing table and decides which interface to send it to.

  • If you send a packet to 192.168.10.49 it matchs first line and sends it through the Wifi interface.
  • If you send a packet to 10.10.10.50 it matches the third line and sends it through the 3G interface.
  • If you send a a packet to 8.8.8.8 (Google DNS) there are two matches. Which of the two lines has precedence? The one with the lowest metric. In this case it's the second line and your packet to internet will go through the Wifi interface.

In personal devices, this metric is automatic. Your iphone is smart enough to set a lower metric to Wifi.