60 Commits

Author SHA1 Message Date
Danielle Ratson
102942be40 src: attr: Add mnl_attr_get_uint() function
NLA_UINT attributes have a 4-byte payload if possible, and an 8-byte one
if necessary.

There are some NLA_UINT attributes that lack an appropriate getter function.

Add a function mnl_attr_get_uint() to cover that extract these. Since we
need to dispatch on length anyway, make the getter truly universal by
supporting also u8 and u16.

Signed-off-by: Danielle Ratson <danieller@nvidia.com>
Signed-off-by: Phil Sutter <phil@nwl.cc>
2024-07-31 09:12:57 +02:00
Dario Binacchi
0b242d346d include: cache copy of can.h and can/netlink.h
Ensure that rtnl-link-can example compiles in any installation. These
headers are not installed in the system.

Signed-off-by: Dario Binacchi <dario.binacchi@amarulasolutions.com>
Signed-off-by: Florian Westphal <fw@strlen.de>
2023-05-28 22:44:25 +02:00
Pablo Neira Ayuso
957dbe6fdc include: add MNL_SOCKET_DUMP_SIZE definition
Add definition to recommend a new buffer size for netlink dumps.
Details are available here:

 commit d35c99ff77ecb2eb239731b799386f3b3637a31e
 Author: Eric Dumazet <edumazet@google.com>
 Date:   Thu Oct 6 04:13:18 2016 +0900

    netlink: do not enter direct reclaim from netlink_dump()

iproute2 is using 32 KBytes buffer in netlink dumps to speed up netlink
dumps for a while. Let's recommend this buffer size through this new
definition. Update examples too.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2019-10-14 11:14:26 +02:00
Guillaume Nault
fbe0f33b45 callback: mark cb_ctl_array 'const' in mnl_cb_run2()
Users of mnl_cb_run2() would typically pass a 'static const' array as
cb_ctl_array parameter. Unfortunately this triggers a compiler warning
because the prototype doesn't declare cb_ctl_array with the 'const'
qualifier.

Since mnl_cb_run2() and __mnl_cb_run() don't modify cb_ctl_array (and
don't have any reason to do so in the future), we can mark this
parameter as 'const'.

There should be no impact on existing users beyond removing the
compiler warning when using a constant array.

Signed-off-by: Guillaume Nault <g.nault@alphalink.fr>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2016-08-08 11:32:30 +02:00
Guillaume Nault
1891e0e2ce socket: introduce mnl_socket_open2()
Define mnl_socket_open2() so that user can pass a set of SOCK_* flags
at socket creation time.

Signed-off-by: Guillaume Nault <g.nault@alphalink.fr>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2015-10-04 21:15:53 +02:00
Markus Teich
9ac5b6fb43 header: use sysconf() instead of getpagesize()
The sysconf(_SC_PAGESIZE) call is more portable since it does not need
_BSD_SOURCE defined.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2015-03-02 12:05:26 +01:00
Ken-ichirou MATSUZAWA
e374664f48 socket: creating a struct mnl_socket from a pre-existing socket
This patch defines a new function mnl_socket_fdopen() which creates a
struct mnl_socket object from a pre-existing socket like obtained from
other process and different domain/type from the same prodess.

Signed-off-by: Ken-ichirou MATSUZAWA <chamas@h4.dion.ne.jp>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2014-09-25 12:46:13 +02:00
Pablo Neira Ayuso
f94b2c6748 include: cache copy of include/linux/socket.h
This fixes compilation in Debian Squeeze:

  CC     attr.lo
In file included from ../include/libmnl/libmnl.h:9,
                 from socket.c:10:
../include/linux/netlink.h:35: error: expected specifier-qualifier-list before ‘__kernel_sa_family_t’
In file included from ../include/libmnl/libmnl.h:9,
                 from callback.c:11:
