From b9ce51c5fd9773694856802a175efdd9b37a2242 Mon Sep 17 00:00:00 2001 From: Harald Welte Date: Wed, 30 Jun 2010 19:43:11 +0200 Subject: Add support for plugins (and specifically GPRS encryption plugins) --- src/Makefile.am | 8 ++++- src/gprs_cipher_core.c | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/plugin.c | 62 ++++++++++++++++++++++++++++++++++++ 3 files changed, 154 insertions(+), 1 deletion(-) create mode 100644 src/gprs_cipher_core.c create mode 100644 src/plugin.c (limited to 'src') diff --git a/src/Makefile.am b/src/Makefile.am index dc2309a0..a7d450b1 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -12,7 +12,13 @@ lib_LTLIBRARIES = libosmocore.la libosmocore_la_SOURCES = timer.c select.c signal.c msgb.c rxlev_stat.c \ tlv_parser.c bitvec.c comp128.c gsm_utils.c statistics.c \ write_queue.c utils.c rsl.c gsm48.c gsm48_ie.c \ - logging.c gsm0808.c rate_ctr.c gsmtap_util.c + logging.c gsm0808.c rate_ctr.c gsmtap_util.c \ + gprs_cipher_core.c + +if ENABLE_PLUGIN +libosmocore_la_SOURCES += plugin.c +libosmocore_la_LDFLAGS = -ldl +endif if ENABLE_TALLOC libosmocore_la_SOURCES += talloc.c diff --git a/src/gprs_cipher_core.c b/src/gprs_cipher_core.c new file mode 100644 index 00000000..a72dda84 --- /dev/null +++ b/src/gprs_cipher_core.c @@ -0,0 +1,85 @@ +/* GPRS LLC cipher core infrastructure */ + +/* (C) 2010 by Harald Welte + * + * All Rights Reserved + * + * 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. + * + * 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. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + */ + +#include +#include + +#include +#include +#include + +#include + +static LLIST_HEAD(gprs_ciphers); + +static struct gprs_cipher_impl *selected_ciphers[_GPRS_ALGO_NUM]; + +/* register a cipher with the core */ +int gprs_cipher_register(struct gprs_cipher_impl *ciph) +{ + if (ciph->algo > ARRAY_SIZE(selected_ciphers)) + return -ERANGE; + + llist_add_tail(&ciph->list, &gprs_ciphers); + + /* check if we want to select this implementation over others */ + if (!selected_ciphers[ciph->algo] || + (selected_ciphers[ciph->algo]->priority > ciph->priority)) + selected_ciphers[ciph->algo] = ciph; + + return 0; +} + +/* load all available GPRS cipher plugins */ +int gprs_cipher_load(const char *path) +{ + /* load all plugins available from path */ + return plugin_load_all(path); +} + +/* function to be called by core code */ +int gprs_cipher_run(uint8_t *out, uint16_t len, enum gprs_ciph_algo algo, + uint64_t kc, uint32_t iv, enum gprs_cipher_direction dir) +{ + if (algo > ARRAY_SIZE(selected_ciphers)) + return -ERANGE; + + if (!selected_ciphers[algo]) + return -EINVAL; + + if (len > GSM0464_CIPH_MAX_BLOCK) + return -ERANGE; + + /* run the actual cipher from the plugin */ + return selected_ciphers[algo]->run(out, len, kc, iv, dir); +} + +int gprs_cipher_supported(enum gprs_ciph_algo algo) +{ + if (algo > ARRAY_SIZE(selected_ciphers)) + return -ERANGE; + + if (selected_ciphers[algo]) + return 1; + + return 0; +} diff --git a/src/plugin.c b/src/plugin.c new file mode 100644 index 00000000..e953508a --- /dev/null +++ b/src/plugin.c @@ -0,0 +1,62 @@ +/* plugin infrastructure */ + +/* (C) 2010 by Harald Welte + * + * All Rights Reserved + * + * 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. + * + * 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. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + */ + +#include "../config.h" + +#if HAVE_DLFCN_H + +#include +#include +#include +#include +#include + +#include + +int plugin_load_all(const char *directory) +{ + unsigned int num = 0; + char fname[PATH_MAX]; + DIR *dir; + struct dirent *entry; + + dir = opendir(directory); + if (!dir) + return -errno; + + while ((entry = readdir(dir))) { + snprintf(fname, sizeof(fname), "%s/%s", directory, + entry->d_name); + if (dlopen(fname, RTLD_NOW)) + num++; + } + + closedir(dir); + + return num; +} +#else +int plugin_load_all(const char *directory) +{ + return 0; +} +#endif /* HAVE_DLFCN_H */ -- cgit v1.2.3