ppp: honor ip6table for IPv6 PPP interfaces
[openwrt/openwrt.git] / package / network / services / ppp / files / ppp.sh
index 99fdc87e7a206a980c9d9d952806acc3559e526f..9c27b3c280b5680cd56f3d8b18437e5d6e3b0c4d 100755 (executable)
@@ -4,10 +4,35 @@
 
 [ -n "$INCLUDE_ONLY" ] || {
        . /lib/functions.sh
+       . /lib/functions/network.sh
        . ../netifd-proto.sh
        init_proto "$@"
 }
 
+ppp_select_ipaddr()
+{
+       local subnets=$1
+       local res
+       local res_mask
+
+       for subnet in $subnets; do
+               local addr="${subnet%%/*}"
+               local mask="${subnet#*/}"
+
+               if [ -n "$res_mask" -a "$mask" != 32 ]; then
+                       [ "$mask" -gt "$res_mask" ] || [ "$res_mask" = 32 ] && {
+                               res="$addr"
+                               res_mask="$mask"
+                       }
+               elif [ -z "$res_mask" ]; then
+                       res="$addr"
+                       res_mask="$mask"
+               fi
+       done
+
+       echo "$res"
+}
+
 ppp_exitcode_tostring()
 {
        local errorcode=$1
@@ -53,26 +78,47 @@ ppp_generic_init_config() {
        proto_config_add_boolean authfail
        proto_config_add_int mtu
        proto_config_add_string pppname
+       proto_config_add_string unnumbered
+       proto_config_add_boolean persist
+       proto_config_add_int maxfail
+       proto_config_add_int holdoff
 }
 
 ppp_generic_setup() {
        local config="$1"; shift
+       local localip
 
-       json_get_vars ipv6 demand keepalive keepalive_adaptive username password pppd_options pppname
+       json_get_vars ipv6 ip6table demand keepalive keepalive_adaptive username password pppd_options pppname unnumbered persist maxfail holdoff
        if [ "$ipv6" = 0 ]; then
                ipv6=""
        elif [ -z "$ipv6" -o "$ipv6" = auto ]; then
                ipv6=1
-               proto_export "AUTOIPV6=1"
+               autoipv6=1
        fi
 
        if [ "${demand:-0}" -gt 0 ]; then
                demand="precompiled-active-filter /etc/ppp/filter demand idle $demand"
        else
-               demand="persist"
+               demand=""
+       fi
+       if [ -n "$persist" ]; then
+               [ "${persist}" -lt 1 ] && persist="nopersist" || persist="persist"
+       fi
+       if [ -z "$maxfail" ]; then
+               [ "$persist" = "persist" ] && maxfail=0 || maxfail=1
        fi
        [ -n "$mtu" ] || json_get_var mtu mtu
        [ -n "$pppname" ] || pppname="${proto:-ppp}-$config"
+       [ -n "$unnumbered" ] && {
+               local subnets
+               ( proto_add_host_dependency "$config" "" "$unnumbered" )
+               network_get_subnets subnets "$unnumbered"
+               localip=$(ppp_select_ipaddr "$subnets")
+               [ -n "$localip" ] || {
+                       proto_block_restart "$config"
+                       return
+               }
+       }
 
        local lcp_failure="${keepalive%%[, ]*}"
        local lcp_interval="${keepalive##*[, ]}"
@@ -86,16 +132,20 @@ ppp_generic_setup() {
        proto_run_command "$config" /usr/sbin/pppd \
                nodetach ipparam "$config" \
                ifname "$pppname" \
+               ${localip:+$localip:} \
                ${lcp_failure:+lcp-echo-interval $lcp_interval lcp-echo-failure $lcp_failure $lcp_adaptive} \
                ${ipv6:++ipv6} \
+               ${autoipv6:+set AUTOIPV6=1} \
+               ${ip6table:+set IP6TABLE=$ip6table} \
                nodefaultroute \
                usepeerdns \
-               $demand maxfail 1 \
+               $demand $persist maxfail $maxfail \
+               ${holdoff:+holdoff "$holdoff"} \
                ${username:+user "$username" password "$password"} \
                ${connect:+connect "$connect"} \
                ${disconnect:+disconnect "$disconnect"} \
                ip-up-script /lib/netifd/ppp-up \
-               ipv6-up-script /lib/netifd/ppp-up \
+               ipv6-up-script /lib/netifd/ppp6-up \
                ip-down-script /lib/netifd/ppp-down \
                ipv6-down-script /lib/netifd/ppp-down \
                ${mtu:+mtu $mtu mru $mtu} \