implementation of an CFG attribute framework, currently supporting virtual IPs

updated ipsec.conf sourceip parameter to support
	CIDR notatation to serve from a pool
	%poolname to query a separate (database?) pool
This commit is contained in:
Martin Willi
2008-04-09 12:54:47 +00:00
parent 4a96521965
commit cdcfe777f4
19 changed files with 466 additions and 145 deletions
@@ -0,0 +1,145 @@
/*
* Copyright (C) 2008 Martin Willi
* Hochschule fuer Technik Rapperswil
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* $Id$
*/
#include "attribute_manager.h"
#include <utils/linked_list.h>
#include <utils/mutex.h>
typedef struct private_attribute_manager_t private_attribute_manager_t;
/**
* private data of attribute_manager
*/
struct private_attribute_manager_t {
/**
* public functions
*/
attribute_manager_t public;
/**
* list of registered providers
*/
linked_list_t *providers;
/**
* mutex to lock provider list
*/
mutex_t *mutex;
};
/**
* Implementation of attribute_manager_t.acquire_address.
*/
static host_t* acquire_address(private_attribute_manager_t *this,
char *pool, identification_t *id,
auth_info_t *auth, host_t *requested)
{
enumerator_t *enumerator;
attribute_provider_t *current;
host_t *host = NULL;
this->mutex->lock(this->mutex);
enumerator = this->providers->create_enumerator(this->providers);
while (enumerator->enumerate(enumerator, &current))
{
host = current->acquire_address(current, pool, id, auth, requested);
if (host)
{
break;
}
}
enumerator->destroy(enumerator);
this->mutex->unlock(this->mutex);
return host;
}
/**
* Implementation of attribute_manager_t.release_address.
*/
static void release_address(private_attribute_manager_t *this,
char *pool, host_t *address)
{
enumerator_t *enumerator;
attribute_provider_t *current;
this->mutex->lock(this->mutex);
enumerator = this->providers->create_enumerator(this->providers);
while (enumerator->enumerate(enumerator, &current))
{
if (current->release_address(current, pool, address))
{
break;
}
}
enumerator->destroy(enumerator);
this->mutex->unlock(this->mutex);
}
/**
* Implementation of attribute_manager_t.add_provider.
*/
static void add_provider(private_attribute_manager_t *this,
attribute_provider_t *provider)
{
this->mutex->lock(this->mutex);
this->providers->insert_last(this->providers, provider);
this->mutex->unlock(this->mutex);
}
/**
* Implementation of attribute_manager_t.remove_provider.
*/
static void remove_provider(private_attribute_manager_t *this,
attribute_provider_t *provider)
{
this->mutex->lock(this->mutex);
this->providers->remove(this->providers, provider, NULL);
this->mutex->unlock(this->mutex);
}
/**
* Implementation of attribute_manager_t.destroy
*/
static void destroy(private_attribute_manager_t *this)
{
this->providers->destroy(this->providers);
this->mutex->destroy(this->mutex);
free(this);
}
/*
* see header file
*/
attribute_manager_t *attribute_manager_create()
{
private_attribute_manager_t *this = malloc_thing(private_attribute_manager_t);
this->public.acquire_address = (host_t*(*)(attribute_manager_t*, char*, identification_t*,auth_info_t*,host_t*))acquire_address;
this->public.release_address = (void(*)(attribute_manager_t*, char *, host_t*))release_address;
this->public.add_provider = (void(*)(attribute_manager_t*, attribute_provider_t *provider))add_provider;
this->public.remove_provider = (void(*)(attribute_manager_t*, attribute_provider_t *provider))remove_provider;
this->public.destroy = (void(*)(attribute_manager_t*))destroy;
this->providers = linked_list_create();
this->mutex = mutex_create(MUTEX_DEFAULT);
return &this->public;
}
@@ -0,0 +1,82 @@
/*
* Copyright (C) 2008 Martin Willi
* Hochschule fuer Technik Rapperswil
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* $Id$
*/
/**
* @defgroup attribute_manager attribute_manager
* @{ @ingroup attributes
*/
#ifndef ATTRIBUTE_MANAGER_H_
#define ATTRIBUTE_MANAGER_H_
#include <config/attributes/attribute_provider.h>
typedef struct attribute_manager_t attribute_manager_t;
/**
* Provide configuration attributes to include in CFG Payloads.
*/
struct attribute_manager_t {
/**
* Acquire a virtual IP address to assign to a peer.
*
* @param pool pool name to acquire address from
* @param id peer identity to get address for
* @param auth authorization infos of peer
* @param requested IP in configuration request
* @return allocated address, NULL to serve none
*/
host_t* (*acquire_address)(attribute_manager_t *this,
char *pool, identification_t *id,
auth_info_t *auth, host_t *requested);
/**
* Release a previously acquired address.
*
* @param pool pool name from which the address was acquired
* @param address address to release
*/
void (*release_address)(attribute_manager_t *this,
char *pool, host_t *address);
/**
* Register an attribute provider to the manager.
*
* @param provider attribute provider to register
*/
void (*add_provider)(attribute_manager_t *this,
attribute_provider_t *provider);
/**
* Unregister an attribute provider from the manager.
*
* @param provider attribute provider to unregister
*/
void (*remove_provider)(attribute_manager_t *this,
attribute_provider_t *provider);
/**
* Destroy a attribute_manager instance.
*/
void (*destroy)(attribute_manager_t *this);
};
/**
* Create a attribute_manager instance.
*/
attribute_manager_t *attribute_manager_create();
#endif /* ATTRIBUTE_MANAGER_H_ @}*/
@@ -0,0 +1,60 @@
/*
* Copyright (C) 2008 Martin Willi
* Hochschule fuer Technik Rapperswil
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* $Id$
*/
/**
* @defgroup attribute_provider attribute_provider
* @{ @ingroup attributes
*/
#ifndef ATTRIBUTE_PROVIDER_H_
#define ATTRIBUTE_PROVIDER_H_
#include <library.h>
#include <utils/host.h>
#include <credentials/auth_info.h>
typedef struct attribute_provider_t attribute_provider_t;
/**
* Interface to provide attributes to peers through attribute manager.
*/
struct attribute_provider_t {
/**
* Acquire a virtual IP address to assign to a peer.
*
* @param pool name of the pool to acquire address from
* @param id peer ID
* @param auth authorization infos
* @param requested IP in configuration request
* @return allocated address, NULL to serve none
*/
host_t* (*acquire_address)(attribute_provider_t *this,
char *pool, identification_t *id,
auth_info_t *auth, host_t *requested);
/**
* Release a previously acquired address.
*
* @param pool name of the pool this address was acquired from
* @param address address to release
* @return TRUE if the address has been released by the provider
*/
bool (*release_address)(attribute_provider_t *this,
char *pool, host_t *address);
};
#endif /* ATTRIBUTE_PROVIDER_H_ @}*/
+21 -39
View File
@@ -153,12 +153,12 @@ struct private_peer_cfg_t {
/**
* virtual IP to use locally
*/
host_t *my_virtual_ip;
host_t *virtual_ip;
/**
* virtual IP to use remotly
* pool to acquire configuration attributes from
*/
host_t *other_virtual_ip;
char *pool;
/**
* required authorization constraints
@@ -396,35 +396,19 @@ static dpd_action_t get_dpd_action(private_peer_cfg_t *this)
}
/**
* Implementation of peer_cfg_t.get_my_virtual_ip.
* Implementation of peer_cfg_t.get_virtual_ip.
*/
static host_t* get_my_virtual_ip(private_peer_cfg_t *this)
static host_t* get_virtual_ip(private_peer_cfg_t *this)
{
if (this->my_virtual_ip == NULL)
{
return NULL;
}
return this->my_virtual_ip->clone(this->my_virtual_ip);
return this->virtual_ip;
}
/**
* Implementation of peer_cfg_t.get_other_virtual_ip.
* Implementation of peer_cfg_t.get_pool.
*/
static host_t* get_other_virtual_ip(private_peer_cfg_t *this, host_t *suggestion)
static char* get_pool(private_peer_cfg_t *this)
{
if (this->other_virtual_ip == NULL)
{ /* disallow */
return NULL;
}
if (!this->other_virtual_ip->is_anyaddr(this->other_virtual_ip))
{ /* force own configuration */
return this->other_virtual_ip->clone(this->other_virtual_ip);
}
if (suggestion == NULL || suggestion->is_anyaddr(suggestion))
{
return NULL;
}
return suggestion->clone(suggestion);
return this->pool;
}
/**
@@ -491,12 +475,10 @@ static bool equals(private_peer_cfg_t *this, private_peer_cfg_t *other)
this->over_time == other->over_time &&
this->dpd_delay == other->dpd_delay &&
this->dpd_action == other->dpd_action &&
(this->my_virtual_ip == other->my_virtual_ip ||
(this->my_virtual_ip && other->my_virtual_ip &&
this->my_virtual_ip->equals(this->my_virtual_ip, other->my_virtual_ip))) &&
(this->other_virtual_ip == other->other_virtual_ip ||
(this->other_virtual_ip && other->other_virtual_ip &&
this->other_virtual_ip->equals(this->other_virtual_ip, other->other_virtual_ip))) &&
(this->virtual_ip == other->virtual_ip ||
(this->virtual_ip && other->virtual_ip &&
this->virtual_ip->equals(this->virtual_ip, other->virtual_ip))) &&
(this->pool == other->pool || streq(this->pool, other->pool)) &&
this->auth->equals(this->auth, other->auth)
#ifdef ME
&& this->mediation == other->mediation &&
@@ -527,14 +509,14 @@ static void destroy(private_peer_cfg_t *this)
this->child_cfgs->destroy_offset(this->child_cfgs, offsetof(child_cfg_t, destroy));
this->my_id->destroy(this->my_id);
this->other_id->destroy(this->other_id);
DESTROY_IF(this->my_virtual_ip);
DESTROY_IF(this->other_virtual_ip);
DESTROY_IF(this->virtual_ip);
this->auth->destroy(this->auth);
#ifdef ME
DESTROY_IF(this->mediated_by);
DESTROY_IF(this->peer_id);
#endif /* ME */
free(this->name);
free(this->pool);
free(this);
}
}
@@ -551,7 +533,7 @@ peer_cfg_t *peer_cfg_create(char *name, u_int ike_version, ike_cfg_t *ike_cfg,
u_int32_t reauth_time, u_int32_t jitter_time,
u_int32_t over_time, bool mobike,
u_int32_t dpd_delay, dpd_action_t dpd_action,
host_t *my_virtual_ip, host_t *other_virtual_ip,
host_t *virtual_ip, char *pool,
bool mediation, peer_cfg_t *mediated_by,
identification_t *peer_id)
{
@@ -577,8 +559,8 @@ peer_cfg_t *peer_cfg_create(char *name, u_int ike_version, ike_cfg_t *ike_cfg,
this->public.use_mobike = (bool (*) (peer_cfg_t *))use_mobike;
this->public.get_dpd_delay = (u_int32_t (*) (peer_cfg_t *))get_dpd_delay;
this->public.get_dpd_action = (dpd_action_t (*) (peer_cfg_t *))get_dpd_action;
this->public.get_my_virtual_ip = (host_t* (*) (peer_cfg_t *))get_my_virtual_ip;
this->public.get_other_virtual_ip = (host_t* (*) (peer_cfg_t *, host_t *))get_other_virtual_ip;
this->public.get_virtual_ip = (host_t* (*) (peer_cfg_t *))get_virtual_ip;
this->public.get_pool = (char*(*)(peer_cfg_t*))get_pool;
this->public.get_auth = (auth_info_t*(*)(peer_cfg_t*))get_auth;
this->public.equals = (bool(*)(peer_cfg_t*, peer_cfg_t *other))equals;
this->public.get_ref = (void(*)(peer_cfg_t *))get_ref;
@@ -617,8 +599,8 @@ peer_cfg_t *peer_cfg_create(char *name, u_int ike_version, ike_cfg_t *ike_cfg,
this->use_mobike = mobike;
this->dpd_delay = dpd_delay;
this->dpd_action = dpd_action;
this->my_virtual_ip = my_virtual_ip;
this->other_virtual_ip = other_virtual_ip;
this->virtual_ip = virtual_ip;
this->pool = pool ? strdup(pool) : NULL;
this->auth = auth_info_create();
this->refcount = 1;
#ifdef ME
+8 -13
View File
@@ -276,21 +276,16 @@ struct peer_cfg_t {
* used for a request and may be changed by the server.
*
* @param suggestion NULL, %any or specific
* @return clone of an IP, %any or NULL
* @return virtual IP, %any or NULL
*/
host_t* (*get_my_virtual_ip) (peer_cfg_t *this);
host_t* (*get_virtual_ip) (peer_cfg_t *this);
/**
* Get a virtual IP for the remote peer.
* Get the name of the pool to acquire configuration attributes from.
*
* An IP may be supplied, if one was requested by the initiator. However,
* the suggestion is not more as it says, any address may be returned, even
* NULL to not use virtual IPs.
*
* @param suggestion NULL, %any or specific
* @return clone of an IP to use
* @return pool name, NULL if none defined
*/
host_t* (*get_other_virtual_ip) (peer_cfg_t *this, host_t *suggestion);
char* (*get_pool)(peer_cfg_t *this);
#ifdef ME
/**
@@ -378,8 +373,8 @@ struct peer_cfg_t {
* @param mobike use MOBIKE (RFC4555) if peer supports it
* @param dpd_delay after how many seconds of inactivity to check DPD
* @param dpd_action what to do with CHILD_SAs when detected a dead peer
* @param my_virtual_ip virtual IP for local host, or NULL
* @param other_virtual_ip virtual IP for remote host, or NULL
* @param virtual_ip virtual IP for local host, or NULL
* @param pool pool name to get configuration attributes from, or NULL
* @param mediation TRUE if this is a mediation connection
* @param mediated_by peer_cfg_t of the mediation connection to mediate through
* @param peer_id ID that identifies our peer at the mediation server
@@ -394,7 +389,7 @@ peer_cfg_t *peer_cfg_create(char *name, u_int ikev_version, ike_cfg_t *ike_cfg,
u_int32_t reauth_time, u_int32_t jitter_time,
u_int32_t over_time, bool mobike,
u_int32_t dpd_delay, dpd_action_t dpd_action,
host_t *my_virtual_ip, host_t *other_virtual_ip,
host_t *virtual_ip, char *pool,
bool mediation, peer_cfg_t *mediated_by,
identification_t *peer_id);