libnl-tiny: move source code into separate Git repository
In order to make the source code usable and testable separately out of buildroot. Acked-by: Hauke Mehrtens <hauke@hauke-m.de> Acked-by: Jo-Philipp Wich <jo@mein.io> Acked-by: Hans Dedecker <dedeckeh@gmail.com> Signed-off-by: Petr Štetiar <ynezz@true.cz>
This commit is contained in:
parent
700e7a2eb9
commit
a92f74ba8d
@ -8,8 +8,13 @@
|
||||
include $(TOPDIR)/rules.mk
|
||||
|
||||
PKG_NAME:=libnl-tiny
|
||||
PKG_VERSION:=0.1
|
||||
PKG_RELEASE:=5
|
||||
PKG_RELEASE:=1
|
||||
|
||||
PKG_SOURCE_PROTO:=git
|
||||
PKG_SOURCE_URL=$(PROJECT_GIT)/project/libnl-tiny.git
|
||||
PKG_SOURCE_DATE:=2017-11-16
|
||||
PKG_SOURCE_VERSION:=8ee5f3a4023f822d1c916bb2b5e845d3597ac674
|
||||
PKG_MIRROR_HASH:=e3dd137d69848b8cf2dbc58d70452586dd4178b55977896ea3a9dedaf62c768c
|
||||
|
||||
PKG_LICENSE:=LGPL-2.1
|
||||
PKG_MAINTAINER:=Felix Fietkau <nbd@nbd.name>
|
||||
@ -29,7 +34,7 @@ endef
|
||||
TARGET_CFLAGS += $(FPIC)
|
||||
|
||||
define Build/Compile
|
||||
$(MAKE) -C $(PKG_BUILD_DIR) \
|
||||
$(MAKE) -C $(PKG_BUILD_DIR)/src \
|
||||
$(TARGET_CONFIGURE_OPTS) \
|
||||
CFLAGS="$(TARGET_CFLAGS)" \
|
||||
all
|
||||
@ -37,14 +42,14 @@ endef
|
||||
|
||||
define Build/InstallDev
|
||||
$(INSTALL_DIR) $(1)/usr/lib/pkgconfig $(1)/usr/include/libnl-tiny
|
||||
$(CP) $(PKG_BUILD_DIR)/include/* $(1)/usr/include/libnl-tiny
|
||||
$(CP) $(PKG_BUILD_DIR)/libnl-tiny.so $(1)/usr/lib/
|
||||
$(CP) ./files/libnl-tiny.pc $(1)/usr/lib/pkgconfig
|
||||
$(CP) $(PKG_BUILD_DIR)/src/include/* $(1)/usr/include/libnl-tiny
|
||||
$(CP) $(PKG_BUILD_DIR)/src/libnl-tiny.so $(1)/usr/lib/
|
||||
$(CP) $(PKG_BUILD_DIR)/files/libnl-tiny.pc $(1)/usr/lib/pkgconfig
|
||||
endef
|
||||
|
||||
define Package/libnl-tiny/install
|
||||
$(INSTALL_DIR) $(1)/usr/lib
|
||||
$(CP) $(PKG_BUILD_DIR)/libnl-tiny.so $(1)/usr/lib/
|
||||
$(CP) $(PKG_BUILD_DIR)/src/libnl-tiny.so $(1)/usr/lib/
|
||||
endef
|
||||
|
||||
$(eval $(call BuildPackage,libnl-tiny))
|
||||
|
||||
@ -1,10 +0,0 @@
|
||||
prefix=/usr
|
||||
exec_prefix=/usr
|
||||
libdir=${exec_prefix}/lib
|
||||
includedir=${prefix}/include/libnl-tiny
|
||||
|
||||
Name: libnl-tiny
|
||||
Description: Convenience library for netlink sockets
|
||||
Version: 2.0
|
||||
Libs: -L${libdir} -lnl-tiny
|
||||
Cflags: -I${includedir}
|
||||
@ -1,17 +0,0 @@
|
||||
CC=gcc
|
||||
WFLAGS=-Wall
|
||||
CFLAGS=-O2
|
||||
INCLUDES=-Iinclude
|
||||
|
||||
LIBNAME=libnl-tiny.so
|
||||
|
||||
all: $(LIBNAME)
|
||||
|
||||
%.o: %.c
|
||||
$(CC) $(WFLAGS) -c -o $@ $(INCLUDES) $(CFLAGS) $<
|
||||
|
||||
LIBNL_OBJ=nl.o handlers.o msg.o attr.o cache.o cache_mngt.o object.o socket.o error.o
|
||||
GENL_OBJ=genl.o genl_family.o genl_ctrl.o genl_mngt.o unl.o
|
||||
|
||||
$(LIBNAME): $(LIBNL_OBJ) $(GENL_OBJ)
|
||||
$(CC) $(CFLAGS) -Wl,-Bsymbolic-functions -shared -o $@ $^
|
||||
@ -1,668 +0,0 @@
|
||||
/*
|
||||
* lib/attr.c Netlink Attributes
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation version 2.1
|
||||
* of the License.
|
||||
*
|
||||
* Copyright (c) 2003-2008 Thomas Graf <tgraf@suug.ch>
|
||||
*/
|
||||
|
||||
#include <netlink-local.h>
|
||||
#include <netlink/netlink.h>
|
||||
#include <netlink/utils.h>
|
||||
#include <netlink/addr.h>
|
||||
#include <netlink/attr.h>
|
||||
#include <netlink/msg.h>
|
||||
#include <linux/socket.h>
|
||||
|
||||
/**
|
||||
* @ingroup msg
|
||||
* @defgroup attr Attributes
|
||||
* Netlink Attributes Construction/Parsing Interface
|
||||
*
|
||||
* \section attr_sec Netlink Attributes
|
||||
* Netlink attributes allow for data chunks of arbitary length to be
|
||||
* attached to a netlink message. Each attribute is encoded with a
|
||||
* type and length field, both 16 bits, stored in the attribute header
|
||||
* preceding the attribute data. The main advantage of using attributes
|
||||
* over packing everything into the family header is that the interface
|
||||
* stays extendable as new attributes can supersede old attributes while
|
||||
* remaining backwards compatible. Also attributes can be defined optional
|
||||
* thus avoiding the transmission of unnecessary empty data blocks.
|
||||
* Special nested attributes allow for more complex data structures to
|
||||
* be transmitted, e.g. trees, lists, etc.
|
||||
*
|
||||
* While not required, netlink attributes typically follow the family
|
||||
* header of a netlink message and must be properly aligned to NLA_ALIGNTO:
|
||||
* @code
|
||||
* +----------------+- - -+---------------+- - -+------------+- - -+
|
||||
* | Netlink Header | Pad | Family Header | Pad | Attributes | Pad |
|
||||
* +----------------+- - -+---------------+- - -+------------+- - -+
|
||||
* @endcode
|
||||
*
|
||||
* The actual attributes are chained together each separately aligned to
|
||||
* NLA_ALIGNTO. The position of an attribute is defined based on the
|
||||
* length field of the preceding attributes:
|
||||
* @code
|
||||
* +-------------+- - -+-------------+- - -+------
|
||||
* | Attribute 1 | Pad | Attribute 2 | Pad | ...
|
||||
* +-------------+- - -+-------------+- - -+------
|
||||
* nla_next(attr1)------^
|
||||
* @endcode
|
||||
*
|
||||
* The attribute itself consists of the attribute header followed by
|
||||
* the actual payload also aligned to NLA_ALIGNTO. The function nla_data()
|
||||
* returns a pointer to the start of the payload while nla_len() returns
|
||||
* the length of the payload in bytes.
|
||||
*
|
||||
* \b Note: Be aware, NLA_ALIGNTO equals to 4 bytes, therefore it is not
|
||||
* safe to dereference any 64 bit data types directly.
|
||||
*
|
||||
* @code
|
||||
* <----------- nla_total_size(payload) ----------->
|
||||
* <-------- nla_attr_size(payload) --------->
|
||||
* +------------------+- - -+- - - - - - - - - +- - -+
|
||||
* | Attribute Header | Pad | Payload | Pad |
|
||||
* +------------------+- - -+- - - - - - - - - +- - -+
|
||||
* nla_data(nla)-------------^
|
||||
* <- nla_len(nla) ->
|
||||
* @endcode
|
||||
*
|
||||
* @subsection attr_datatypes Attribute Data Types
|
||||
* A number of basic data types are supported to simplify access and
|
||||
* validation of netlink attributes. This data type information is
|
||||
* not encoded in the attribute, both the kernel and userspace part
|
||||
* are required to share this information on their own.
|
||||
*
|
||||
* One of the major advantages of these basic types is the automatic
|
||||
* validation of each attribute based on an attribute policy. The
|
||||
* validation covers most of the checks required to safely use
|
||||
* attributes and thus keeps the individual sanity check to a minimum.
|
||||
*
|
||||
* Never access attribute payload without ensuring basic validation
|
||||
* first, attributes may:
|
||||
* - not be present even though required
|
||||
* - contain less actual payload than expected
|
||||
* - fake a attribute length which exceeds the end of the message
|
||||
* - contain unterminated character strings
|
||||
*
|
||||
* Policies are defined as array of the struct nla_policy. The array is
|
||||
* indexed with the attribute type, therefore the array must be sized
|
||||
* accordingly.
|
||||
* @code
|
||||
* static struct nla_policy my_policy[ATTR_MAX+1] = {
|
||||
* [ATTR_FOO] = { .type = ..., .minlen = ..., .maxlen = ... },
|
||||
* };
|
||||
*
|
||||
* err = nla_validate(attrs, attrlen, ATTR_MAX, &my_policy);
|
||||
* @endcode
|
||||
*
|
||||
* Some basic validations are performed on every attribute, regardless of type.
|
||||
* - If the attribute type exceeds the maximum attribute type specified or
|
||||
* the attribute type is lesser-or-equal than zero, the attribute will
|
||||
* be silently ignored.
|
||||
* - If the payload length falls below the \a minlen value the attribute
|
||||
* will be rejected.
|
||||
* - If \a maxlen is non-zero and the payload length exceeds the \a maxlen
|
||||
* value the attribute will be rejected.
|
||||
*
|
||||
*
|
||||
* @par Unspecific Attribute (NLA_UNSPEC)
|
||||
* This is the standard type if no type is specified. It is used for
|
||||
* binary data of arbitary length. Typically this attribute carries
|
||||
* a binary structure or a stream of bytes.
|
||||
* @par
|
||||
* @code
|
||||
* // In this example, we will assume a binary structure requires to
|
||||
* // be transmitted. The definition of the structure will typically
|
||||
* // go into a header file available to both the kernel and userspace
|
||||
* // side.
|
||||
* //
|
||||
* // Note: Be careful when putting 64 bit data types into a structure.
|
||||
* // The attribute payload is only aligned to 4 bytes, dereferencing
|
||||
* // the member may fail.
|
||||
* struct my_struct {
|
||||
* int a;
|
||||
* int b;
|
||||
* };
|
||||
*
|
||||
* // The validation function will not enforce an exact length match to
|
||||
* // allow structures to grow as required. Note: While it is allowed
|
||||
* // to add members to the end of the structure, changing the order or
|
||||
* // inserting members in the middle of the structure will break your
|
||||
* // binary interface.
|
||||
* static struct nla_policy my_policy[ATTR_MAX+1] = {
|
||||
* [ATTR_MY_STRICT] = { .type = NLA_UNSPEC,
|
||||
* .minlen = sizeof(struct my_struct) },
|
||||
*
|
||||
* // The binary structure is appened to the message using nla_put()
|
||||
* struct my_struct foo = { .a = 1, .b = 2 };
|
||||
* nla_put(msg, ATTR_MY_STRUCT, sizeof(foo), &foo);
|
||||
*
|
||||
* // On the receiving side, a pointer to the structure pointing inside
|
||||
* // the message payload is returned by nla_get().
|
||||
* if (attrs[ATTR_MY_STRUCT])
|
||||
* struct my_struct *foo = nla_get(attrs[ATTR_MY_STRUCT]);
|
||||
* @endcode
|
||||
*
|
||||
* @par Integers (NLA_U8, NLA_U16, NLA_U32, NLA_U64)
|
||||
* Integers come in different sizes from 8 bit to 64 bit. However, since the
|
||||
* payload length is aligned to 4 bytes, integers smaller than 32 bit are
|
||||
* only useful to enforce the maximum range of values.
|
||||
* @par
|
||||
* \b Note: There is no difference made between signed and unsigned integers.
|
||||
* The validation only enforces the minimal payload length required to store
|
||||
* an integer of specified type.
|
||||
* @par
|
||||
* @code
|
||||
* // Even though possible, it does not make sense to specify .minlen or
|
||||
* // .maxlen for integer types. The data types implies the corresponding
|
||||
* // minimal payload length.
|
||||
* static struct nla_policy my_policy[ATTR_MAX+1] = {
|
||||
* [ATTR_FOO] = { .type = NLA_U32 },
|
||||
*
|
||||
* // Numeric values can be appended directly using the respective
|
||||
* // nla_put_uxxx() function
|
||||
* nla_put_u32(msg, ATTR_FOO, 123);
|
||||
*
|
||||
* // Same for the receiving side.
|
||||
* if (attrs[ATTR_FOO])
|
||||
* uint32_t foo = nla_get_u32(attrs[ATTR_FOO]);
|
||||
* @endcode
|
||||
*
|
||||
* @par Character string (NLA_STRING)
|
||||
* This data type represents a NUL terminated character string of variable
|
||||
* length. For binary data streams the type NLA_UNSPEC is recommended.
|
||||
* @par
|
||||
* @code
|
||||
* // Enforce a NUL terminated character string of at most 4 characters
|
||||
* // including the NUL termination.
|
||||
* static struct nla_policy my_policy[ATTR_MAX+1] = {
|
||||
* [ATTR_BAR] = { .type = NLA_STRING, maxlen = 4 },
|
||||
*
|
||||
* // nla_put_string() creates a string attribute of the necessary length
|
||||
* // and appends it to the message including the NUL termination.
|
||||
* nla_put_string(msg, ATTR_BAR, "some text");
|
||||
*
|
||||
* // It is safe to use the returned character string directly if the
|
||||
* // attribute has been validated as the validation enforces the proper
|
||||
* // termination of the string.
|
||||
* if (attrs[ATTR_BAR])
|
||||
* char *text = nla_get_string(attrs[ATTR_BAR]);
|
||||
* @endcode
|
||||
*
|
||||
* @par Flag (NLA_FLAG)
|
||||
* This attribute type may be used to indicate the presence of a flag. The
|
||||
* attribute is only valid if the payload length is zero. The presence of
|
||||
* the attribute header indicates the presence of the flag.
|
||||
* @par
|
||||
* @code
|
||||
* // This attribute type is special as .minlen and .maxlen have no effect.
|
||||
* static struct nla_policy my_policy[ATTR_MAX+1] = {
|
||||
* [ATTR_FLAG] = { .type = NLA_FLAG },
|
||||
*
|
||||
* // nla_put_flag() appends a zero sized attribute to the message.
|
||||
* nla_put_flag(msg, ATTR_FLAG);
|
||||
*
|
||||
* // There is no need for a receival function, the presence is the value.
|
||||
* if (attrs[ATTR_FLAG])
|
||||
* // flag is present
|
||||
* @endcode
|
||||
*
|
||||
* @par Micro Seconds (NLA_MSECS)
|
||||
*
|
||||
* @par Nested Attribute (NLA_NESTED)
|
||||
* Attributes can be nested and put into a container to create groups, lists
|
||||
* or to construct trees of attributes. Nested attributes are often used to
|
||||
* pass attributes to a subsystem where the top layer has no knowledge of the
|
||||
* configuration possibilities of each subsystem.
|
||||
* @par
|
||||
* \b Note: When validating the attributes using nlmsg_validate() or
|
||||
* nlmsg_parse() it will only affect the top level attributes. Each
|
||||
* level of nested attributes must be validated seperately using
|
||||
* nla_parse_nested() or nla_validate().
|
||||
* @par
|
||||
* @code
|
||||
* // The minimal length policy may be used to enforce the presence of at
|
||||
* // least one attribute.
|
||||
* static struct nla_policy my_policy[ATTR_MAX+1] = {
|
||||
* [ATTR_OPTS] = { .type = NLA_NESTED, minlen = NLA_HDRLEN },
|
||||
*
|
||||
* // Nested attributes are constructed by enclosing the attributes
|
||||
* // to be nested with calls to nla_nest_start() respetively nla_nest_end().
|
||||
* struct nlattr *opts = nla_nest_start(msg, ATTR_OPTS);
|
||||
* nla_put_u32(msg, ATTR_FOO, 123);
|
||||
* nla_put_string(msg, ATTR_BAR, "some text");
|
||||
* nla_nest_end(msg, opts);
|
||||
*
|
||||
* // Various methods exist to parse nested attributes, the easiest being
|
||||
* // nla_parse_nested() which also allows validation in the same step.
|
||||
* if (attrs[ATTR_OPTS]) {
|
||||
* struct nlattr *nested[ATTR_MAX+1];
|
||||
*
|
||||
* nla_parse_nested(nested, ATTR_MAX, attrs[ATTR_OPTS], &policy);
|
||||
*
|
||||
* if (nested[ATTR_FOO])
|
||||
* uint32_t foo = nla_get_u32(nested[ATTR_FOO]);
|
||||
* }
|
||||
* @endcode
|
||||
*
|
||||
* @subsection attr_exceptions Exception Based Attribute Construction
|
||||
* Often a large number of attributes are added to a message in a single
|
||||
* function. In order to simplify error handling, a second set of
|
||||
* construction functions exist which jump to a error label when they
|
||||
* fail instead of returning an error code. This second set consists
|
||||
* of macros which are named after their error code based counterpart
|
||||
* except that the name is written all uppercase.
|
||||
*
|
||||
* All of the macros jump to the target \c nla_put_failure if they fail.
|
||||
* @code
|
||||
* void my_func(struct nl_msg *msg)
|
||||
* {
|
||||
* NLA_PUT_U32(msg, ATTR_FOO, 10);
|
||||
* NLA_PUT_STRING(msg, ATTR_BAR, "bar");
|
||||
*
|
||||
* return 0;
|
||||
*
|
||||
* nla_put_failure:
|
||||
* return -NLE_NOMEM;
|
||||
* }
|
||||
* @endcode
|
||||
*
|
||||
* @subsection attr_examples Examples
|
||||
* @par Example 1.1 Constructing a netlink message with attributes.
|
||||
* @code
|
||||
* struct nl_msg *build_msg(int ifindex, struct nl_addr *lladdr, int mtu)
|
||||
* {
|
||||
* struct nl_msg *msg;
|
||||
* struct nlattr *info, *vlan;
|
||||
* struct ifinfomsg ifi = {
|
||||
* .ifi_family = AF_INET,
|
||||
* .ifi_index = ifindex,
|
||||
* };
|
||||
*
|
||||
* // Allocate a new netlink message, type=RTM_SETLINK, flags=NLM_F_ECHO
|
||||
* if (!(msg = nlmsg_alloc_simple(RTM_SETLINK, NLM_F_ECHO)))
|
||||
* return NULL;
|
||||
*
|
||||
* // Append the family specific header (struct ifinfomsg)
|
||||
* if (nlmsg_append(msg, &ifi, sizeof(ifi), NLMSG_ALIGNTO) < 0)
|
||||
* goto nla_put_failure
|
||||
*
|
||||
* // Append a 32 bit integer attribute to carry the MTU
|
||||
* NLA_PUT_U32(msg, IFLA_MTU, mtu);
|
||||
*
|
||||
* // Append a unspecific attribute to carry the link layer address
|
||||
* NLA_PUT_ADDR(msg, IFLA_ADDRESS, lladdr);
|
||||
*
|
||||
* // Append a container for nested attributes to carry link information
|
||||
* if (!(info = nla_nest_start(msg, IFLA_LINKINFO)))
|
||||
* goto nla_put_failure;
|
||||
*
|
||||
* // Put a string attribute into the container
|
||||
* NLA_PUT_STRING(msg, IFLA_INFO_KIND, "vlan");
|
||||
*
|
||||
* // Append another container inside the open container to carry
|
||||
* // vlan specific attributes
|
||||
* if (!(vlan = nla_nest_start(msg, IFLA_INFO_DATA)))
|
||||
* goto nla_put_failure;
|
||||
*
|
||||
* // add vlan specific info attributes here...
|
||||
*
|
||||
* // Finish nesting the vlan attributes and close the second container.
|
||||
* nla_nest_end(msg, vlan);
|
||||
*
|
||||
* // Finish nesting the link info attribute and close the first container.
|
||||
* nla_nest_end(msg, info);
|
||||
*
|
||||
* return msg;
|
||||
*
|
||||
* // If any of the construction macros fails, we end up here.
|
||||
* nla_put_failure:
|
||||
* nlmsg_free(msg);
|
||||
* return NULL;
|
||||
* }
|
||||
* @endcode
|
||||
*
|
||||
* @par Example 2.1 Parsing a netlink message with attributes.
|
||||
* @code
|
||||
* int parse_message(struct nl_msg *msg)
|
||||
* {
|
||||
* // The policy defines two attributes: a 32 bit integer and a container
|
||||
* // for nested attributes.
|
||||
* struct nla_policy attr_policy[ATTR_MAX+1] = {
|
||||
* [ATTR_FOO] = { .type = NLA_U32 },
|
||||
* [ATTR_BAR] = { .type = NLA_NESTED },
|
||||
* };
|
||||
* struct nlattr *attrs[ATTR_MAX+1];
|
||||
* int err;
|
||||
*
|
||||
* // The nlmsg_parse() function will make sure that the message contains
|
||||
* // enough payload to hold the header (struct my_hdr), validates any
|
||||
* // attributes attached to the messages and stores a pointer to each
|
||||
* // attribute in the attrs[] array accessable by attribute type.
|
||||
* if ((err = nlmsg_parse(nlmsg_hdr(msg), sizeof(struct my_hdr), attrs,
|
||||
* ATTR_MAX, attr_policy)) < 0)
|
||||
* goto errout;
|
||||
*
|
||||
* if (attrs[ATTR_FOO]) {
|
||||
* // It is safe to directly access the attribute payload without
|
||||
* // any further checks since nlmsg_parse() enforced the policy.
|
||||
* uint32_t foo = nla_get_u32(attrs[ATTR_FOO]);
|
||||
* }
|
||||
*
|
||||
* if (attrs[ATTR_BAR]) {
|
||||
* struct nlattr *nested[NESTED_MAX+1];
|
||||
*
|
||||
* // Attributes nested in a container can be parsed the same way
|
||||
* // as top level attributes.
|
||||
* if ((err = nla_parse_nested(nested, NESTED_MAX, attrs[ATTR_BAR],
|
||||
* nested_policy)) < 0)
|
||||
* goto errout;
|
||||
*
|
||||
* // Process nested attributes here.
|
||||
* }
|
||||
*
|
||||
* err = 0;
|
||||
* errout:
|
||||
* return err;
|
||||
* }
|
||||
* @endcode
|
||||
*
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @name Attribute Size Calculation
|
||||
* @{
|
||||
*/
|
||||
|
||||
/** @} */
|
||||
|
||||
/**
|
||||
* @name Parsing Attributes
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* Check if the attribute header and payload can be accessed safely.
|
||||
* @arg nla Attribute of any kind.
|
||||
* @arg remaining Number of bytes remaining in attribute stream.
|
||||
*
|
||||
* Verifies that the header and payload do not exceed the number of
|
||||
* bytes left in the attribute stream. This function must be called
|
||||
* before access the attribute header or payload when iterating over
|
||||
* the attribute stream using nla_next().
|
||||
*
|
||||
* @return True if the attribute can be accessed safely, false otherwise.
|
||||
*/
|
||||
int nla_ok(const struct nlattr *nla, int remaining)
|
||||
{
|
||||
return remaining >= sizeof(*nla) &&
|
||||
nla->nla_len >= sizeof(*nla) &&
|
||||
nla->nla_len <= remaining;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return next attribute in a stream of attributes.
|
||||
* @arg nla Attribute of any kind.
|
||||
* @arg remaining Variable to count remaining bytes in stream.
|
||||
*
|
||||
* Calculates the offset to the next attribute based on the attribute
|
||||
* given. The attribute provided is assumed to be accessible, the
|
||||
* caller is responsible to use nla_ok() beforehand. The offset (length
|
||||
* of specified attribute including padding) is then subtracted from
|
||||
* the remaining bytes variable and a pointer to the next attribute is
|
||||
* returned.
|
||||
*
|
||||
* nla_next() can be called as long as remainig is >0.
|
||||
*
|
||||
* @return Pointer to next attribute.
|
||||
*/
|
||||
struct nlattr *nla_next(const struct nlattr *nla, int *remaining)
|
||||
{
|
||||
int totlen = NLA_ALIGN(nla->nla_len);
|
||||
|
||||
*remaining -= totlen;
|
||||
return (struct nlattr *) ((char *) nla + totlen);
|
||||
}
|
||||
|
||||
static uint16_t nla_attr_minlen[NLA_TYPE_MAX+1] = {
|
||||
[NLA_U8] = sizeof(uint8_t),
|
||||
[NLA_U16] = sizeof(uint16_t),
|
||||
[NLA_U32] = sizeof(uint32_t),
|
||||
[NLA_U64] = sizeof(uint64_t),
|
||||
[NLA_STRING] = 1,
|
||||
};
|
||||
|
||||
static int validate_nla(struct nlattr *nla, int maxtype,
|
||||
struct nla_policy *policy)
|
||||
{
|
||||
struct nla_policy *pt;
|
||||
int minlen = 0, type = nla_type(nla);
|
||||
|
||||
if (type <= 0 || type > maxtype)
|
||||
return 0;
|
||||
|
||||
pt = &policy[type];
|
||||
|
||||
if (pt->type > NLA_TYPE_MAX)
|
||||
BUG();
|
||||
|
||||
if (pt->minlen)
|
||||
minlen = pt->minlen;
|
||||
else if (pt->type != NLA_UNSPEC)
|
||||
minlen = nla_attr_minlen[pt->type];
|
||||
|
||||
if (pt->type == NLA_FLAG && nla_len(nla) > 0)
|
||||
return -NLE_RANGE;
|
||||
|
||||
if (nla_len(nla) < minlen)
|
||||
return -NLE_RANGE;
|
||||
|
||||
if (pt->maxlen && nla_len(nla) > pt->maxlen)
|
||||
return -NLE_RANGE;
|
||||
|
||||
if (pt->type == NLA_STRING) {
|
||||
char *data = nla_data(nla);
|
||||
if (data[nla_len(nla) - 1] != '\0')
|
||||
return -NLE_INVAL;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Create attribute index based on a stream of attributes.
|
||||
* @arg tb Index array to be filled (maxtype+1 elements).
|
||||
* @arg maxtype Maximum attribute type expected and accepted.
|
||||
* @arg head Head of attribute stream.
|
||||
* @arg len Length of attribute stream.
|
||||
* @arg policy Attribute validation policy.
|
||||
*
|
||||
* Iterates over the stream of attributes and stores a pointer to each
|
||||
* attribute in the index array using the attribute type as index to
|
||||
* the array. Attribute with a type greater than the maximum type
|
||||
* specified will be silently ignored in order to maintain backwards
|
||||
* compatibility. If \a policy is not NULL, the attribute will be
|
||||
* validated using the specified policy.
|
||||
*
|
||||
* @see nla_validate
|
||||
* @return 0 on success or a negative error code.
|
||||
*/
|
||||
int nla_parse(struct nlattr *tb[], int maxtype, struct nlattr *head, int len,
|
||||
struct nla_policy *policy)
|
||||
{
|
||||
struct nlattr *nla;
|
||||
int rem, err;
|
||||
|
||||
memset(tb, 0, sizeof(struct nlattr *) * (maxtype + 1));
|
||||
|
||||
nla_for_each_attr(nla, head, len, rem) {
|
||||
int type = nla_type(nla);
|
||||
|
||||
if (type == 0) {
|
||||
fprintf(stderr, "Illegal nla->nla_type == 0\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
if (type <= maxtype) {
|
||||
if (policy) {
|
||||
err = validate_nla(nla, maxtype, policy);
|
||||
if (err < 0)
|
||||
goto errout;
|
||||
}
|
||||
|
||||
tb[type] = nla;
|
||||
}
|
||||
}
|
||||
|
||||
if (rem > 0)
|
||||
fprintf(stderr, "netlink: %d bytes leftover after parsing "
|
||||
"attributes.\n", rem);
|
||||
|
||||
err = 0;
|
||||
errout:
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate a stream of attributes.
|
||||
* @arg head Head of attributes stream.
|
||||
* @arg len Length of attributes stream.
|
||||
* @arg maxtype Maximum attribute type expected and accepted.
|
||||
* @arg policy Validation policy.
|
||||
*
|
||||
* Iterates over the stream of attributes and validates each attribute
|
||||
* one by one using the specified policy. Attributes with a type greater
|
||||
* than the maximum type specified will be silently ignored in order to
|
||||
* maintain backwards compatibility.
|
||||
*
|
||||
* See \ref attr_datatypes for more details on what kind of validation
|
||||
* checks are performed on each attribute data type.
|
||||
*
|
||||
* @return 0 on success or a negative error code.
|
||||
*/
|
||||
int nla_validate(struct nlattr *head, int len, int maxtype,
|
||||
struct nla_policy *policy)
|
||||
{
|
||||
struct nlattr *nla;
|
||||
int rem, err;
|
||||
|
||||
nla_for_each_attr(nla, head, len, rem) {
|
||||
err = validate_nla(nla, maxtype, policy);
|
||||
if (err < 0)
|
||||
goto errout;
|
||||
}
|
||||
|
||||
err = 0;
|
||||
errout:
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find a single attribute in a stream of attributes.
|
||||
* @arg head Head of attributes stream.
|
||||
* @arg len Length of attributes stream.
|
||||
* @arg attrtype Attribute type to look for.
|
||||
*
|
||||
* Iterates over the stream of attributes and compares each type with
|
||||
* the type specified. Returns the first attribute which matches the
|
||||
* type.
|
||||
*
|
||||
* @return Pointer to attribute found or NULL.
|
||||
*/
|
||||
struct nlattr *nla_find(struct nlattr *head, int len, int attrtype)
|
||||
{
|
||||
struct nlattr *nla;
|
||||
int rem;
|
||||
|
||||
nla_for_each_attr(nla, head, len, rem)
|
||||
if (nla_type(nla) == attrtype)
|
||||
return nla;
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/** @} */
|
||||
|
||||
/**
|
||||
* @name Unspecific Attribute
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* Reserve space for a attribute.
|
||||
* @arg msg Netlink Message.
|
||||
* @arg attrtype Attribute Type.
|
||||
* @arg attrlen Length of payload.
|
||||
*
|
||||
* Reserves room for a attribute in the specified netlink message and
|
||||
* fills in the attribute header (type, length). Returns NULL if there
|
||||
* is unsuficient space for the attribute.
|
||||
*
|
||||
* Any padding between payload and the start of the next attribute is
|
||||
* zeroed out.
|
||||
*
|
||||
* @return Pointer to start of attribute or NULL on failure.
|
||||
*/
|
||||
struct nlattr *nla_reserve(struct nl_msg *msg, int attrtype, int attrlen)
|
||||
{
|
||||
struct nlattr *nla;
|
||||
int tlen;
|
||||
|
||||
tlen = NLMSG_ALIGN(msg->nm_nlh->nlmsg_len) + nla_total_size(attrlen);
|
||||
|
||||
if ((tlen + msg->nm_nlh->nlmsg_len) > msg->nm_size)
|
||||
return NULL;
|
||||
|
||||
nla = (struct nlattr *) nlmsg_tail(msg->nm_nlh);
|
||||
nla->nla_type = attrtype;
|
||||
nla->nla_len = nla_attr_size(attrlen);
|
||||
|
||||
memset((unsigned char *) nla + nla->nla_len, 0, nla_padlen(attrlen));
|
||||
msg->nm_nlh->nlmsg_len = tlen;
|
||||
|
||||
NL_DBG(2, "msg %p: Reserved %d bytes at offset +%td for attr %d "
|
||||
"nlmsg_len=%d\n", msg, attrlen,
|
||||
(void *) nla - nlmsg_data(msg->nm_nlh),
|
||||
attrtype, msg->nm_nlh->nlmsg_len);
|
||||
|
||||
return nla;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a unspecific attribute to netlink message.
|
||||
* @arg msg Netlink message.
|
||||
* @arg attrtype Attribute type.
|
||||
* @arg datalen Length of data to be used as payload.
|
||||
* @arg data Pointer to data to be used as attribute payload.
|
||||
*
|
||||
* Reserves room for a unspecific attribute and copies the provided data
|
||||
* into the message as payload of the attribute. Returns an error if there
|
||||
* is insufficient space for the attribute.
|
||||
*
|
||||
* @see nla_reserve
|
||||
* @return 0 on success or a negative error code.
|
||||
*/
|
||||
int nla_put(struct nl_msg *msg, int attrtype, int datalen, const void *data)
|
||||
{
|
||||
struct nlattr *nla;
|
||||
|
||||
nla = nla_reserve(msg, attrtype, datalen);
|
||||
if (!nla)
|
||||
return -NLE_NOMEM;
|
||||
|
||||
memcpy(nla_data(nla), data, datalen);
|
||||
NL_DBG(2, "msg %p: Wrote %d bytes at offset +%td for attr %d\n",
|
||||
msg, datalen, (void *) nla - nlmsg_data(msg->nm_nlh), attrtype);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/** @} */
|
||||
@ -1,376 +0,0 @@
|
||||
/*
|
||||
* lib/cache.c Caching Module
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation version 2.1
|
||||
* of the License.
|
||||
*
|
||||
* Copyright (c) 2003-2008 Thomas Graf <tgraf@suug.ch>
|
||||
*/
|
||||
|
||||
/**
|
||||
* @ingroup cache_mngt
|
||||
* @defgroup cache Cache
|
||||
*
|
||||
* @code
|
||||
* Cache Management | | Type Specific Cache Operations
|
||||
*
|
||||
* | | +----------------+ +------------+
|
||||
* | request update | | msg_parser |
|
||||
* | | +----------------+ +------------+
|
||||
* +- - - - -^- - - - - - - -^- -|- - - -
|
||||
* nl_cache_update: | | | |
|
||||
* 1) --------- co_request_update ------+ | |
|
||||
* | | |
|
||||
* 2) destroy old cache +----------- pp_cb ---------|---+
|
||||
* | | |
|
||||
* 3) ---------- nl_recvmsgs ----------+ +- cb_valid -+
|
||||
* +--------------+ | | | |
|
||||
* | nl_cache_add |<-----+ + - - -v- -|- - - - - - - - - - -
|
||||
* +--------------+ | | +-------------+
|
||||
* | nl_recvmsgs |
|
||||
* | | +-----|-^-----+
|
||||
* +---v-|---+
|
||||
* | | | nl_recv |
|
||||
* +---------+
|
||||
* | | Core Netlink
|
||||
* @endcode
|
||||
*
|
||||
* @{
|
||||
*/
|
||||
|
||||
#include <netlink-local.h>
|
||||
#include <netlink/netlink.h>
|
||||
#include <netlink/cache.h>
|
||||
#include <netlink/object.h>
|
||||
#include <netlink/utils.h>
|
||||
|
||||
/**
|
||||
* @name Cache Creation/Deletion
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* Allocate an empty cache
|
||||
* @arg ops cache operations to base the cache on
|
||||
*
|
||||
* @return A newly allocated and initialized cache.
|
||||
*/
|
||||
struct nl_cache *nl_cache_alloc(struct nl_cache_ops *ops)
|
||||
{
|
||||
struct nl_cache *cache;
|
||||
|
||||
cache = calloc(1, sizeof(*cache));
|
||||
if (!cache)
|
||||
return NULL;
|
||||
|
||||
nl_init_list_head(&cache->c_items);
|
||||
cache->c_ops = ops;
|
||||
|
||||
NL_DBG(2, "Allocated cache %p <%s>.\n", cache, nl_cache_name(cache));
|
||||
|
||||
return cache;
|
||||
}
|
||||
|
||||
int nl_cache_alloc_and_fill(struct nl_cache_ops *ops, struct nl_sock *sock,
|
||||
struct nl_cache **result)
|
||||
{
|
||||
struct nl_cache *cache;
|
||||
int err;
|
||||
|
||||
if (!(cache = nl_cache_alloc(ops)))
|
||||
return -NLE_NOMEM;
|
||||
|
||||
if (sock && (err = nl_cache_refill(sock, cache)) < 0) {
|
||||
nl_cache_free(cache);
|
||||
return err;
|
||||
}
|
||||
|
||||
*result = cache;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear a cache.
|
||||
* @arg cache cache to clear
|
||||
*
|
||||
* Removes all elements of a cache.
|
||||
*/
|
||||
void nl_cache_clear(struct nl_cache *cache)
|
||||
{
|
||||
struct nl_object *obj, *tmp;
|
||||
|
||||
NL_DBG(1, "Clearing cache %p <%s>...\n", cache, nl_cache_name(cache));
|
||||
|
||||
nl_list_for_each_entry_safe(obj, tmp, &cache->c_items, ce_list)
|
||||
nl_cache_remove(obj);
|
||||
}
|
||||
|
||||
/**
|
||||
* Free a cache.
|
||||
* @arg cache Cache to free.
|
||||
*
|
||||
* Removes all elements of a cache and frees all memory.
|
||||
*
|
||||
* @note Use this function if you are working with allocated caches.
|
||||
*/
|
||||
void nl_cache_free(struct nl_cache *cache)
|
||||
{
|
||||
if (!cache)
|
||||
return;
|
||||
|
||||
nl_cache_clear(cache);
|
||||
NL_DBG(1, "Freeing cache %p <%s>...\n", cache, nl_cache_name(cache));
|
||||
free(cache);
|
||||
}
|
||||
|
||||
/** @} */
|
||||
|
||||
/**
|
||||
* @name Cache Modifications
|
||||
* @{
|
||||
*/
|
||||
|
||||
static int __cache_add(struct nl_cache *cache, struct nl_object *obj)
|
||||
{
|
||||
obj->ce_cache = cache;
|
||||
|
||||
nl_list_add_tail(&obj->ce_list, &cache->c_items);
|
||||
cache->c_nitems++;
|
||||
|
||||
NL_DBG(1, "Added %p to cache %p <%s>.\n",
|
||||
obj, cache, nl_cache_name(cache));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add object to a cache.
|
||||
* @arg cache Cache to add object to
|
||||
* @arg obj Object to be added to the cache
|
||||
*
|
||||
* Adds the given object to the specified cache. The object is cloned
|
||||
* if it has been added to another cache already.
|
||||
*
|
||||
* @return 0 or a negative error code.
|
||||
*/
|
||||
int nl_cache_add(struct nl_cache *cache, struct nl_object *obj)
|
||||
{
|
||||
struct nl_object *new;
|
||||
|
||||
if (cache->c_ops->co_obj_ops != obj->ce_ops)
|
||||
return -NLE_OBJ_MISMATCH;
|
||||
|
||||
if (!nl_list_empty(&obj->ce_list)) {
|
||||
new = nl_object_clone(obj);
|
||||
if (!new)
|
||||
return -NLE_NOMEM;
|
||||
} else {
|
||||
nl_object_get(obj);
|
||||
new = obj;
|
||||
}
|
||||
|
||||
return __cache_add(cache, new);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes an object from a cache.
|
||||
* @arg obj Object to remove from its cache
|
||||
*
|
||||
* Removes the object \c obj from the cache it is assigned to, since
|
||||
* an object can only be assigned to one cache at a time, the cache
|
||||
* must ne be passed along with it.
|
||||
*/
|
||||
void nl_cache_remove(struct nl_object *obj)
|
||||
{
|
||||
struct nl_cache *cache = obj->ce_cache;
|
||||
|
||||
if (cache == NULL)
|
||||
return;
|
||||
|
||||
nl_list_del(&obj->ce_list);
|
||||
obj->ce_cache = NULL;
|
||||
nl_object_put(obj);
|
||||
cache->c_nitems--;
|
||||
|
||||
NL_DBG(1, "Deleted %p from cache %p <%s>.\n",
|
||||
obj, cache, nl_cache_name(cache));
|
||||
}
|
||||
|
||||
/** @} */
|
||||
|
||||
/**
|
||||
* @name Synchronization
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* Request a full dump from the kernel to fill a cache
|
||||
* @arg sk Netlink socket.
|
||||
* @arg cache Cache subjected to be filled.
|
||||
*
|
||||
* Send a dumping request to the kernel causing it to dump all objects
|
||||
* related to the specified cache to the netlink socket.
|
||||
*
|
||||
* Use nl_cache_pickup() to read the objects from the socket and fill them
|
||||
* into a cache.
|
||||
*/
|
||||
int nl_cache_request_full_dump(struct nl_sock *sk, struct nl_cache *cache)
|
||||
{
|
||||
NL_DBG(2, "Requesting dump from kernel for cache %p <%s>...\n",
|
||||
cache, nl_cache_name(cache));
|
||||
|
||||
if (cache->c_ops->co_request_update == NULL)
|
||||
return -NLE_OPNOTSUPP;
|
||||
|
||||
return cache->c_ops->co_request_update(cache, sk);
|
||||
}
|
||||
|
||||
/** @cond SKIP */
|
||||
struct update_xdata {
|
||||
struct nl_cache_ops *ops;
|
||||
struct nl_parser_param *params;
|
||||
};
|
||||
|
||||
static int update_msg_parser(struct nl_msg *msg, void *arg)
|
||||
{
|
||||
struct update_xdata *x = arg;
|
||||
|
||||
return nl_cache_parse(x->ops, &msg->nm_src, msg->nm_nlh, x->params);
|
||||
}
|
||||
/** @endcond */
|
||||
|
||||
int __cache_pickup(struct nl_sock *sk, struct nl_cache *cache,
|
||||
struct nl_parser_param *param)
|
||||
{
|
||||
int err;
|
||||
struct nl_cb *cb;
|
||||
struct update_xdata x = {
|
||||
.ops = cache->c_ops,
|
||||
.params = param,
|
||||
};
|
||||
|
||||
NL_DBG(1, "Picking up answer for cache %p <%s>...\n",
|
||||
cache, nl_cache_name(cache));
|
||||
|
||||
cb = nl_cb_clone(sk->s_cb);
|
||||
if (cb == NULL)
|
||||
return -NLE_NOMEM;
|
||||
|
||||
nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, update_msg_parser, &x);
|
||||
|
||||
err = nl_recvmsgs(sk, cb);
|
||||
if (err < 0)
|
||||
NL_DBG(2, "While picking up for %p <%s>, recvmsgs() returned " \
|
||||
"%d: %s", cache, nl_cache_name(cache),
|
||||
err, nl_geterror(err));
|
||||
|
||||
nl_cb_put(cb);
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
static int pickup_cb(struct nl_object *c, struct nl_parser_param *p)
|
||||
{
|
||||
return nl_cache_add((struct nl_cache *) p->pp_arg, c);
|
||||
}
|
||||
|
||||
/**
|
||||
* Pickup a netlink dump response and put it into a cache.
|
||||
* @arg sk Netlink socket.
|
||||
* @arg cache Cache to put items into.
|
||||
*
|
||||
* Waits for netlink messages to arrive, parses them and puts them into
|
||||
* the specified cache.
|
||||
*
|
||||
* @return 0 on success or a negative error code.
|
||||
*/
|
||||
int nl_cache_pickup(struct nl_sock *sk, struct nl_cache *cache)
|
||||
{
|
||||
struct nl_parser_param p = {
|
||||
.pp_cb = pickup_cb,
|
||||
.pp_arg = cache,
|
||||
};
|
||||
|
||||
return __cache_pickup(sk, cache, &p);
|
||||
}
|
||||
|
||||
|
||||
/** @} */
|
||||
|
||||
/**
|
||||
* @name Parsing
|
||||
* @{
|
||||
*/
|
||||
|
||||
/** @cond SKIP */
|
||||
int nl_cache_parse(struct nl_cache_ops *ops, struct sockaddr_nl *who,
|
||||
struct nlmsghdr *nlh, struct nl_parser_param *params)
|
||||
{
|
||||
int i, err;
|
||||
|
||||
if (!nlmsg_valid_hdr(nlh, ops->co_hdrsize))
|
||||
return -NLE_MSG_TOOSHORT;
|
||||
|
||||
for (i = 0; ops->co_msgtypes[i].mt_id >= 0; i++) {
|
||||
if (ops->co_msgtypes[i].mt_id == nlh->nlmsg_type) {
|
||||
err = ops->co_msg_parser(ops, who, nlh, params);
|
||||
if (err != -NLE_OPNOTSUPP)
|
||||
goto errout;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
err = -NLE_MSGTYPE_NOSUPPORT;
|
||||
errout:
|
||||
return err;
|
||||
}
|
||||
/** @endcond */
|
||||
|
||||
/**
|
||||
* Parse a netlink message and add it to the cache.
|
||||
* @arg cache cache to add element to
|
||||
* @arg msg netlink message
|
||||
*
|
||||
* Parses a netlink message by calling the cache specific message parser
|
||||
* and adds the new element to the cache.
|
||||
*
|
||||
* @return 0 or a negative error code.
|
||||
*/
|
||||
int nl_cache_parse_and_add(struct nl_cache *cache, struct nl_msg *msg)
|
||||
{
|
||||
struct nl_parser_param p = {
|
||||
.pp_cb = pickup_cb,
|
||||
.pp_arg = cache,
|
||||
};
|
||||
|
||||
return nl_cache_parse(cache->c_ops, NULL, nlmsg_hdr(msg), &p);
|
||||
}
|
||||
|
||||
/**
|
||||
* (Re)fill a cache with the contents in the kernel.
|
||||
* @arg sk Netlink socket.
|
||||
* @arg cache cache to update
|
||||
*
|
||||
* Clears the specified cache and fills it with the current state in
|
||||
* the kernel.
|
||||
*
|
||||
* @return 0 or a negative error code.
|
||||
*/
|
||||
int nl_cache_refill(struct nl_sock *sk, struct nl_cache *cache)
|
||||
{
|
||||
int err;
|
||||
|
||||
err = nl_cache_request_full_dump(sk, cache);
|
||||
if (err < 0)
|
||||
return err;
|
||||
|
||||
NL_DBG(2, "Upading cache %p <%s>, request sent, waiting for dump...\n",
|
||||
cache, nl_cache_name(cache));
|
||||
nl_cache_clear(cache);
|
||||
|
||||
return nl_cache_pickup(sk, cache);
|
||||
}
|
||||
|
||||
/** @} */
|
||||
@ -1,131 +0,0 @@
|
||||
/*
|
||||
* lib/cache_mngt.c Cache Management
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation version 2.1
|
||||
* of the License.
|
||||
*
|
||||
* Copyright (c) 2003-2008 Thomas Graf <tgraf@suug.ch>
|
||||
*/
|
||||
|
||||
/**
|
||||
* @ingroup core
|
||||
* @defgroup cache_mngt Caching
|
||||
* @{
|
||||
*/
|
||||
|
||||
#include <netlink-local.h>
|
||||
#include <netlink/netlink.h>
|
||||
#include <netlink/cache.h>
|
||||
#include <netlink/utils.h>
|
||||
|
||||
static struct nl_cache_ops *cache_ops;
|
||||
|
||||
/**
|
||||
* @name Cache Operations Sets
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* Lookup the set cache operations of a certain cache type
|
||||
* @arg name name of the cache type
|
||||
*
|
||||
* @return The cache operations or NULL if no operations
|
||||
* have been registered under the specified name.
|
||||
*/
|
||||
struct nl_cache_ops *nl_cache_ops_lookup(const char *name)
|
||||
{
|
||||
struct nl_cache_ops *ops;
|
||||
|
||||
for (ops = cache_ops; ops; ops = ops->co_next)
|
||||
if (!strcmp(ops->co_name, name))
|
||||
return ops;
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Associate a message type to a set of cache operations
|
||||
* @arg protocol netlink protocol
|
||||
* @arg msgtype netlink message type
|
||||
*
|
||||
* Associates the specified netlink message type with
|
||||
* a registered set of cache operations.
|
||||
*
|
||||
* @return The cache operations or NULL if no association
|
||||
* could be made.
|
||||
*/
|
||||
struct nl_cache_ops *nl_cache_ops_associate(int protocol, int msgtype)
|
||||
{
|
||||
int i;
|
||||
struct nl_cache_ops *ops;
|
||||
|
||||
for (ops = cache_ops; ops; ops = ops->co_next) {
|
||||
if (ops->co_protocol != protocol)
|
||||
continue;
|
||||
|
||||
for (i = 0; ops->co_msgtypes[i].mt_id >= 0; i++)
|
||||
if (ops->co_msgtypes[i].mt_id == msgtype)
|
||||
return ops;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a set of cache operations
|
||||
* @arg ops cache operations
|
||||
*
|
||||
* Called by users of caches to announce the avaibility of
|
||||
* a certain cache type.
|
||||
*
|
||||
* @return 0 on success or a negative error code.
|
||||
*/
|
||||
int nl_cache_mngt_register(struct nl_cache_ops *ops)
|
||||
{
|
||||
if (!ops->co_name || !ops->co_obj_ops)
|
||||
return -NLE_INVAL;
|
||||
|
||||
if (nl_cache_ops_lookup(ops->co_name))
|
||||
return -NLE_EXIST;
|
||||
|
||||
ops->co_next = cache_ops;
|
||||
cache_ops = ops;
|
||||
|
||||
NL_DBG(1, "Registered cache operations %s\n", ops->co_name);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unregister a set of cache operations
|
||||
* @arg ops cache operations
|
||||
*
|
||||
* Called by users of caches to announce a set of
|
||||
* cache operations is no longer available. The
|
||||
* specified cache operations must have been registered
|
||||
* previously using nl_cache_mngt_register()
|
||||
*
|
||||
* @return 0 on success or a negative error code
|
||||
*/
|
||||
int nl_cache_mngt_unregister(struct nl_cache_ops *ops)
|
||||
{
|
||||
struct nl_cache_ops *t, **tp;
|
||||
|
||||
for (tp = &cache_ops; (t=*tp) != NULL; tp = &t->co_next)
|
||||
if (t == ops)
|
||||
break;
|
||||
|
||||
if (!t)
|
||||
return -NLE_NOCACHE;
|
||||
|
||||
NL_DBG(1, "Unregistered cache operations %s\n", ops->co_name);
|
||||
|
||||
*tp = t->co_next;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/** @} */
|
||||
|
||||
/** @} */
|
||||
@ -1,116 +0,0 @@
|
||||
/*
|
||||
* lib/error.c Error Handling
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation version 2.1
|
||||
* of the License.
|
||||
*
|
||||
* Copyright (c) 2008 Thomas Graf <tgraf@suug.ch>
|
||||
*/
|
||||
|
||||
#include <netlink-local.h>
|
||||
#include <netlink/netlink.h>
|
||||
|
||||
static const char *errmsg[NLE_MAX+1] = {
|
||||
[NLE_SUCCESS] = "Success",
|
||||
[NLE_FAILURE] = "Unspecific failure",
|
||||
[NLE_INTR] = "Interrupted system call",
|
||||
[NLE_BAD_SOCK] = "Bad socket",
|
||||
[NLE_AGAIN] = "Try again",
|
||||
[NLE_NOMEM] = "Out of memory",
|
||||
[NLE_EXIST] = "Object exists",
|
||||
[NLE_INVAL] = "Invalid input data or parameter",
|
||||
[NLE_RANGE] = "Input data out of range",
|
||||
[NLE_MSGSIZE] = "Message size not sufficient",
|
||||
[NLE_OPNOTSUPP] = "Operation not supported",
|
||||
[NLE_AF_NOSUPPORT] = "Address family not supported",
|
||||
[NLE_OBJ_NOTFOUND] = "Object not found",
|
||||
[NLE_NOATTR] = "Attribute not available",
|
||||
[NLE_MISSING_ATTR] = "Missing attribute",
|
||||
[NLE_AF_MISMATCH] = "Address family mismatch",
|
||||
[NLE_SEQ_MISMATCH] = "Message sequence number mismatch",
|
||||
[NLE_MSG_OVERFLOW] = "Kernel reported message overflow",
|
||||
[NLE_MSG_TRUNC] = "Kernel reported truncated message",
|
||||
[NLE_NOADDR] = "Invalid address for specified address family",
|
||||
[NLE_SRCRT_NOSUPPORT] = "Source based routing not supported",
|
||||
[NLE_MSG_TOOSHORT] = "Netlink message is too short",
|
||||
[NLE_MSGTYPE_NOSUPPORT] = "Netlink message type is not supported",
|
||||
[NLE_OBJ_MISMATCH] = "Object type does not match cache",
|
||||
[NLE_NOCACHE] = "Unknown or invalid cache type",
|
||||
[NLE_BUSY] = "Object busy",
|
||||
[NLE_PROTO_MISMATCH] = "Protocol mismatch",
|
||||
[NLE_NOACCESS] = "No Access",
|
||||
[NLE_PERM] = "Operation not permitted",
|
||||
[NLE_PKTLOC_FILE] = "Unable to open packet location file",
|
||||
[NLE_PARSE_ERR] = "Unable to parse object",
|
||||
[NLE_NODEV] = "No such device",
|
||||
[NLE_IMMUTABLE] = "Immutable attribute",
|
||||
[NLE_DUMP_INTR] = "Dump inconsistency detected, interrupted",
|
||||
};
|
||||
|
||||
/**
|
||||
* Return error message for an error code
|
||||
* @return error message
|
||||
*/
|
||||
const char *nl_geterror(int error)
|
||||
{
|
||||
error = abs(error);
|
||||
|
||||
if (error > NLE_MAX)
|
||||
error = NLE_FAILURE;
|
||||
|
||||
return errmsg[error];
|
||||
}
|
||||
|
||||
/**
|
||||
* Print a libnl error message
|
||||
* @arg s error message prefix
|
||||
*
|
||||
* Prints the error message of the call that failed last.
|
||||
*
|
||||
* If s is not NULL and *s is not a null byte the argument
|
||||
* string is printed, followed by a colon and a blank. Then
|
||||
* the error message and a new-line.
|
||||
*/
|
||||
void nl_perror(int error, const char *s)
|
||||
{
|
||||
if (s && *s)
|
||||
fprintf(stderr, "%s: %s\n", s, nl_geterror(error));
|
||||
else
|
||||
fprintf(stderr, "%s\n", nl_geterror(error));
|
||||
}
|
||||
|
||||
int nl_syserr2nlerr(int error)
|
||||
{
|
||||
error = abs(error);
|
||||
|
||||
switch (error) {
|
||||
case EBADF: return NLE_BAD_SOCK;
|
||||
case EADDRINUSE: return NLE_EXIST;
|
||||
case EEXIST: return NLE_EXIST;
|
||||
case EADDRNOTAVAIL: return NLE_NOADDR;
|
||||
case ESRCH: /* fall through */
|
||||
case ENOENT: return NLE_OBJ_NOTFOUND;
|
||||
case EINTR: return NLE_INTR;
|
||||
case EAGAIN: return NLE_AGAIN;
|
||||
case ENOTSOCK: return NLE_BAD_SOCK;
|
||||
case ENOPROTOOPT: return NLE_INVAL;
|
||||
case EFAULT: return NLE_INVAL;
|
||||
case EACCES: return NLE_NOACCESS;
|
||||
case EINVAL: return NLE_INVAL;
|
||||
case ENOBUFS: return NLE_NOMEM;
|
||||
case ENOMEM: return NLE_NOMEM;
|
||||
case EAFNOSUPPORT: return NLE_AF_NOSUPPORT;
|
||||
case EPROTONOSUPPORT: return NLE_PROTO_MISMATCH;
|
||||
case EOPNOTSUPP: return NLE_OPNOTSUPP;
|
||||
case EPERM: return NLE_PERM;
|
||||
case EBUSY: return NLE_BUSY;
|
||||
case ERANGE: return NLE_RANGE;
|
||||
case ENODEV: return NLE_NODEV;
|
||||
default: return NLE_FAILURE;
|
||||
}
|
||||
}
|
||||
|
||||
/** @} */
|
||||
|
||||