|
Packit |
c22fc9 |
/*
|
|
Packit |
c22fc9 |
* libnl_link: Handle dynamic linking to netlink libraries
|
|
Packit |
c22fc9 |
*
|
|
Packit |
c22fc9 |
* Authors: P. Quentin Armitage <Quentin@Armitage.org.uk>
|
|
Packit |
c22fc9 |
*
|
|
Packit |
c22fc9 |
* This program is free software; you can redistribute it and/or
|
|
Packit |
c22fc9 |
* modify it under the terms of the GNU General Public License
|
|
Packit |
c22fc9 |
* as published by the Free Software Foundation; either version
|
|
Packit |
c22fc9 |
* 2 of the License, or (at your option) any later version.
|
|
Packit |
c22fc9 |
*
|
|
Packit |
c22fc9 |
* Copyright (C) 2017-2017 Alexandre Cassen, <acassen@gmail.com>
|
|
Packit |
c22fc9 |
*/
|
|
Packit |
c22fc9 |
|
|
Packit |
c22fc9 |
#include "config.h"
|
|
Packit |
c22fc9 |
|
|
Packit |
c22fc9 |
#ifdef _LIBNL_DYNAMIC_
|
|
Packit |
c22fc9 |
|
|
Packit |
c22fc9 |
#include <stdbool.h>
|
|
Packit |
c22fc9 |
|
|
Packit |
c22fc9 |
#include <dlfcn.h>
|
|
Packit |
c22fc9 |
#include <netlink/netlink.h>
|
|
Packit |
c22fc9 |
#include <netlink/socket.h>
|
|
Packit |
c22fc9 |
|
|
Packit |
c22fc9 |
#ifdef LIBIPVS_USE_NL
|
|
Packit |
c22fc9 |
#include <netlink/genl/genl.h>
|
|
Packit |
c22fc9 |
#include <netlink/genl/ctrl.h>
|
|
Packit |
c22fc9 |
#endif
|
|
Packit |
c22fc9 |
|
|
Packit |
c22fc9 |
#include "libnl_link.h"
|
|
Packit |
c22fc9 |
#include "logger.h"
|
|
Packit |
c22fc9 |
|
|
Packit |
c22fc9 |
|
|
Packit |
c22fc9 |
/* The addresses of the functions we want */
|
|
Packit |
c22fc9 |
struct nl_sock * (*nl_socket_alloc_addr)(void);
|
|
Packit |
c22fc9 |
void (*nl_socket_free_addr)(struct nl_sock *);
|
|
Packit |
c22fc9 |
#ifdef LIBIPVS_USE_NL
|
|
Packit |
c22fc9 |
int (*genl_connect_addr)(struct nl_sock *);
|
|
Packit |
c22fc9 |
int (*genl_ctrl_resolve_addr)(struct nl_sock *, const char *);
|
|
Packit |
c22fc9 |
int (*genlmsg_parse_addr)(struct nlmsghdr *, int, struct nlattr **, int, struct nla_policy *);
|
|
Packit |
c22fc9 |
void * (*genlmsg_put_addr)(struct nl_msg *, uint32_t, uint32_t, int, int, int, uint8_t, uint8_t);
|
|
Packit |
c22fc9 |
int (*nla_nest_end_addr)(struct nl_msg *, struct nlattr *);
|
|
Packit |
c22fc9 |
struct nlattr * (*nla_nest_start_addr)(struct nl_msg *, int);
|
|
Packit |
c22fc9 |
int (*nla_put_daddr)(struct nl_msg *, int, int, const void *);
|
|
Packit |
c22fc9 |
struct nl_msg * (*nlmsg_alloc_addr)(void);
|
|
Packit |
c22fc9 |
void (*nlmsg_free_addr)(struct nl_msg *);
|
|
Packit |
c22fc9 |
struct nlmsghdr * (*nlmsg_hdr_addr)(struct nl_msg *);
|
|
Packit |
c22fc9 |
int (*nl_recvmsgs_default_addr)(struct nl_sock *);
|
|
Packit |
c22fc9 |
int (*nl_send_auto_complete_addr)(struct nl_sock *, struct nl_msg *);
|
|
Packit |
c22fc9 |
int (*nl_socket_modify_cb_addr)(struct nl_sock *, enum nl_cb_type, enum nl_cb_kind, nl_recvmsg_msg_cb_t, void *);
|
|
Packit |
c22fc9 |
#ifdef _HAVE_LIBNL3_
|
|
Packit |
c22fc9 |
void * (*nla_data_addr)(const struct nlattr *);
|
|
Packit |
c22fc9 |
int32_t (*nla_get_s32_addr)(const struct nlattr *);
|
|
Packit |
c22fc9 |
char * (*nla_get_string_addr)(const struct nlattr *);
|
|
Packit |
c22fc9 |
uint16_t (*nla_get_u16_addr)(const struct nlattr *);
|
|
Packit |
c22fc9 |
uint32_t (*nla_get_u32_addr)(const struct nlattr *);
|
|
Packit |
c22fc9 |
uint64_t (*nla_get_u64_addr)(const struct nlattr *);
|
|
Packit |
c22fc9 |
int (*nla_memcpy_addr)(void *, const struct nlattr *, int);
|
|
Packit |
c22fc9 |
int (*nla_parse_nested_addr)(struct nlattr **, int, struct nlattr *, struct nla_policy *);
|
|
Packit |
c22fc9 |
#endif
|
|
Packit |
c22fc9 |
#endif
|
|
Packit |
c22fc9 |
|
|
Packit |
c22fc9 |
|
|
Packit |
c22fc9 |
static void* libnl_handle;
|
|
Packit |
c22fc9 |
#ifdef LIBIPVS_USE_NL
|
|
Packit |
c22fc9 |
static void* libnl_genl_handle;
|
|
Packit |
c22fc9 |
#endif
|
|
Packit |
c22fc9 |
|
|
Packit |
c22fc9 |
bool
|
|
Packit |
c22fc9 |
libnl_init(void)
|
|
Packit |
c22fc9 |
{
|
|
Packit |
c22fc9 |
if (libnl_handle)
|
|
Packit |
c22fc9 |
return true;
|
|
Packit |
c22fc9 |
|
|
Packit |
c22fc9 |
/* Attempt to open the necessary libraries */
|
|
Packit |
c22fc9 |
#ifdef _HAVE_LIBNL1_
|
|
Packit |
c22fc9 |
#ifdef _WITH_LVS_
|
|
Packit |
c22fc9 |
if (!(libnl_handle = dlopen("libnl.so", RTLD_NOW)) &&
|
|
Packit |
c22fc9 |
!(libnl_handle = dlopen(NL_LIB_NAME, RTLD_NOW))) {
|
|
Packit |
c22fc9 |
log_message(LOG_INFO, "Unable to load nl library - %s", dlerror());
|
|
Packit |
c22fc9 |
return false;
|
|
Packit |
c22fc9 |
}
|
|
Packit |
c22fc9 |
libnl_genl_handle = libnl_handle;
|
|
Packit |
c22fc9 |
#endif
|
|
Packit |
c22fc9 |
#else
|
|
Packit |
c22fc9 |
if (!(libnl_handle = dlopen("libnl-3.so", RTLD_NOW)) &&
|
|
Packit |
c22fc9 |
!(libnl_handle = dlopen(NL3_LIB_NAME, RTLD_NOW))) {
|
|
Packit |
c22fc9 |
log_message(LOG_INFO, "Unable to load nl-3 library - %s", dlerror());
|
|
Packit |
c22fc9 |
return false;
|
|
Packit |
c22fc9 |
}
|
|
Packit |
c22fc9 |
#ifdef _WITH_LVS_
|
|
Packit |
c22fc9 |
if (!(libnl_genl_handle = dlopen("libnl-genl-3.so", RTLD_NOW)) &&
|
|
Packit |
c22fc9 |
!(libnl_genl_handle = dlopen(NL3_GENL_LIB_NAME, RTLD_NOW))) {
|
|
Packit |
c22fc9 |
log_message(LOG_INFO, "Unable to load nl-genl-3 library - %s", dlerror());
|
|
Packit |
c22fc9 |
return false;
|
|
Packit |
c22fc9 |
}
|
|
Packit |
c22fc9 |
#endif
|
|
Packit |
c22fc9 |
#endif
|
|
Packit |
c22fc9 |
|
|
Packit |
c22fc9 |
if (
|
|
Packit |
c22fc9 |
#ifdef _HAVE_LIBNL1_
|
|
Packit |
c22fc9 |
!(nl_socket_alloc_addr = dlsym(libnl_handle, "nl_handle_alloc")) ||
|
|
Packit |
c22fc9 |
!(nl_socket_free_addr = dlsym(libnl_handle, "nl_handle_destroy")) ||
|
|
Packit |
c22fc9 |
#else
|
|
Packit |
c22fc9 |
!(nl_socket_alloc_addr = dlsym(libnl_handle, "nl_socket_alloc")) ||
|
|
Packit |
c22fc9 |
!(nl_socket_free_addr = dlsym(libnl_handle, "nl_socket_free")) ||
|
|
Packit |
c22fc9 |
#endif
|
|
Packit |
c22fc9 |
#ifdef _WITH_LVS_
|
|
Packit |
c22fc9 |
!(genl_connect_addr = dlsym(libnl_genl_handle, "genl_connect")) ||
|
|
Packit |
c22fc9 |
!(genl_ctrl_resolve_addr = dlsym(libnl_genl_handle, "genl_ctrl_resolve")) ||
|
|
Packit |
c22fc9 |
!(genlmsg_parse_addr = dlsym(libnl_genl_handle, "genlmsg_parse")) ||
|
|
Packit |
c22fc9 |
!(genlmsg_put_addr = dlsym(libnl_genl_handle, "genlmsg_put")) ||
|
|
Packit |
c22fc9 |
!(nla_nest_end_addr = dlsym(libnl_handle, "nla_nest_end")) ||
|
|
Packit |
c22fc9 |
!(nla_nest_start_addr = dlsym(libnl_handle, "nla_nest_start")) ||
|
|
Packit |
c22fc9 |
!(nla_put_daddr = dlsym(libnl_handle, "nla_put")) ||
|
|
Packit |
c22fc9 |
!(nlmsg_alloc_addr = dlsym(libnl_handle, "nlmsg_alloc")) ||
|
|
Packit |
c22fc9 |
!(nlmsg_free_addr = dlsym(libnl_handle, "nlmsg_free")) ||
|
|
Packit |
c22fc9 |
!(nlmsg_hdr_addr = dlsym(libnl_handle, "nlmsg_hdr")) ||
|
|
Packit |
c22fc9 |
!(nl_recvmsgs_default_addr = dlsym(libnl_handle, "nl_recvmsgs_default")) ||
|
|
Packit |
c22fc9 |
!(nl_send_auto_complete_addr = dlsym(libnl_handle, "nl_send_auto_complete")) ||
|
|
Packit |
c22fc9 |
!(nl_socket_modify_cb_addr = dlsym(libnl_handle, "nl_socket_modify_cb")) ||
|
|
Packit |
c22fc9 |
#ifdef _HAVE_LIBNL3_
|
|
Packit |
c22fc9 |
!(nla_data_addr = dlsym(libnl_handle, "nla_data")) ||
|
|
Packit |
c22fc9 |
!(nla_get_s32_addr = dlsym(libnl_handle, "nla_get_s32")) ||
|
|
Packit |
c22fc9 |
!(nla_get_string_addr = dlsym(libnl_handle, "nla_get_string")) ||
|
|
Packit |
c22fc9 |
!(nla_get_u16_addr = dlsym(libnl_handle, "nla_get_u16")) ||
|
|
Packit |
c22fc9 |
!(nla_get_u32_addr = dlsym(libnl_handle, "nla_get_u32")) ||
|
|
Packit |
c22fc9 |
!(nla_get_u64_addr = dlsym(libnl_handle, "nla_get_u64")) ||
|
|
Packit |
c22fc9 |
!(nla_memcpy_addr = dlsym(libnl_handle, "nla_memcpy")) ||
|
|
Packit |
c22fc9 |
!(nla_parse_nested_addr = dlsym(libnl_handle, "nla_parse_nested")) ||
|
|
Packit |
c22fc9 |
#endif
|
|
Packit |
c22fc9 |
#endif
|
|
Packit |
c22fc9 |
false)
|
|
Packit |
c22fc9 |
log_message(LOG_INFO, "Failed to dynamic link a libnli/libnl-3 function");
|
|
Packit |
c22fc9 |
|
|
Packit |
c22fc9 |
return true;
|
|
Packit |
c22fc9 |
}
|
|
Packit |
c22fc9 |
#endif
|