merging modularized kernel interface back to trunk

This commit is contained in:
Tobias Brunner
2008-09-25 07:56:58 +00:00
parent 80fc5bd95c
commit 507f26f685
21 changed files with 4299 additions and 2999 deletions
@@ -0,0 +1,11 @@
INCLUDES = -I${linuxdir} -I$(top_srcdir)/src/libstrongswan -I$(top_srcdir)/src/charon
AM_CFLAGS = -rdynamic
plugin_LTLIBRARIES = libstrongswan-kernel-netlink.la
libstrongswan_kernel_netlink_la_SOURCES = kernel_netlink_plugin.h kernel_netlink_plugin.c \
kernel_netlink_ipsec.h kernel_netlink_ipsec.c kernel_netlink_net.h kernel_netlink_net.c \
kernel_netlink_shared.h kernel_netlink_shared.c
libstrongswan_kernel_netlink_la_LDFLAGS = -module
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,48 @@
/*
* Copyright (C) 2008 Tobias Brunner
* 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 kernel_netlink_ipsec_i kernel_netlink_ipsec
* @{ @ingroup kernel_netlink
*/
#ifndef KERNEL_NETLINK_IPSEC_H_
#define KERNEL_NETLINK_IPSEC_H_
#include <kernel/kernel_ipsec.h>
typedef struct kernel_netlink_ipsec_t kernel_netlink_ipsec_t;
/**
* Implementation of the kernel ipsec interface using Netlink.
*/
struct kernel_netlink_ipsec_t {
/**
* Implements kernel_ipsec_t interface
*/
kernel_ipsec_t interface;
};
/**
* Create a netlink kernel ipsec interface instance.
*
* @return kernel_netlink_ipsec_t instance
*/
kernel_netlink_ipsec_t *kernel_netlink_ipsec_create();
#endif /* KERNEL_NETLINK_IPSEC_H_ */
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,48 @@
/*
* Copyright (C) 2008 Tobias Brunner
* 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 kernel_netlink_net_i kernel_netlink_net
* @{ @ingroup kernel_netlink
*/
#ifndef KERNEL_NETLINK_NET_H_
#define KERNEL_NETLINK_NET_H_
#include <kernel/kernel_net.h>
typedef struct kernel_netlink_net_t kernel_netlink_net_t;
/**
* Implementation of the kernel network interface using Netlink.
*/
struct kernel_netlink_net_t {
/**
* Implements kernel_net_t interface
*/
kernel_net_t interface;
};
/**
* Create a netlink kernel network interface instance.
*
* @return kernel_netlink_net_t instance
*/
kernel_netlink_net_t *kernel_netlink_net_create();
#endif /* KERNEL_NETLINK_NET_H_ @} */
@@ -0,0 +1,61 @@
/*
* Copyright (C) 2008 Tobias Brunner
* 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 "kernel_netlink_plugin.h"
#include "kernel_netlink_ipsec.h"
#include "kernel_netlink_net.h"
#include <daemon.h>
typedef struct private_kernel_netlink_plugin_t private_kernel_netlink_plugin_t;
/**
* private data of kernel netlink plugin
*/
struct private_kernel_netlink_plugin_t {
/**
* implements plugin interface
*/
kernel_netlink_plugin_t public;
};
/**
* Implementation of plugin_t.destroy
*/
static void destroy(private_kernel_netlink_plugin_t *this)
{
charon->kernel_interface->remove_ipsec_interface(charon->kernel_interface, (kernel_ipsec_constructor_t)kernel_netlink_ipsec_create);
charon->kernel_interface->remove_net_interface(charon->kernel_interface, (kernel_net_constructor_t)kernel_netlink_net_create);
free(this);
}
/*
* see header file
*/
plugin_t *plugin_create()
{
private_kernel_netlink_plugin_t *this = malloc_thing(private_kernel_netlink_plugin_t);
this->public.plugin.destroy = (void(*)(plugin_t*))destroy;
charon->kernel_interface->add_ipsec_interface(charon->kernel_interface, (kernel_ipsec_constructor_t)kernel_netlink_ipsec_create);
charon->kernel_interface->add_net_interface(charon->kernel_interface, (kernel_net_constructor_t)kernel_netlink_net_create);
return &this->public.plugin;
}
@@ -0,0 +1,49 @@
/*
* Copyright (C) 2008 Tobias Brunner
* 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 kernel_netlink kernel_netlink
* @ingroup cplugins
*
* @defgroup kernel_netlink_plugin kernel_netlink_plugin
* @{ @ingroup kernel_netlink
*/
#ifndef KERNEL_NETLINK_PLUGIN_H_
#define KERNEL_NETLINK_PLUGIN_H_
#include <plugins/plugin.h>
typedef struct kernel_netlink_plugin_t kernel_netlink_plugin_t;
/**
* netlink kernel interface plugin
*/
struct kernel_netlink_plugin_t {
/**
* implements plugin interface
*/
plugin_t plugin;
};
/**
* Create a kernel_netlink_plugin instance.
*/
plugin_t *plugin_create();
#endif /* KERNEL_NETLINK_PLUGIN_H_ */
@@ -0,0 +1,280 @@
/*
* Copyright (C) 2008 Tobias Brunner
* 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 <sys/socket.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <errno.h>
#include <unistd.h>
#include "kernel_netlink_shared.h"
#include <daemon.h>
typedef struct private_netlink_socket_t private_netlink_socket_t;
/**
* Private variables and functions of netlink_socket_t class.
*/
struct private_netlink_socket_t {
/**
* public part of the netlink_socket_t object.
*/
netlink_socket_t public;
/**
* mutex to lock access to netlink socket
*/
pthread_mutex_t mutex;
/**
* current sequence number for netlink request
*/
int seq;
/**
* netlink socket
*/
int socket;
};
/**
* Implementation of netlink_socket_t.send
*/
static status_t netlink_send(private_netlink_socket_t *this, struct nlmsghdr *in,
struct nlmsghdr **out, size_t *out_len)
{
int len, addr_len;
struct sockaddr_nl addr;
chunk_t result = chunk_empty, tmp;
struct nlmsghdr *msg, peek;
pthread_mutex_lock(&this->mutex);
in->nlmsg_seq = ++this->seq;
in->nlmsg_pid = getpid();
memset(&addr, 0, sizeof(addr));
addr.nl_family = AF_NETLINK;
addr.nl_pid = 0;
addr.nl_groups = 0;
while (TRUE)
{
len = sendto(this->socket, in, in->nlmsg_len, 0,
(struct sockaddr*)&addr, sizeof(addr));
if (len != in->nlmsg_len)
{
if (errno == EINTR)
{
/* interrupted, try again */
continue;
}
pthread_mutex_unlock(&this->mutex);
DBG1(DBG_KNL, "error sending to netlink socket: %s", strerror(errno));
return FAILED;
}
break;
}
while (TRUE)
{
char buf[4096];
tmp.len = sizeof(buf);
tmp.ptr = buf;
msg = (struct nlmsghdr*)tmp.ptr;
memset(&addr, 0, sizeof(addr));
addr.nl_family = AF_NETLINK;
addr.nl_pid = getpid();
addr.nl_groups = 0;
addr_len = sizeof(addr);
len = recvfrom(this->socket, tmp.ptr, tmp.len, 0,
(struct sockaddr*)&addr, &addr_len);
if (len < 0)
{
if (errno == EINTR)
{
DBG1(DBG_KNL, "got interrupted");
/* interrupted, try again */
continue;
}
DBG1(DBG_KNL, "error reading from netlink socket: %s", strerror(errno));
pthread_mutex_unlock(&this->mutex);
free(result.ptr);
return FAILED;
}
if (!NLMSG_OK(msg, len))
{
DBG1(DBG_KNL, "received corrupted netlink message");
pthread_mutex_unlock(&this->mutex);
free(result.ptr);
return FAILED;
}
if (msg->nlmsg_seq != this->seq)
{
DBG1(DBG_KNL, "received invalid netlink sequence number");
if (msg->nlmsg_seq < this->seq)
{
continue;
}
pthread_mutex_unlock(&this->mutex);
free(result.ptr);
return FAILED;
}
tmp.len = len;
result.ptr = realloc(result.ptr, result.len + tmp.len);
memcpy(result.ptr + result.len, tmp.ptr, tmp.len);
result.len += tmp.len;
/* NLM_F_MULTI flag does not seem to be set correctly, we use sequence
* numbers to detect multi header messages */
len = recvfrom(this->socket, &peek, sizeof(peek), MSG_PEEK | MSG_DONTWAIT,
(struct sockaddr*)&addr, &addr_len);
if (len == sizeof(peek) && peek.nlmsg_seq == this->seq)
{
/* seems to be multipart */
continue;
}
break;
}
*out_len = result.len;
*out = (struct nlmsghdr*)result.ptr;
pthread_mutex_unlock(&this->mutex);
return SUCCESS;
}
/**
* Implementation of netlink_socket_t.send_ack.
*/
static status_t netlink_send_ack(private_netlink_socket_t *this, struct nlmsghdr *in)
{
struct nlmsghdr *out, *hdr;
size_t len;
if (netlink_send(this, in, &out, &len) != SUCCESS)
{
return FAILED;
}
hdr = out;
while (NLMSG_OK(hdr, len))
{
switch (hdr->nlmsg_type)
{
case NLMSG_ERROR:
{
struct nlmsgerr* err = (struct nlmsgerr*)NLMSG_DATA(hdr);
if (err->error)
{
if (-err->error == EEXIST)
{ /* do not report existing routes */
free(out);
return ALREADY_DONE;
}
DBG1(DBG_KNL, "received netlink error: %s (%d)",
strerror(-err->error), -err->error);
free(out);
return FAILED;
}
free(out);
return SUCCESS;
}
default:
hdr = NLMSG_NEXT(hdr, len);
continue;
case NLMSG_DONE:
break;
}
break;
}
DBG1(DBG_KNL, "netlink request not acknowledged");
free(out);
return FAILED;
}
/**
* Implementation of netlink_socket_t.destroy.
*/
static void destroy(private_netlink_socket_t *this)
{
close(this->socket);
free(this);
}
/**
* Described in header.
*/
netlink_socket_t *netlink_socket_create(int protocol) {
private_netlink_socket_t *this = malloc_thing(private_netlink_socket_t);
struct sockaddr_nl addr;
/* public functions */
this->public.send = (status_t(*)(netlink_socket_t*,struct nlmsghdr*, struct nlmsghdr**, size_t*))netlink_send;
this->public.send_ack = (status_t(*)(netlink_socket_t*,struct nlmsghdr*))netlink_send_ack;
this->public.destroy = (void(*)(netlink_socket_t*))destroy;
/* private members */
this->seq = 200;
pthread_mutex_init(&this->mutex, NULL);
memset(&addr, 0, sizeof(addr));
addr.nl_family = AF_NETLINK;
this->socket = socket(AF_NETLINK, SOCK_RAW, protocol);
if (this->socket <= 0)
{
charon->kill(charon, "unable to create netlink socket");
}
addr.nl_groups = 0;
if (bind(this->socket, (struct sockaddr*)&addr, sizeof(addr)))
{
charon->kill(charon, "unable to bind netlink socket");
}
return &this->public;
}
/**
* Described in header.
*/
void netlink_add_attribute(struct nlmsghdr *hdr, int rta_type, chunk_t data,
size_t buflen)
{
struct rtattr *rta;
if (NLMSG_ALIGN(hdr->nlmsg_len) + RTA_ALIGN(data.len) > buflen)
{
DBG1(DBG_KNL, "unable to add attribute, buffer too small");
return;
}
rta = (struct rtattr*)(((char*)hdr) + NLMSG_ALIGN(hdr->nlmsg_len));
rta->rta_type = rta_type;
rta->rta_len = RTA_LENGTH(data.len);
memcpy(RTA_DATA(rta), data.ptr, data.len);
hdr->nlmsg_len = NLMSG_ALIGN(hdr->nlmsg_len) + rta->rta_len;
}
@@ -0,0 +1,71 @@
/*
* Copyright (C) 2008 Tobias Brunner
* 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$
*/
#ifndef KERNEL_NETLINK_SHARED_H_
#define KERNEL_NETLINK_SHARED_H_
#include <library.h>
#define NETLINK_BUFFER_SIZE 1024
typedef struct netlink_socket_t netlink_socket_t;
/**
* Wrapper around a netlink socket.
*/
struct netlink_socket_t {
/**
* Send a netlink message and wait for a reply.
*
* @param in netlink message to send
* @param out received netlink message
* @param out_len length of the received message
*/
status_t (*send)(netlink_socket_t *this, struct nlmsghdr *in, struct nlmsghdr **out, size_t *out_len);
/**
* Send a netlink message and wait for its acknowledge.
*
* @param in netlink message to send
*/
status_t (*send_ack)(netlink_socket_t *this, struct nlmsghdr *in);
/**
* Destroy the socket.
*/
void (*destroy)(netlink_socket_t *this);
};
/**
* Create a netlink_socket_t object.
*
* @param protocol protocol type (e.g. NETLINK_XFRM or NETLINK_ROUTE)
*/
netlink_socket_t *netlink_socket_create(int protocol);
/**
* Creates an rtattr and adds it to the given netlink message.
*
* @param hdr netlink message
* @param rta_type type of the rtattr
* @param data data to add to the rtattr
* @param buflen length of the netlink message buffer
*/
void netlink_add_attribute(struct nlmsghdr *hdr, int rta_type, chunk_t data, size_t buflen);
#endif /* KERNEL_NETLINK_SHARED_H_ */
+4 -5
View File
@@ -227,7 +227,6 @@ static void log_child_sa(FILE *out, child_sa_t *child_sa, bool all)
static void status(private_stroke_list_t *this, stroke_msg_t *msg, FILE *out, bool all)
{
enumerator_t *enumerator, *children;
iterator_t *iterator;
ike_cfg_t *ike_cfg;
child_cfg_t *child_cfg;
ike_sa_t *ike_sa;
@@ -260,14 +259,14 @@ static void status(private_stroke_list_t *this, stroke_msg_t *msg, FILE *out, bo
enumerator->destroy(enumerator);
fprintf(out, "\n");
iterator = charon->kernel_interface->create_address_iterator(
charon->kernel_interface);
enumerator = charon->kernel_interface->create_address_enumerator(
charon->kernel_interface, FALSE, FALSE);
fprintf(out, "Listening IP addresses:\n");
while (iterator->iterate(iterator, (void**)&host))
while (enumerator->enumerate(enumerator, (void**)&host))
{
fprintf(out, " %H\n", host);
}
iterator->destroy(iterator);
enumerator->destroy(enumerator);
fprintf(out, "Connections:\n");
enumerator = charon->backends->create_peer_cfg_enumerator(charon->backends);