../include/linux/netlink.h:35: error: expected specifier-qualifier-list before ‘__kernel_sa_family_t’
socket.c: In function ‘mnl_socket_get_portid’:
socket.c:102: error: ‘const struct sockaddr_nl’ has no member named ‘nl_pid’
socket.c: In function ‘mnl_socket_bind’:
socket.c:146: error: ‘struct sockaddr_nl’ has no member named ‘nl_family’
socket.c:147: error: ‘struct sockaddr_nl’ has no member named ‘nl_groups’
socket.c:148: error: ‘struct sockaddr_nl’ has no member named ‘nl_pid’
socket.c:163: error: ‘struct sockaddr_nl’ has no member named ‘nl_family’
socket.c: In function ‘mnl_socket_sendto’:
socket.c:184: error: unknown field ‘nl_family’ specified in initializer
socket.c:185: warning: excess elements in struct initializer
socket.c:185: warning: (near initialization for ‘snl’)

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2014-06-13 11:20:55 +02:00
Pablo Neira Ayuso
5b8a0c7479 include: don't force compiler to enter C++11 mode
Thomas reports that g++ forces you to enable C++11 mode if
you want to use <cstdint>. Tested with g++ (GCC) 4.4.4. He
reports that this compiles fine without the special headers.
So let's get rid of them. This reverts bb8c40e.

Reported-by: Thomas Jarosch <thomas.jarosch@intra2net.com>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
Tested-by: Thomas Jarosch <thomas.jarosch@intra2net.com>
2013-07-16 11:21:26 +02:00
Pablo Neira Ayuso
419513fb3b include: cache copy of nfnetlink_conntrack.h
To ensure that examples compile in any installation. This header
is not installed in the system.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2013-06-11 17:58:32 +02:00
Stephen Hemminger
11cb82355d headers: update netlink.h to 3.9
Use sanitized kernel header from 3.9 upstream kernel.

Reported-by: Nicolas Dichtel <nicolas.dichtel@6wind.com>
Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2013-03-26 22:07:54 +01:00
Pablo Neira Ayuso
00b82dd6c9 parse: add mnl_attr_for_each_payload
This function allows you to parse the payload of the Netlink message.
Thus, you can skip the headers and go down to the part of the message
you want to parse.

This patch has updated the LIBVERSION and it also bumps the version
to 1.0.3, in case that someone wants to package some snapshot, not to
mess with the new symbol we have added.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2012-05-18 01:02:49 +02:00
Jan Engelhardt
612770e648 include: use sanitized header from headers_install
Signed-off-by: Jan Engelhardt <jengelh@medozas.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2011-03-17 23:42:30 +01:00
Pablo Neira Ayuso
dbbd8a2274 header: fix invalid use of type void * in arithmetic of mnl_for_each_nest(...)
This patch fixes the following warnings:

netlink.c:121: warning: pointer of type ‘void *’ used in arithmetic
netlink.c:121: warning: pointer of type ‘void *’ used in subtraction

We cannot use type void * for pointer arithmetic operations.

This was introduced in 478dc5f4ab8d0a639d1bafe3bd53ff3309727836

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2010-12-30 02:02:05 +01:00
Pablo Neira Ayuso
92eddd8ad5 header: fix signess issues in MNL_SOCKET_BUFFER_SIZE
This patch fixes the following compilation warning:

warning: comparison between signed and unsigned integer expressions
warning: signed and unsigned type in conditional expression

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2010-12-25 22:22:41 +01:00
Pablo Neira Ayuso
516477a5d2 headers: add local copy of linux/netlink.h to libmnl
Davem suggested to add a copy of linux/netlink.h to avoid compilation
problems in all cases scenarios.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2010-12-22 20:46:46 +01:00
Jan Engelhardt
eb5265f5cc include: add missing unistd.h
When a project uses MNL_SOCKET_BUFFER_SIZE without having included
unistd.h, it gets:

mystuff.c:36:2: warning: implicit declaration of function "getpagesize"

It is libmnl's job to include this, since mystuff.c did not use
getpagesize anywhere.

Signed-off-by: Jan Engelhardt <jengelh@medozas.de>
2010-12-20 01:40:48 +01:00
Pablo Neira Ayuso
494f4855ae nlmsg: add new message batching infrastructure
This patch adds the new message batching infrastructure that allows to
store several messages into one single datagram.

This patch includes an example for ctnetlink.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2010-12-17 12:38:42 +01:00
Pablo Neira Ayuso
fb4edf5aff header: missing parenthesis in MNL_SOCKET_BUFFER_SIZE definition
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2010-12-17 12:36:35 +01:00
Pablo Neira Ayuso
44fde57023 header: use getpagesize() for MNL_SOCKET_BUFFER_SIZE
This system call returns PAGE_SIZE which depends on the
architecture. See linux/netlink.h for more information
on the appropriate datagram size for netlink.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2010-12-17 11:41:34 +01:00
Pablo Neira Ayuso
eadc7a04ea attr: add put function that allows to check buffer size
This patch adds a set of function that allows to check the
size of the buffer. This is useful for attribute batching.

