#!/bin/bash
# DO NOT MODIFY THIS FILE. YOUR CHANGES WILL BE OVERWRITTEN WHEN BONDING IS UPGRADED.
# To extend this functionality add a new hook.

# Configure the tunnel as part of the policy routing container ${NAME}.

CONNECTEDIP_SAVE_DIRECTORY="/var/lib/bondingprivatewan/connectedips"
ROUTE_SAVE_DIRECTORY="/var/lib/bondingprivatewan/routes"
test -f /etc/bonding/privatewan/${NAME} && . /etc/bonding/privatewan/${NAME}
start () {
    remove 2> /dev/null
    ip route add $PEER_IP table $TABLE dev $TUN_NAME

    # Connected IPs that need routes into the tunnel are added using data in
    # the files in $CONNECTEDIP_SAVE_DIRECTORY.
    for CONNECTEDIP in $CONNECTEDIP_SAVE_DIRECTORY/$ID/*; do
        . $CONNECTEDIP
        ip route add table $TABLE $SUBNET dev $TUN_NAME
    done

    # Same for routes.
    for ROUTE in $ROUTE_SAVE_DIRECTORY/$ID/*; do
        . $ROUTE
        ip route add table $TABLE $SUBNET dev $TUN_NAME
    done
}
stop () {
    remove
}
remove () {
    # When the tunnel closes, the kernel removes routes into the tun, so we
    # don't need to do anything here.
    true
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    *)
        echo "Usage: $0 {start|stop}"
        exit 1
        ;;
esac
