Starting AICCU on OpenWRT

OpenWRT have apparently removed the /etc/init.d/aiccu start script in order not to violate the SixXS terms of service regarding repeated and unattended tunnel setup.

However, I want to have the router try one tunnel setup after I have manually switched it on.

Now, in order to have aiccu successfully set up a tunnel, we will have to ensure a set of conditions is met when we run `aiccu start´.

  • IPv4 connectivity to tic.sixxs.net
  • IPv4 connectivity to my POP
  • local system time must be accurate

In my case, I can reasonably guesstimate the IPv4 connectivity by the presence of a default route.

However, guessing wether the system time is accurate on a system without an RTC is not that trivial - at first glance.

Fortunately, the NTP daemon which comes with OpenWRT can run a program when it receives a time update, so we can change its init script to add -S /etc/ntp-hook to the ntpd arguments.

/etc/init.d/sysntpd snippet
1
2
3
4
5
6
7
8
9
[...]

    append args "-S /etc/ntp-hook"

    if [ "$args" != "-n" ]; then
        service_start /usr/sbin/ntpd $args
    fi

[...]

The hook just writes the current time into a file /tmp/date which does not exist on bootup. So the presence of /tmp/date indicates the ntp-hook has run at least once, which means that we have a proper system time.

/etc/ntp-hook
1
2
3
#!/bin/sh

date > /tmp/date

Note that we need to be careful to start aiccu from rc.local in the background, so that the init startup process can continue.

/etc/rc.local snippet
1
2
3
4
5
[...]

(/etc/start-aiccu-when-useful &)

[...]
/etc/start-aiccu-when-useful
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/bin/sh

# Wait until IPv4 default route exists
while :
do
    if ip -4 route list default | grep -q default
    then
        break
    fi
    sleep 2
done

# Wait until we have a proper system time
while :
do
    if test -f /tmp/date
    then
        break
    fi
    sleep 2
done

# Wait a little more to make race conditions faster than ourselves.
sleep 2

# And that's it.
(aiccu start &)