This is strongly based on a patch from Jozsef.

Signed-off-by: Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2010-12-17 10:28:38 +01:00
Pablo Neira Ayuso
18302b93fc header: add MNL_ARRAY_SIZE(x)
This patch adds MNL_ARRAY_SIZE(x) which likely to defined over
and over again if you use mnl_cb_run2(...).

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2010-12-09 13:28:34 +01:00
Pablo Neira Ayuso
bbe1f68ded attr: add mnl_attr_nest_cancel()
This patch adds mnl_attr_nest_cancel() that allows to cancel the
current nest that we are building.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2010-11-22 21:51:48 +01:00
Jan Engelhardt
fe73d7d885 build: tag function headers rather than decls as exported
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2010-11-19 13:07:48 +01:00
Pablo Neira Ayuso
68dd582805 nlmsg: remove unexisting mnl_nlmsg_total_size
Remove function whose implementation does not exist. It seems to me
like a leftover from the early days of the library.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2010-11-19 12:24:28 +01:00
Jan Engelhardt
a90cfc66d9 socket: propagate sendto/recvmsg's return types
sendto/recvfrom return ssize_t, so libmnl should not truncate the
result range.

Signed-off-by: Jan Engelhardt <jengelh@medozas.de>
2010-11-16 12:23:52 +01:00
Jan Engelhardt
478dc5f4ab attr: avoid multiple definition of hidden variable
When nesting two mnl_attr_for_each loops, the __len__ variable will be
declared twice, eliciting a warning when -Wshadow is turned on. There
can also be warnings in pre-C99 because declarations and code are
mixed. Do without any temporaries that are not explicitly specified as
macro parameters.

Signed-off-by: Jan Engelhardt <jengelh@medozas.de>
2010-11-16 12:23:25 +01:00
Jan Engelhardt
bb8c40e9ef include: use C++ headers in C++ mode
Signed-off-by: Jan Engelhardt <jengelh@medozas.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2010-11-15 20:38:05 +01:00
Cristian Rodríguez
37dac00e2a src: implement both GCC visibility support and export script
Read "How to write shared libraries" from Ulrich Drepper for more
information on this patch. Specifically section 2.2: Export control
and section 3: Maintaining APIs and ABIs.

Signed-off-by: Cristian Rodríguez <cristian.rodriguez@opensuse.org>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2010-11-15 20:31:25 +01:00
Jan Engelhardt
8958399098 nlmsg: use bool for mnl_nlmsg_ok()
Signed-off-by: Jan Engelhardt <jengelh@medozas.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2010-11-11 13:57:57 +01:00
Pablo Neira Ayuso
ea4b5f9066 nlmsg: rework mnl_nlmsg_fprintf
This patch reworks mnl_nlmsg_fprintf. It breaks backward compatibility
of this function, there was no way to improve it without doing so
(and we are still in time to break thing, BTW).

