#!/bin/sh
. /lib/functions.sh

#
# Script which removes connection tracking file on tunnel down
#
# When the ppp link comes up, this script is called with the following
# parameters
#       $1      the interface name used by pppd (e.g. ppp3)
#       $2      the tty device name
#       $3      the tty device speed
#       $4      the local IP address for the interface
#       $5      the remote IP address
#       $6      the parameter specified by the 'ipparam' option to pppd
#

IFNAME_HASH="xl2tp-$(printf '%s' "${PEERNAME}" | md5sum | cut -c1-8)"

status=$(ubus call ip ip '{"params":["link", "del", "'"${IFNAME_HASH}"'"]}' | jsonfilter -e '@.code')
if [ "$status" -ne 0 ]; then
	logger -p daemon.notice -t "xl2tpd" "failed ubus call, status: $status"
fi
rm /tmp/state/l2tp/"${PEERNAME}".status
exit 0
