#!/bin/sh
#----------------------------------------------------------------------------
# /etc/ppp/ipv6-up - called whenever a circuit connection goes online
#
# Input:
#   $1 = network interface
#   $2 = TTY device (mostly useless, often set to /dev/null if there is no TTY)
#   $3 = TTY speed (may be empty, see above)
#   $4 = local IPv6 address (optionally with subnet mask in CIDR notation)
#   $5 = remote IPv6 address (if this is a point-to-point connection)
#   $6 = circuit identifier
#   $7 = if non-empty, we are called from within addrv6-up
#
# Creation:        26.09.2009  zone42
# Last Update:     $Id: ipv6-up 55392 2019-03-12 06:29:32Z kristov $
#----------------------------------------------------------------------------

. /etc/boot.d/lazy.inc
. /etc/boot.d/networking.inc

run_ipv6_up ()
{
    circuit_lock ipv6updown-$circ_id "$script"
    circuit_lock $circ_id "$script"

    local is_default_route=no i=
    for i in $(seq 1 $circ_nets_ipv6_n)
    do
        eval local net=\$circ_nets_ipv6_$i gateway=\$circ_nets_ipv6_${i}_gateway
        if [ "$net" = "::/0" ]
        then
            is_default_route=yes
            : ${remote:=$gateway}
            break
        fi
    done

    echo "circuit $circ_id[$interface] comes up"
    [ -n "$local" ] && echo "$circ_id: local IPv6 address: $local"
    [ -n "$remote" ] && echo "$circ_id: remote IPv6 address: $remote"

    eval local $(grep "^\(IPV6_NET_N\|IPV6_NET_[0-9]\+\(_DEV\)\?\)=" /etc/rc.cfg)
    for i in $(seq 1 $circ_nets_ipv6_n)
    do
        eval local net=\$circ_nets_ipv6_$i gateway=\$circ_nets_ipv6_${i}_gateway
        : ${gateway:=$remote}
        local route_spec="dev $interface"
        if [ -n "$gateway" ]
        then
            route_spec="via $gateway $route_spec"
            local gwif
            if ! translate_ipv6_to_dev $gateway gwif 1 ||
                [ "$gwif" != "$interface" ]
            then
                circuit_add_route $circ_id 6 "$gateway dev $interface"
            fi
        fi
        circuit_add_route $circ_id 6 "$net $route_spec"
        circuit_register_routed_network $circ_id ipv6 $net
        [ -f /var/run/imond.pid ] && imond-stat "$circ_id up $net"
    done

    setup-dynamic-ip6tables.sh

    # now the circuit is (at least semi)online
    circuit_go_online_if_possible $circ_id ipv6
    circuit_unlock $circ_id "$script"

    # Call all custom ipv6-ups
    lazy_begin
    for j in /etc/ppp/ipv6-up[0-9][0-9][0-9].*
    do
        if [ -f $j ]
        then
            echo "executing $j"
            . $j
        fi
    done
    lazy_end

    if [ ! -f /var/run/ipv6-up.ppp-once ]
    then
        lazy_begin
        for j in /etc/ppp-once/ipv6-up[0-9][0-9][0-9].*
        do
            if [ -f $j ]
            then
                echo "executing $j"
                . $j
            fi
        done
        lazy_end
    fi

    circuit_unlock ipv6updown-$circ_id "$script"
}

interface=$1
tty=$2
speed=$3
local=${4:+$(netcalc canonicalize $4)}
remote=$5
circ_id=$6
addrup=$7

if [ -n "$local" -a -z "$addrup" ]
then
    /etc/ppp/addrv6-up $interface $circ_id "$local" 1 &
fi

ip=ipv6
script_op=up
. /etc/ppp/ip-up-down
