OpenWrt Forum Archive

Topic: How to detect a private WAN IP and redial up?

The content of this topic has been archived on 8 Apr 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

I hate my ISP, but have to like its low price of ADSL access. The price not only delivers me an ADSL connection, also

- Transparent proxies seemingly have been imposed by the ISP so that "my" IP when browsing websites isn't the WAN IP of my router, and
- The ISP assign any possible IP including private address to my router, i.e. randomly sometimes public IP and sometimes private IP 10.x.x.x (thank them for still assigning an IP).

I need DDNS to access my home router and NAS. With transparent proxy injected, DDNS in ordinary home router only exposes IP of ISP proxy not my router. Fortunately, OpenWrt can update DDNS services directly with its own WAN IP. Then when I'm more lucky that my router gets a global IP, I can really access my home router with this global IP. But if without luck my router gets a private IP, what I can only do is waiting - until my router refreshes its IP or someone helps restart my router.

So I would like to beg your help - to eliminate private IP from my router. I believe OpenWrt can be the hero again. My rough idea is: when the router detects a private WAN IP after PPPoE up, it automatically disconnects/reconnects until a global IP is received, or less good, a cron job checks the WAN IP every e.g. 5 minutes and re-establishes WAN connection in case of private IP.

But I don't know how to make that implemented - what and how to change. Could anybody please help me? And if this feature is useful enough to be integrated into the release, it would be perfect.

I appreciate any help!

Is there anyone knowing how to?

You could use a hotplug-event to run your script when the pppoe-interface comes up
http://wiki.openwrt.org/doc/techref/hotplug

to get the wan-ip (on AA or trunk) you can run this command:

. /lib/functions/network.sh; network_get_ipaddr ip wan; echo $ip

then you just need to check if it's private IP and if thats the case do a

 ifdown wan && ifup wan 

(Last edited by eleon216 on 23 Feb 2013, 11:37)

Great, thank you eleon216! With your guide, I created and run this script successfully.

   /etc/hotplug.d/iface/99-noPrivIP

#!/bin/sh

[ "$INTERFACE" = "wan" -a "$ACTION" = "ifup" ] && {

  coins=5
  journal="/var/PrivIP"
  [ -f $journal ] && plays=$(cat $journal) || plays=0

  [ $plays -ge $coins ] && plays=0 || {
    . /lib/functions/network.sh
    network_get_ipaddr ip wan
    if [ "${ip:0:3}" = "172" -a "${ip:6:1}" = "." ] && [ "${ip:4:2}" -ge 16 -a "${ip:4:2}" -le 31 ] || [ "${ip:0:3}" = "10." ]
    then let ++plays && ifdown wan && ifup wan
    else plays=0
    fi
  }
  
  [ -f $journal -o $plays -gt 0 ] && echo $plays > $journal
  
}

But still some minor questions:

1, The codes to decide (class B) private IP are not concise, is there a better way to do that?

2, It will be safer to limit this script to PPPoE WAN, at least, it shouldn't apply to WAN connection with static IP or even DHCP. In what way (variable) can PPPoE protocol be checked?

3, I tried quite much but couldn't find any way to use an environmental variable to count failure in getting public IP. Is that impossible?

(Last edited by chromatid on 21 Apr 2013, 19:09)

The discussion might have continued from here.