tag:
priority: 380
type: txt
help: l2tpv3 interface
val_help: <l2tpethN>; l2tpv3 interface name
syntax:expression: pattern $VAR(@) "^l2tpeth[0-9]+$" \
                   ; "tunnel must be (l2tpeth0-l2tpeth999)"

commit:expression: $VAR(./local-ip/) != "" ;                    \
                   "Must configure the l2tpv3 local-ip for $VAR(@)"
commit:expression: $VAR(./remote-ip/) != "" ;                   \
                   "Must configure the l2tpv3 remote-ip for $VAR(@)"
commit:expression: $VAR(./tunnel-id/) != "" ;                    \
                   "Must configure the l2tpv3 tunnel-id for $VAR(@)"
commit:expression: $VAR(./peer-tunnel-id/) != "" ;                    \
                   "Must configure the l2tpv3 peer-tunnel-id for $VAR(@)"
commit:expression: $VAR(./session-id/) != "" ;                    \
                   "Must configure the l2tpv3 session-id for $VAR(@)"
commit:expression: $VAR(./peer-session-id/) != "" ;                    \
                   "Must configure the l2tpv3 peer-session-id for $VAR(@)"

begin:
  [ -d /sys/module/l2tp_eth ] || sudo modprobe l2tp_eth
  [ -d /sys/module/l2tp_netlink ] || sudo modprobe l2tp_netlink
  if [ "$VAR(./encap/@)" = "ip" ] && [ ! -d /sys/module/l2tp_ip ] ; then
    sudo modprobe l2tp_ip
  fi

create:
  WAIT_COUNT=$VAR(./wait/@)
  if [ $WAIT_COUNT != 0 ]; then
    for i in `seq 1 $VAR(./wait/@)`
    do
      ping -c 1 $VAR(./remote-ip/@)  > /dev/null 2>&1 && break
      sleep 1
    done 
  fi
  ip l2tp add tunnel tunnel_id $VAR(./tunnel-id/@) peer_tunnel_id $VAR(./peer-tunnel-id/@) udp_sport $VAR(./source-port/@) udp_dport $VAR(./destination-port/@) encap $VAR(./encapsulation/@) local $VAR(./local-ip/@) remote $VAR(./remote-ip/@) ||
  echo "ip l2tp add tunnel tunnel_id $VAR(./tunnel-id/@) peer_tunnel_id  $VAR(./peer-tunnel-id/@) udp_sport $VAR(./sport/@) udp_dport $VAR(./dport/@) encap $VAR(./encap/@) local $VAR(./local-ip/@) remote $VAR(./remote-ip/@)"
  ip l2tp add session tunnel_id  $VAR(./tunnel-id/@) session_id $VAR(./session-id/@) peer_session_id $VAR(./peer-session-id/@) || echo "ip l2tp add session tunnel_id  $VAR(./tunnel-id/@) session_id $VAR(./session-id/@) peer_session_id $VAR(./peer-session-id/@)"

delete:
  ip link set $VAR(@) down
  if [ -n "$VAR(./tunnel-id@/)" ] && [ -n "$VAR(./session-id@/)" ] ; then
    ip l2tp del session $VAR(./tunnel-id/@) session_id $VAR(./session-id/@)
  fi
  if [ -n "$VAR(./tunnel-id@/)" ] ; then
    ip l2tp del tunnel tunnel_id  $VAR(./tunnel-id/@)
  fi
