Do not kill daemon, just not use pluggable kernel interface if initialization failed

This commit is contained in:
Martin Willi
2010-02-26 11:44:33 +01:00
parent 54f818590e
commit d6a27ec64e
6 changed files with 121 additions and 38 deletions
@@ -2566,9 +2566,18 @@ METHOD(kernel_ipsec_t, bypass_socket, bool,
METHOD(kernel_ipsec_t, destroy, void, METHOD(kernel_ipsec_t, destroy, void,
private_kernel_klips_ipsec_t *this) private_kernel_klips_ipsec_t *this)
{ {
this->job->cancel(this->job); if (this->job)
close(this->socket); {
close(this->socket_events); this->job->cancel(this->job);
}
if (this->socket > 0)
{
close(this->socket);
}
if (this->socket_evnets > 0)
{
close(this->socket_events);
}
this->mutex_pfkey->destroy(this->mutex_pfkey); this->mutex_pfkey->destroy(this->mutex_pfkey);
this->mutex->destroy(this->mutex); this->mutex->destroy(this->mutex);
this->ipsec_devices->destroy_function(this->ipsec_devices, (void*)ipsec_dev_destroy); this->ipsec_devices->destroy_function(this->ipsec_devices, (void*)ipsec_dev_destroy);
@@ -2616,21 +2625,27 @@ kernel_klips_ipsec_t *kernel_klips_ipsec_create()
this->socket = socket(PF_KEY, SOCK_RAW, PF_KEY_V2); this->socket = socket(PF_KEY, SOCK_RAW, PF_KEY_V2);
if (this->socket <= 0) if (this->socket <= 0)
{ {
charon->kill(charon, "unable to create PF_KEY socket"); DBG1(DBG_KNL, "unable to create PF_KEY socket");
destroy(this);
return NULL;
} }
/* create a PF_KEY socket for ACQUIRE & EXPIRE */ /* create a PF_KEY socket for ACQUIRE & EXPIRE */
this->socket_events = socket(PF_KEY, SOCK_RAW, PF_KEY_V2); this->socket_events = socket(PF_KEY, SOCK_RAW, PF_KEY_V2);
if (this->socket_events <= 0) if (this->socket_events <= 0)
{ {
charon->kill(charon, "unable to create PF_KEY event socket"); DBG1(DBG_KNL, "unable to create PF_KEY event socket");
destroy(this);
return NULL;
} }
/* register the event socket */ /* register the event socket */
if (register_pfkey_socket(this, SADB_SATYPE_ESP) != SUCCESS || if (register_pfkey_socket(this, SADB_SATYPE_ESP) != SUCCESS ||
register_pfkey_socket(this, SADB_SATYPE_AH) != SUCCESS) register_pfkey_socket(this, SADB_SATYPE_AH) != SUCCESS)
{ {
charon->kill(charon, "unable to register PF_KEY event socket"); DBG1(DBG_KNL, "unable to register PF_KEY event socket");
destroy(this);
return NULL;
} }
this->job = callback_job_create((callback_job_cb_t)receive_events, this->job = callback_job_create((callback_job_cb_t)receive_events,
@@ -2639,3 +2654,4 @@ kernel_klips_ipsec_t *kernel_klips_ipsec_create()
return &this->public; return &this->public;
} }
@@ -1939,9 +1939,15 @@ METHOD(kernel_ipsec_t, destroy, void,
enumerator_t *enumerator; enumerator_t *enumerator;
policy_entry_t *policy; policy_entry_t *policy;
this->job->cancel(this->job); if (this->job)
close(this->socket_xfrm_events); {
this->socket_xfrm->destroy(this->socket_xfrm); this->job->cancel(this->job);
}
if (this->socket_xfrm_events > 0)
{
close(this->socket_xfrm_events);
}
DESTROY_IF(this->socket_xfrm);
enumerator = this->policies->create_enumerator(this->policies); enumerator = this->policies->create_enumerator(this->policies);
while (enumerator->enumerate(enumerator, &policy, &policy)) while (enumerator->enumerate(enumerator, &policy, &policy))
{ {
@@ -1992,6 +1998,11 @@ kernel_netlink_ipsec_t *kernel_netlink_ipsec_create()
} }
this->socket_xfrm = netlink_socket_create(NETLINK_XFRM); this->socket_xfrm = netlink_socket_create(NETLINK_XFRM);
if (!this->socket_xfrm)
{
destroy(this);
return NULL;
}
memset(&addr, 0, sizeof(addr)); memset(&addr, 0, sizeof(addr));
addr.nl_family = AF_NETLINK; addr.nl_family = AF_NETLINK;
@@ -2000,13 +2011,17 @@ kernel_netlink_ipsec_t *kernel_netlink_ipsec_create()
this->socket_xfrm_events = socket(AF_NETLINK, SOCK_RAW, NETLINK_XFRM); this->socket_xfrm_events = socket(AF_NETLINK, SOCK_RAW, NETLINK_XFRM);
if (this->socket_xfrm_events <= 0) if (this->socket_xfrm_events <= 0)
{ {
charon->kill(charon, "unable to create XFRM event socket"); DBG1(DBG_KNL, "unable to create XFRM event socket");
destroy(this);
return NULL;
} }
addr.nl_groups = XFRMNLGRP(ACQUIRE) | XFRMNLGRP(EXPIRE) | addr.nl_groups = XFRMNLGRP(ACQUIRE) | XFRMNLGRP(EXPIRE) |
XFRMNLGRP(MIGRATE) | XFRMNLGRP(MAPPING); XFRMNLGRP(MIGRATE) | XFRMNLGRP(MAPPING);
if (bind(this->socket_xfrm_events, (struct sockaddr*)&addr, sizeof(addr))) if (bind(this->socket_xfrm_events, (struct sockaddr*)&addr, sizeof(addr)))
{ {
charon->kill(charon, "unable to bind XFRM event socket"); DBG1(DBG_KNL, "unable to bind XFRM event socket");
destroy(this);
return NULL;
} }
this->job = callback_job_create((callback_job_cb_t)receive_events, this->job = callback_job_create((callback_job_cb_t)receive_events,
this, NULL, NULL); this, NULL, NULL);
@@ -1336,10 +1336,15 @@ static void destroy(private_kernel_netlink_net_t *this)
manage_rule(this, RTM_DELRULE, AF_INET6, this->routing_table, manage_rule(this, RTM_DELRULE, AF_INET6, this->routing_table,
this->routing_table_prio); this->routing_table_prio);
} }
if (this->job)
this->job->cancel(this->job); {
close(this->socket_events); this->job->cancel(this->job);
this->socket->destroy(this->socket); }
if (this->socket_events > 0)
{
close(this->socket_events);
}
DESTROY_IF(this->socket);
this->ifaces->destroy_function(this->ifaces, (void*)iface_entry_destroy); this->ifaces->destroy_function(this->ifaces, (void*)iface_entry_destroy);
this->condvar->destroy(this->condvar); this->condvar->destroy(this->condvar);
this->mutex->destroy(this->mutex); this->mutex->destroy(this->mutex);
@@ -1380,21 +1385,26 @@ kernel_netlink_net_t *kernel_netlink_net_create()
"charon.install_virtual_ip", TRUE); "charon.install_virtual_ip", TRUE);
this->socket = netlink_socket_create(NETLINK_ROUTE); this->socket = netlink_socket_create(NETLINK_ROUTE);
this->job = NULL;
memset(&addr, 0, sizeof(addr)); memset(&addr, 0, sizeof(addr));
addr.nl_family = AF_NETLINK; addr.nl_family = AF_NETLINK;
/* create and bind RT socket for events (address/interface/route changes) */ /* create and bind RT socket for events (address/interface/route changes) */
this->socket_events = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE); this->socket_events = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
if (this->socket_events <= 0) if (this->socket_events < 0)
{ {
charon->kill(charon, "unable to create RT event socket"); DBG1(DBG_KNL, "unable to create RT event socket");
destroy(this);
return NULL;
} }
addr.nl_groups = RTMGRP_IPV4_IFADDR | RTMGRP_IPV6_IFADDR | addr.nl_groups = RTMGRP_IPV4_IFADDR | RTMGRP_IPV6_IFADDR |
RTMGRP_IPV4_ROUTE | RTMGRP_IPV4_ROUTE | RTMGRP_LINK; RTMGRP_IPV4_ROUTE | RTMGRP_IPV4_ROUTE | RTMGRP_LINK;
if (bind(this->socket_events, (struct sockaddr*)&addr, sizeof(addr))) if (bind(this->socket_events, (struct sockaddr*)&addr, sizeof(addr)))
{ {
charon->kill(charon, "unable to bind RT event socket"); DBG1(DBG_KNL, "unable to bind RT event socket");
destroy(this);
return NULL;
} }
this->job = callback_job_create((callback_job_cb_t)receive_events, this->job = callback_job_create((callback_job_cb_t)receive_events,
@@ -1403,7 +1413,9 @@ kernel_netlink_net_t *kernel_netlink_net_create()
if (init_address_list(this) != SUCCESS) if (init_address_list(this) != SUCCESS)
{ {
charon->kill(charon, "unable to get interface list"); DBG1(DBG_KNL, "unable to get interface list");
destroy(this);
return NULL;
} }
if (this->routing_table) if (this->routing_table)
@@ -236,7 +236,10 @@ static status_t netlink_send_ack(private_netlink_socket_t *this, struct nlmsghdr
*/ */
static void destroy(private_netlink_socket_t *this) static void destroy(private_netlink_socket_t *this)
{ {
close(this->socket); if (this->socket > 0)
{
close(this->socket);
}
this->mutex->destroy(this->mutex); this->mutex->destroy(this->mutex);
free(this); free(this);
} }
@@ -244,7 +247,8 @@ static void destroy(private_netlink_socket_t *this)
/** /**
* Described in header. * Described in header.
*/ */
netlink_socket_t *netlink_socket_create(int protocol) { netlink_socket_t *netlink_socket_create(int protocol)
{
private_netlink_socket_t *this = malloc_thing(private_netlink_socket_t); private_netlink_socket_t *this = malloc_thing(private_netlink_socket_t);
struct sockaddr_nl addr; struct sockaddr_nl addr;
@@ -262,15 +266,19 @@ netlink_socket_t *netlink_socket_create(int protocol) {
this->protocol = protocol; this->protocol = protocol;
this->socket = socket(AF_NETLINK, SOCK_RAW, protocol); this->socket = socket(AF_NETLINK, SOCK_RAW, protocol);
if (this->socket <= 0) if (this->socket < 0)
{ {
charon->kill(charon, "unable to create netlink socket"); DBG1(DBG_KNL, "unable to create netlink socket");
destroy(this);
return NULL;
} }
addr.nl_groups = 0; addr.nl_groups = 0;
if (bind(this->socket, (struct sockaddr*)&addr, sizeof(addr))) if (bind(this->socket, (struct sockaddr*)&addr, sizeof(addr)))
{ {
charon->kill(charon, "unable to bind netlink socket"); DBG1(DBG_KNL, "unable to bind netlink socket");
destroy(this);
return NULL;
} }
return &this->public; return &this->public;
@@ -2093,9 +2093,18 @@ METHOD(kernel_ipsec_t, bypass_socket, bool,
METHOD(kernel_ipsec_t, destroy, void, METHOD(kernel_ipsec_t, destroy, void,
private_kernel_pfkey_ipsec_t *this) private_kernel_pfkey_ipsec_t *this)
{ {
this->job->cancel(this->job); if (this->job)
close(this->socket); {
close(this->socket_events); this->job->cancel(this->job);
}
if (this->socket > 0)
{
close(this->socket);
}
if (this->socket_events > 0)
{
close(this->socket_events);
}
this->policies->destroy_function(this->policies, (void*)policy_entry_destroy); this->policies->destroy_function(this->policies, (void*)policy_entry_destroy);
this->mutex->destroy(this->mutex); this->mutex->destroy(this->mutex);
this->mutex_pfkey->destroy(this->mutex_pfkey); this->mutex_pfkey->destroy(this->mutex_pfkey);
@@ -2134,21 +2143,27 @@ kernel_pfkey_ipsec_t *kernel_pfkey_ipsec_create()
this->socket = socket(PF_KEY, SOCK_RAW, PF_KEY_V2); this->socket = socket(PF_KEY, SOCK_RAW, PF_KEY_V2);
if (this->socket <= 0) if (this->socket <= 0)
{ {
charon->kill(charon, "unable to create PF_KEY socket"); DBG1(DBG_KNL, "unable to create PF_KEY socket");
destroy(this);
return NULL;
} }
/* create a PF_KEY socket for ACQUIRE & EXPIRE */ /* create a PF_KEY socket for ACQUIRE & EXPIRE */
this->socket_events = socket(PF_KEY, SOCK_RAW, PF_KEY_V2); this->socket_events = socket(PF_KEY, SOCK_RAW, PF_KEY_V2);
if (this->socket_events <= 0) if (this->socket_events <= 0)
{ {
charon->kill(charon, "unable to create PF_KEY event socket"); DBG1(DBG_KNL, "unable to create PF_KEY event socket");
destroy(this);
return NULL;
} }
/* register the event socket */ /* register the event socket */
if (register_pfkey_socket(this, SADB_SATYPE_ESP) != SUCCESS || if (register_pfkey_socket(this, SADB_SATYPE_ESP) != SUCCESS ||
register_pfkey_socket(this, SADB_SATYPE_AH) != SUCCESS) register_pfkey_socket(this, SADB_SATYPE_AH) != SUCCESS)
{ {
charon->kill(charon, "unable to register PF_KEY event socket"); DBG1(DBG_KNL, "unable to register PF_KEY event socket");
destroy(this);
return NULL;
} }
this->job = callback_job_create((callback_job_cb_t)receive_events, this->job = callback_job_create((callback_job_cb_t)receive_events,
@@ -650,9 +650,18 @@ static status_t init_address_list(private_kernel_pfroute_net_t *this)
*/ */
static void destroy(private_kernel_pfroute_net_t *this) static void destroy(private_kernel_pfroute_net_t *this)
{ {
this->job->cancel(this->job); if (this->job)
close(this->socket); {
close(this->socket_events); this->job->cancel(this->job);
}
if (this->socket > 0)
{
close(this->socket);
}
if (this->socket_events)
{
close(this->socket_events);
}
this->ifaces->destroy_function(this->ifaces, (void*)iface_entry_destroy); this->ifaces->destroy_function(this->ifaces, (void*)iface_entry_destroy);
this->mutex->destroy(this->mutex); this->mutex->destroy(this->mutex);
this->mutex_pfroute->destroy(this->mutex_pfroute); this->mutex_pfroute->destroy(this->mutex_pfroute);
@@ -684,19 +693,25 @@ kernel_pfroute_net_t *kernel_pfroute_net_create()
this->mutex_pfroute = mutex_create(MUTEX_TYPE_DEFAULT); this->mutex_pfroute = mutex_create(MUTEX_TYPE_DEFAULT);
this->seq = 0; this->seq = 0;
this->socket_events = 0;
this->job = NULL;
/* create a PF_ROUTE socket to communicate with the kernel */ /* create a PF_ROUTE socket to communicate with the kernel */
this->socket = socket(PF_ROUTE, SOCK_RAW, AF_UNSPEC); this->socket = socket(PF_ROUTE, SOCK_RAW, AF_UNSPEC);
if (this->socket <= 0) if (this->socket < 0)
{ {
charon->kill(charon, "unable to create PF_ROUTE socket"); DBG1(DBG_KNL, "unable to create PF_ROUTE socket");
destroy(this);
return NULL;
} }
/* create a PF_ROUTE socket to receive events */ /* create a PF_ROUTE socket to receive events */
this->socket_events = socket(PF_ROUTE, SOCK_RAW, AF_UNSPEC); this->socket_events = socket(PF_ROUTE, SOCK_RAW, AF_UNSPEC);
if (this->socket_events <= 0) if (this->socket_events < 0)
{ {
charon->kill(charon, "unable to create PF_ROUTE event socket"); DBG1(DBG_KNL, "unable to create PF_ROUTE event socket");
destroy(this);
return NULL;
} }
this->job = callback_job_create((callback_job_cb_t)receive_events, this->job = callback_job_create((callback_job_cb_t)receive_events,
@@ -705,7 +720,9 @@ kernel_pfroute_net_t *kernel_pfroute_net_create()
if (init_address_list(this) != SUCCESS) if (init_address_list(this) != SUCCESS)
{ {
charon->kill(charon, "unable to get interface list"); DBG1(DBG_KNL, "unable to get interface list");
destroy(this);
return NULL;
} }
return &this->public; return &this->public;