Signed-off-bu: Pablo Neira Ayuso <pablo@netfilter.org>
2010-11-07 20:40:27 +01:00
Jan Engelhardt
bae3405934 attr: rename str_null from NULL away
A small little pedantism: NULL (as in, the pointer) isn't NUL (as in,
the character with value zero, '\0'). I propose to rename it to strz,
for the zero-terminated string (sometimes referred to as ASCIZ string,
but of course C isn't tied to ASCII).

Signed-off-by: Jan Engelhardt <jengelh@medozas.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2010-10-25 00:44:00 +02:00
Jan Engelhardt
23b58da791 nlmsg: use bool return type for yes-no functions
Signed-off-by: Jan Engelhardt <jengelh@medozas.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2010-10-25 00:43:18 +02:00
Jan Engelhardt
dc7aaccd9f include: add cplusplus guards for extern
Signed-off-by: Jan Engelhardt <jengelh@medozas.de>
2010-10-22 23:10:58 +02:00
Jan Engelhardt
672366907b socket: use more appropriate types for mnl_socket_bind
groups, being a bitmask, should be unsigned; and let's also use pid_t
for pid.

Signed-off-by: Jan Engelhardt <jengelh@medozas.de>
2010-10-22 23:10:57 +02:00
Jan Engelhardt
53bcdb5446 callback: mnl_cb_run should use a void *
Because in most cases, it won't be a nul-terminated string :)

Signed-off-by: Jan Engelhardt <jengelh@medozas.de>
2010-10-22 23:10:54 +02:00
Jan Engelhardt
b0967993a7 attr: string functions should take char *
I do not quite see the point of using strlen in conjunction with
non-string data; so let's use char * as argument types to catch
misuses.

Signed-off-by: Jan Engelhardt <jengelh@medozas.de>
2010-10-22 23:10:50 +02:00
Jan Engelhardt
412c9a6afa include: consistent usage of "extern"
Signed-off-by: Jan Engelhardt <jengelh@medozas.de>
2010-10-22 22:25:16 +02:00
Pablo Neira Ayuso
4eaebf5ef2 src: define MNL_SOCKET_BUFFER_SIZE to 8192UL
Davem spotted during the Netfilter Workshop that user-space applications
should use 8KB buffers for recv(). I accidentally found that NFLOG is
not following this approach (in this case we're using 131072 bytes
messages), we have to document this.

Anyway, according to linux/netlink.h (and to complete this log message):

"skb should fit one page. This choice is good for headerless malloc.
But we should limit to 8K so that userspace does not have to
use enormous buffer sizes on recvmsg() calls just to avoid
MSG_TRUNC when PAGE_SIZE is very large."

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2010-10-21 12:00:27 +02:00
Pablo Neira Ayuso
5d51315f52 socket: remove mnl_socket_sendmsg() and mnl_socket_recvmsg()
These functions are too simple wrappers that bloat the library.
We can use mnl_socket_get_fd() to obtain the file descriptor
and invoke message-oriented system call instead.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2010-06-25 15:05:38 +02:00
Pablo Neira Ayuso
ff088fc0d8 nlmsg: use size_t instead of int for several input parameters
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2010-05-17 17:43:33 +02:00
Pablo Neira Ayuso
d75ac4598f add helpers to nest attributes
Based on code from Jozsef Kadlecsik and its ipset-5 implementation.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2010-04-30 12:29:17 +02:00
Pablo Neira Ayuso
7bdf8b8ace remove references to 'generic' in header file
The term 'Generic' is used in GeNetlink, we don't want any confusion
with it. Better remove it.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2010-04-22 14:15:25 +02:00
Pablo Neira Ayuso
9ad5f22842 use size_t to indicate the buffer size in mnl_cb_run*()
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2010-04-22 13:24:01 +02:00
Pablo Neira Ayuso
158a5b97a3 review data types for input parameters of mnl_attr_*() functions
This patch includes the following changes:
 * use uint16_t for attribute types in mnl_attr_put_*() function
 * use uint16_t for maximun type in mnl_attr_type_valid(...)
 * use size_t for attribute size validation in mnl_attr_validate2(...)
 * use unsigned int for offset in mnl_attr_parse(...) instead of int

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2010-04-22 12:53:51 +02:00
Pablo Neira Ayuso
31118c770f add mnl_nlmsg_fprintf() function for debugging purposes
This function is still quite preliminary, comments welcome!

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2010-04-19 18:14:45 +02:00
Jozsef Kadlecsik
4d2a25dafd fix for mnl_attr_for_each_nested()
Signed-off-by: Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2010-04-16 16:41:09 +02:00
Pablo Neira Ayuso
1f09fdccb7 remove mnl_nlmsg_get_len() function
Remove mnl_nlmsg_get_len() since it returns a field of a structure
that is public (struct nlmsghdr). We can directly access the header
fields and they are not likely to change in the future (at least
for this version of Netlink I think).

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2010-04-12 18:06:28 +02:00
Pablo Neira Ayuso
8e9a6ecf73 more consistency name issues: rename get_data*() to get_payload*()
This includes renaming get_tail() to get_payload_tail()

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2010-04-05 18:47:04 +02:00
Pablo Neira Ayuso
adae0f1a89 rename mnl_nlmsg_payload_size() to mnl_nlmsg_get_payload_len() for consistency
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
2010-04-05 18:23:25 +02:00