1# Set dhcp.<iface>.routeN.dest and dhcp.<iface>.routeN.via properties with
2# the static routes provided by the DHCP server.
3
4# CAUTION
5#
6# Placing this in the hooks directory will allow DHCP servers to push static
7# routes to ALL of the interfaces that use the directory.
8#
9# To avoid this, create separate dhcpcd configurations, one for the interfaces
10# that should accept static routes and another for the interfaces that should
11# not accept static routes routes.
12
13# Add this script in the hooks directory only for the interfaces that should
14# accept static routes.
15#
16# Add "nooption classless_static_routes, static_routes" to the dhcpcd.conf
17# file for the interfaces that should not accept static routes. Do not add the
18# script to the hooks directory.
19
20next_set_interface=1
21
22set_route_props_from_list()
23{
24    while [[ $# -ge 2 ]]; do
25        setprop dhcp.${interface}.route${next_set_interface}.dest $1
26        shift
27        setprop dhcp.${interface}.route${next_set_interface}.via $1
28        shift
29        next_set_interface=$(($next_set_interface + 1))
30    done
31}
32
33unset_route_props()
34{
35    next_clear_interface=1
36    while [[ ! -z "$(getprop dhcp.${interface}.route${next_clear_interface}.dest)" ]]; do
37      setprop dhcp.${interface}.route${next_clear_interface}.dest ""
38      setprop dhcp.${interface}.route${next_clear_interface}.via ""
39      next_clear_interface=$(($next_clear_interface + 1))
40    done
41    while [[ ! -z "$(getprop dhcp.${interface}.route${next_clear_interface}.via)" ]]; do
42      setprop dhcp.${interface}.route${next_clear_interface}.dest ""
43      setprop dhcp.${interface}.route${next_clear_interface}.via ""
44      next_clear_interface=$(($next_clear_interface + 1))
45    done
46}
47
48set_route_props()
49{
50    unset_route_props
51    if [[ ! -z "${new_classless_static_routes}" ]]; then
52        set_route_props_from_list ${new_classless_static_routes}
53    else
54        set_route_props_from_list ${new_static_routes}
55    fi
56}
57
58case "${reason}" in
59BOUND|INFORM|REBIND|REBOOT|RENEW|TIMEOUT)       set_route_props;;
60EXPIRE|FAIL|IPV4LL|RELEASE|STOP)                unset_route_props;;
61esac
62