diff options
Diffstat (limited to 'src/charon/config')
-rw-r--r-- | src/charon/config/connections/local_connection_store.c | 14 | ||||
-rw-r--r-- | src/charon/config/policies/policy.c | 54 | ||||
-rw-r--r-- | src/charon/config/traffic_selector.c | 199 | ||||
-rw-r--r-- | src/charon/config/traffic_selector.h | 83 |
4 files changed, 223 insertions, 127 deletions
diff --git a/src/charon/config/connections/local_connection_store.c b/src/charon/config/connections/local_connection_store.c index 31d466e4d..c6e982e23 100644 --- a/src/charon/config/connections/local_connection_store.c +++ b/src/charon/config/connections/local_connection_store.c @@ -75,7 +75,7 @@ static connection_t *get_connection_by_hosts(private_local_connection_store_t *t connection_t *found = NULL; this->logger->log(this->logger, CONTROL|LEVEL1, "looking for connection for host pair %s...%s", - my_host->get_address(my_host), other_host->get_address(other_host)); + my_host->get_string(my_host), other_host->get_string(other_host)); pthread_mutex_lock(&(this->mutex)); iterator = this->connections->create_iterator(this->connections, TRUE); @@ -108,8 +108,8 @@ static connection_t *get_connection_by_hosts(private_local_connection_store_t *t this->logger->log(this->logger, CONTROL|LEVEL2, "candidate connection \"%s\": %s...%s (prio=%d)", candidate->get_name(candidate), - candidate_my_host->get_address(candidate_my_host), - candidate_other_host->get_address(candidate_other_host), + candidate_my_host->get_string(candidate_my_host), + candidate_other_host->get_string(candidate_other_host), prio); if (prio > best_prio) @@ -129,8 +129,8 @@ static connection_t *get_connection_by_hosts(private_local_connection_store_t *t this->logger->log(this->logger, CONTROL, "found matching connection \"%s\": %s...%s (prio=%d)", found->get_name(found), - found_my_host->get_address(found_my_host), - found_other_host->get_address(found_other_host), + found_my_host->get_string(found_my_host), + found_other_host->get_string(found_other_host), best_prio); found = found->clone(found); @@ -243,8 +243,8 @@ void log_connections(private_local_connection_store_t *this, logger_t *logger, c logger->log(logger, CONTROL, " \"%s\": %s...%s", current->get_name(current), - my_host->get_address(my_host), - other_host->get_address(other_host)); + my_host->get_string(my_host), + other_host->get_string(other_host)); } } iterator->destroy(iterator); diff --git a/src/charon/config/policies/policy.c b/src/charon/config/policies/policy.c index 561c28f12..152c157a8 100644 --- a/src/charon/config/policies/policy.c +++ b/src/charon/config/policies/policy.c @@ -29,7 +29,7 @@ #include <utils/linked_list.h> #include <utils/identification.h> -#include <utils/logger.h> +#include <utils/logger_manager.h> typedef struct private_policy_t private_policy_t; @@ -105,9 +105,9 @@ struct private_policy_t { u_int32_t jitter; /** - * select_traffic_selectors for both + * logger */ - linked_list_t *(*select_traffic_selectors) (private_policy_t *,linked_list_t*,linked_list_t*); + logger_t *logger; }; /** @@ -202,21 +202,6 @@ static linked_list_t *get_other_traffic_selectors(private_policy_t *this, traffi } /** - * Implementation of private_policy_t.select_my_traffic_selectors - */ -static linked_list_t *select_my_traffic_selectors(private_policy_t *this, linked_list_t *supplied) -{ - return this->select_traffic_selectors(this, this->my_ts, supplied); -} - -/** - * Implementation of private_policy_t.select_other_traffic_selectors - */ -static linked_list_t *select_other_traffic_selectors(private_policy_t *this, linked_list_t *supplied) -{ - return this->select_traffic_selectors(this, this->other_ts, supplied); -} -/** * Implementation of private_policy_t.select_traffic_selectors */ static linked_list_t *select_traffic_selectors(private_policy_t *this, linked_list_t *stored, linked_list_t *supplied) @@ -225,6 +210,9 @@ static linked_list_t *select_traffic_selectors(private_policy_t *this, linked_li traffic_selector_t *supplied_ts, *stored_ts, *selected_ts; linked_list_t *selected = linked_list_create(); + this->logger->log(this->logger, CONTROL|LEVEL1, + "selecting traffic selectors for %s host", + stored == this->my_ts ? "local" : "remote"); stored_iter = stored->create_iterator(stored, TRUE); supplied_iter = supplied->create_iterator(supplied, TRUE); @@ -240,11 +228,19 @@ static linked_list_t *select_traffic_selectors(private_policy_t *this, linked_li { supplied_iter->current(supplied_iter, (void**)&supplied_ts); + this->logger->log(this->logger, CONTROL|LEVEL2, + " stored %s <=> %s received", + stored_ts->get_string(stored_ts), + supplied_ts->get_string(supplied_ts)); + selected_ts = stored_ts->get_subset(stored_ts, supplied_ts); if (selected_ts) { /* got a match, add to list */ selected->insert_last(selected, (void*)selected_ts); + + this->logger->log(this->logger, CONTROL|LEVEL1, " got a match: %s", + selected_ts->get_string(selected_ts)); } } } @@ -255,6 +251,22 @@ static linked_list_t *select_traffic_selectors(private_policy_t *this, linked_li } /** + * Implementation of private_policy_t.select_my_traffic_selectors + */ +static linked_list_t *select_my_traffic_selectors(private_policy_t *this, linked_list_t *supplied) +{ + return select_traffic_selectors(this, this->my_ts, supplied); +} + +/** + * Implementation of private_policy_t.select_other_traffic_selectors + */ +static linked_list_t *select_other_traffic_selectors(private_policy_t *this, linked_list_t *supplied) +{ + return select_traffic_selectors(this, this->other_ts, supplied); +} + +/** * Implementation of policy_t.get_proposal_iterator */ static linked_list_t *get_proposals(private_policy_t *this) @@ -365,7 +377,7 @@ static u_int32_t get_hard_lifetime(private_policy_t *this) /** * Implements policy_t.clone. */ -static policy_t *clone(private_policy_t *this) +static policy_t *clone_(private_policy_t *this) { private_policy_t *clone = (private_policy_t*)policy_create(this->name, this->my_id->clone(this->my_id), @@ -507,7 +519,7 @@ policy_t *policy_create(char *name, identification_t *my_id, identification_t *o this->public.add_updown = (void(*)(policy_t*,char*))add_updown; this->public.get_soft_lifetime = (u_int32_t (*) (policy_t *))get_soft_lifetime; this->public.get_hard_lifetime = (u_int32_t (*) (policy_t *))get_hard_lifetime; - this->public.clone = (policy_t*(*)(policy_t*))clone; + this->public.clone = (policy_t*(*)(policy_t*))clone_; this->public.destroy = (void(*)(policy_t*))destroy; /* apply init values */ @@ -521,10 +533,10 @@ policy_t *policy_create(char *name, identification_t *my_id, identification_t *o /* initialize private members*/ this->my_ca = NULL; this->other_ca = NULL; - this->select_traffic_selectors = select_traffic_selectors; this->proposals = linked_list_create(); this->my_ts = linked_list_create(); this->other_ts = linked_list_create(); + this->logger = logger_manager->get_logger(logger_manager, CONFIG); return (&this->public); } diff --git a/src/charon/config/traffic_selector.c b/src/charon/config/traffic_selector.c index 203efe299..b0b0a13ce 100644 --- a/src/charon/config/traffic_selector.c +++ b/src/charon/config/traffic_selector.c @@ -21,13 +21,14 @@ * for more details. */ +#include <arpa/inet.h> +#include <string.h> +#include <netdb.h> + #include "traffic_selector.h" #include <utils/linked_list.h> #include <utils/identification.h> -#include <utils/logger_manager.h> -#include <arpa/inet.h> -#include <string.h> typedef struct private_traffic_selector_t private_traffic_selector_t; @@ -76,9 +77,9 @@ struct private_traffic_selector_t { u_int16_t to_port; /** - * Logger reference + * string representation of this traffic selector */ - logger_t *logger; + char *string; }; /** @@ -87,6 +88,122 @@ struct private_traffic_selector_t { static private_traffic_selector_t *traffic_selector_create(u_int8_t protocol, ts_type_t type, u_int16_t from_port, u_int16_t to_port); /** + * update the string representation of this traffic selector + */ +static void update_string(private_traffic_selector_t *this) +{ + char buf[256]; + struct protoent *proto; + struct servent *serv; + char *serv_proto = NULL; + char proto_str[8] = ""; + char addr_str[INET6_ADDRSTRLEN]; + char port_str[16] = ""; + char mask_str[8] = ""; + char proto_port_str[32] = ""; + bool has_proto = FALSE, has_port = FALSE; + + if (this->type == TS_IPV4_ADDR_RANGE) + { + u_int32_t from_no, to_no, bit; + u_int8_t mask = 32; + + /* build address string */ + from_no = htonl(this->from_addr_ipv4); + to_no = htonl(this->to_addr_ipv4); + inet_ntop(AF_INET, &from_no, addr_str, sizeof(addr_str)); + + /* build network mask string */ + for (bit = 0; bit < 32; bit++) + { + if ((1<<bit & from_no) != (1<<bit & to_no)) + { + mask = bit; + break; + } + } + if (mask != 32) + { + snprintf(mask_str, sizeof(mask_str), "/%d", mask); + } + } + else + { + /* TODO: be a little bit more verbose ;-) */ + snprintf(addr_str, sizeof(addr_str), "(IPv6 address range)"); + } + + /* build protocol string */ + if (this->protocol) + { + proto = getprotobynumber(this->protocol); + if (proto) + { + snprintf(proto_str, sizeof(proto_str), "%s", proto->p_name); + serv_proto = proto->p_name; + } + else + { + snprintf(proto_str, sizeof(proto_str), "%d", this->protocol); + } + has_proto = TRUE; + } + + /* build port string */ + if (this->from_port == this->to_port) + { + serv = getservbyport(htons(this->from_port), serv_proto); + if (serv) + { + snprintf(port_str, sizeof(port_str), "%s", serv->s_name); + } + else + { + snprintf(port_str, sizeof(port_str), "%d", this->from_port); + } + has_port = TRUE; + } + else if (!(this->from_port == 0 && this->to_port == 0xFFFF)) + { + snprintf(port_str, sizeof(port_str), "%d-%d", + this->from_port, this->to_port); + has_port = TRUE; + } + + /* concatenate port & proto string */ + if (has_proto && has_port) + { + snprintf(proto_port_str, sizeof(proto_port_str), "[%s/%s]", + proto_str, port_str); + } + else if (has_proto) + { + snprintf(proto_port_str, sizeof(proto_port_str), "[%s]", proto_str); + } + else if (has_port) + { + snprintf(proto_port_str, sizeof(proto_port_str), "[%s]", port_str); + } + + /* concatenate it all */ + snprintf(buf, sizeof(buf), "%s%s%s", addr_str, mask_str, proto_port_str); + + if (this->string) + { + free(this->string); + } + this->string = strdup(buf); +} + +/** + * implements traffic_selector_t.get_string + */ +static char *get_string(private_traffic_selector_t *this) +{ + return this->string; +} + +/** * implements traffic_selector_t.get_subset */ static traffic_selector_t *get_subset(private_traffic_selector_t *this, private_traffic_selector_t *other) @@ -99,19 +216,12 @@ static traffic_selector_t *get_subset(private_traffic_selector_t *this, private_ u_int8_t protocol; private_traffic_selector_t *new_ts; - /* TODO: make output more human readable */ - this->logger->log(this->logger, CONTROL|LEVEL2, - "matching traffic selector ranges %x:%d-%x:%d <=> %x:%d-%x:%d", - this->from_addr_ipv4, this->from_port, this->to_addr_ipv4, this->to_port, - other->from_addr_ipv4, other->from_port, other->to_addr_ipv4, other->to_port); /* calculate the maximum address range allowed for both */ from_addr = max(this->from_addr_ipv4, other->from_addr_ipv4); to_addr = min(this->to_addr_ipv4, other->to_addr_ipv4); if (from_addr > to_addr) { - this->logger->log(this->logger, CONTROL|LEVEL2, - "no match in address range"); - return NULL; + return NULL; } /* calculate the maximum port range allowed for both */ @@ -119,9 +229,7 @@ static traffic_selector_t *get_subset(private_traffic_selector_t *this, private_ to_port = min(this->to_port, other->to_port); if (from_port > to_port) { - this->logger->log(this->logger, CONTROL|LEVEL2, - "no match in port range"); - return NULL; + return NULL; } /* select protocol, which is not zero */ @@ -132,10 +240,8 @@ static traffic_selector_t *get_subset(private_traffic_selector_t *this, private_ new_ts->from_addr_ipv4 = from_addr; new_ts->to_addr_ipv4 = to_addr; new_ts->type = TS_IPV4_ADDR_RANGE; + update_string(new_ts); - this->logger->log(this->logger, CONTROL|LEVEL2, - "got a match: %x:%d-%x:%d", - new_ts->from_addr_ipv4, new_ts->from_port, new_ts->to_addr_ipv4, new_ts->to_port); return &(new_ts->public); } return NULL; @@ -228,35 +334,6 @@ static u_int8_t get_protocol(private_traffic_selector_t *this) } /** - * Implements traffic_selector_t.get_netmask. - */ -static u_int8_t get_netmask(private_traffic_selector_t *this) -{ - switch (this->type) - { - case TS_IPV4_ADDR_RANGE: - { - u_int32_t from, to, bit; - from = htonl(this->from_addr_ipv4); - to = htonl(this->to_addr_ipv4); - for (bit = 0; bit < 32; bit++) - { - if ((1<<bit & from) != (1<<bit & to)) - { - return bit; - } - } - return 32; - } - case TS_IPV6_ADDR_RANGE: - default: - { - return 0; - } - } -} - -/** * Implements traffic_selector_t.update_address_range. */ static void update_address_range(private_traffic_selector_t *this, host_t *host) @@ -266,12 +343,12 @@ static void update_address_range(private_traffic_selector_t *this, host_t *host) { if (this->from_addr_ipv4 == 0) { - chunk_t from = host->get_address_as_chunk(host); + chunk_t from = host->get_address(host); this->from_addr_ipv4 = ntohl(*((u_int32_t*)from.ptr)); this->to_addr_ipv4 = this->from_addr_ipv4; - chunk_free(&from); } } + update_string(this); } /** @@ -281,6 +358,7 @@ static traffic_selector_t *clone_(private_traffic_selector_t *this) { private_traffic_selector_t *clone = traffic_selector_create(this->protocol, this->type, this->from_port, this->to_port); clone->type = this->type; + clone->string = strdup(this->string); switch (clone->type) { case TS_IPV4_ADDR_RANGE: @@ -302,14 +380,15 @@ static traffic_selector_t *clone_(private_traffic_selector_t *this) * Implements traffic_selector_t.destroy. */ static void destroy(private_traffic_selector_t *this) -{ +{ + free(this->string); free(this); } /* * see header */ -traffic_selector_t *traffic_selector_create_from_bytes(u_int8_t protocol, ts_type_t type, chunk_t from_addr, int16_t from_port, chunk_t to_addr, u_int16_t to_port) +traffic_selector_t *traffic_selector_create_from_bytes(u_int8_t protocol, ts_type_t type, chunk_t from_addr, u_int16_t from_port, chunk_t to_addr, u_int16_t to_port) { private_traffic_selector_t *this = traffic_selector_create(protocol, type, from_port, to_port); @@ -335,6 +414,9 @@ traffic_selector_t *traffic_selector_create_from_bytes(u_int8_t protocol, ts_typ return NULL; } } + + update_string(this); + return (&this->public); } @@ -352,7 +434,7 @@ traffic_selector_t *traffic_selector_create_from_subnet(host_t *net, u_int8_t ne chunk_t from; this->type = TS_IPV4_ADDR_RANGE; - from = net->get_address_as_chunk(net); + from = net->get_address(net); this->from_addr_ipv4 = ntohl(*((u_int32_t*)from.ptr)); if (this->from_addr_ipv4 == 0) { @@ -363,7 +445,6 @@ traffic_selector_t *traffic_selector_create_from_subnet(host_t *net, u_int8_t ne { this->to_addr_ipv4 = this->from_addr_ipv4 | ((1 << (32 - netbits)) - 1); } - chunk_free(&from); break; } case AF_INET6: @@ -379,6 +460,8 @@ traffic_selector_t *traffic_selector_create_from_subnet(host_t *net, u_int8_t ne this->to_port = port; } + update_string(this); + return (&this->public); } @@ -419,7 +502,9 @@ traffic_selector_t *traffic_selector_create_from_string(u_int8_t protocol, ts_ty return NULL; } } - + + update_string(this); + return (&this->public); } @@ -432,13 +517,13 @@ static private_traffic_selector_t *traffic_selector_create(u_int8_t protocol, ts /* public functions */ this->public.get_subset = (traffic_selector_t*(*)(traffic_selector_t*,traffic_selector_t*))get_subset; + this->public.get_string = (char*(*)(traffic_selector_t*))get_string; this->public.get_from_address = (chunk_t(*)(traffic_selector_t*))get_from_address; this->public.get_to_address = (chunk_t(*)(traffic_selector_t*))get_to_address; this->public.get_from_port = (u_int16_t(*)(traffic_selector_t*))get_from_port; this->public.get_to_port = (u_int16_t(*)(traffic_selector_t*))get_to_port; this->public.get_type = (ts_type_t(*)(traffic_selector_t*))get_type; this->public.get_protocol = (u_int8_t(*)(traffic_selector_t*))get_protocol; - this->public.get_netmask = (u_int8_t(*)(traffic_selector_t*))get_netmask; this->public.update_address_range = (void(*)(traffic_selector_t*,host_t*))update_address_range; this->public.clone = (traffic_selector_t*(*)(traffic_selector_t*))clone_; this->public.destroy = (void(*)(traffic_selector_t*))destroy; @@ -447,7 +532,7 @@ static private_traffic_selector_t *traffic_selector_create(u_int8_t protocol, ts this->to_port = to_port; this->protocol = protocol; this->type = type; - this->logger = logger_manager->get_logger(logger_manager, CONFIG); + this->string = NULL; return this; } diff --git a/src/charon/config/traffic_selector.h b/src/charon/config/traffic_selector.h index ef0711418..420f97d0f 100644 --- a/src/charon/config/traffic_selector.h +++ b/src/charon/config/traffic_selector.h @@ -82,20 +82,21 @@ struct traffic_selector_t { /** * @brief Compare two traffic selectors, and create a new one * which is the largest subset of both (subnet & port). - * + * * Resulting traffic_selector is newly created and must be destroyed. - * + * * @param this first to compare * @param other second to compare * @return * - created subset of them * - or NULL if no match between this and other */ - traffic_selector_t *(*get_subset) (traffic_selector_t *this, traffic_selector_t *other); + traffic_selector_t *(*get_subset) (traffic_selector_t *this, + traffic_selector_t *other); /** * @brief Clone a traffic selector. - * + * * @param this traffic selector to clone * @return clone of it */ @@ -103,12 +104,9 @@ struct traffic_selector_t { /** * @brief Get starting address of this ts as a chunk. - * - * Data is in network order and represents the address. - * Size depends on protocol. - * - * Resulting chunk data is allocated and must be freed! - * + * + * Chunk is in network order gets allocated. + * * @param this calling object * @return chunk containing the address */ @@ -116,12 +114,9 @@ struct traffic_selector_t { /** * @brief Get ending address of this ts as a chunk. - * - * Data is in network order and represents the address. - * Size depends on protocol. - * - * Resulting chunk data is allocated and must be freed! - * + * + * Chunk is in network order gets allocated. + * * @param this calling object * @return chunk containing the address */ @@ -140,10 +135,10 @@ struct traffic_selector_t { /** * @brief Get ending port of this ts. - * + * * Port is in host order, since the parser converts it. * Size depends on protocol. - * + * * @param this calling object * @return port */ @@ -151,51 +146,47 @@ struct traffic_selector_t { /** * @brief Get the type of the traffic selector. - * + * * @param this calling obect * @return ts_type_t specifying the type */ ts_type_t (*get_type) (traffic_selector_t *this); - + /** * @brief Get the protocol id of this ts. - * + * * @param this calling obect * @return protocol id */ u_int8_t (*get_protocol) (traffic_selector_t *this); - - /** - * @brief Get the netmask of the address range. - * - * Returns the number of bits associated to the subnet. - * (As the "24" in "192.168.0.0/24"). This is approximated - * if the address range is not a complete subnet! Since Linux - * does not support full IP address ranges (yet), we can't do this - * (much) better. - * - * @param this calling obect - * @return netmask as "bits for subnet" - */ - u_int8_t (*get_netmask) (traffic_selector_t *this); - + /** * @brief Update the address of a traffic selector. - * + * * Update the address range of a traffic selector, * if the current address is 0.0.0.0. The new address range * starts from the supplied address and also ends there * (which means it is a one-host-address-range ;-). - * + * * @param this calling obect * @param host host_t specifying the address range */ void (*update_address_range) (traffic_selector_t *this, host_t* host); /** - * @brief Destroys the ts object + * @brief Get a string representation of the traffic selector. + * + * String points to internal data, do not free/modify. * * @param this calling object + * @return pointer to a string. + */ + char* (*get_string) (traffic_selector_t *this); + + /** + * @brief Destroys the ts object + * + * @param this calling object */ void (*destroy) (traffic_selector_t *this); }; @@ -215,7 +206,10 @@ struct traffic_selector_t { * * @ingroup config */ -traffic_selector_t *traffic_selector_create_from_string(u_int8_t protocol, ts_type_t type, char *from_addr, u_int16_t from_port, char *to_addr, u_int16_t to_port); +traffic_selector_t *traffic_selector_create_from_string( + u_int8_t protocol, ts_type_t type, + char *from_addr, u_int16_t from_port, + char *to_addr, u_int16_t to_port); /** * @brief Create a new traffic selector using data read from the net. @@ -236,7 +230,10 @@ traffic_selector_t *traffic_selector_create_from_string(u_int8_t protocol, ts_ty * * @ingroup config */ -traffic_selector_t *traffic_selector_create_from_bytes(u_int8_t protocol, ts_type_t type, chunk_t from_address, int16_t from_port, chunk_t to_address, u_int16_t to_port); +traffic_selector_t *traffic_selector_create_from_bytes( + u_int8_t protocol, ts_type_t type, + chunk_t from_address, u_int16_t from_port, + chunk_t to_address, u_int16_t to_port); /** * @brief Create a new traffic selector defining a whole subnet. @@ -256,6 +253,8 @@ traffic_selector_t *traffic_selector_create_from_bytes(u_int8_t protocol, ts_typ * * @ingroup config */ -traffic_selector_t *traffic_selector_create_from_subnet(host_t *net, u_int8_t netbits, u_int8_t protocol, u_int16_t port); +traffic_selector_t *traffic_selector_create_from_subnet( + host_t *net, u_int8_t netbits, + u_int8_t protocol, u_int16_t port); #endif /* TRAFFIC_SELECTOR_H_ */ |