nl_recv(): Memory allocation errors are handled properly now
1. all cleanup actions (like free()) now located at the end of function
2. in case of error or EOF, *buf and *creds (if given) set to NULL
This protect from invalid code at user's side, like:
char *buf;
x = nl_recv(..., &buf, ...);
if (x<=0)
goto cleanup;
cleanup:
free(buf);
3. all intermediate buffers are stored into local variables, and user's
variables only touches at the end.
genl/family flags can be damaged during the auto-indentation.
"-" was never used in the names of the flags. "_" was used in all places
of the library. So, I just changed the undescore to the minus.
Automatic indentation can insert spaces on either side of the minus,
so the library will be compiled, but will not be usable (in this part of the code),
as the parser will split words by white space, and the flag "admin - perm"
will never work.
With this change you can still set do modifications of
Links and then to change to pass the changes to the
kernel. But it additionally enables you to interact
with this part of libnl-python in a more pythonic
way. Instead of:
libnl-3.2.12 - ./configure --disable-doc: error: conditional "LINK_DOC" was never defined. \ Usually this means the macro was only invoked conditionally.
configure: error: conditional "LINK_DOC" was never defined.
Usually this means the macro was only invoked conditionally.
Attached patch provided by Martin Jansa.
See also https://bugs.gentoo.org/show_bug.cgi?id=433565
Коренберг Марк [Thu, 30 Aug 2012 16:25:21 +0000 (22:25 +0600)]
asprintf related fixed in yy parser
1. According to man asprintf:
If memory allocation wasn't possible, or some other error occurs,
these functions will return -1, and the contents of strp is undefined.
2. Sometimes, errp was not filled at all. In high-level code, free(errp)
will called, so segmantation fault may appear in case of error in parser
3. The most cases of using asprintf is to report about allocation fail.
So, probability of allocation of asprintf buffer is very high. And that
will lead to trash in errp.
4. For simple casses I decide to replace asprintf with strdup
Fix warning "not checking return value of fscanf" in lib/utils.c: get_psched_settings
Also, change internal variables type from uint32_t to unsigned int.
Correct scanf format string should contain "SCNx32" instead of just "x",
but I decide not to fix that and just changed variable type.
So, according to man 3 printf,
uint64_t should be printed as "%llu" on some architectures, and as "%lu" on another. The same for scanf.
To eliminate that challenge, there is inttypes.h, in which appropriate constants
are defined for current architecture.
32-bit types (and even 16 and 8 bit types) should be printed using such constants if
printed variable defined as uint_XXXt or intXXXt type. But in reality 32-bit and less
types does not gain run-time error (except in scanf), because they pushed to stack as
32-bit values at least. So, I decide not to fix that.
Run-time version information is available as exported four integers:
- const int nl_ver_num = LIBNL_VER_NUM;
- const int nl_ver_maj = LIBNL_VER_MAJ;
- const int nl_ver_min = LIBNL_VER_MIN;
- const int nl_ver_mic = LIBNL_VER_MIC;
The purpose of this is to get version of compiled library as run time.
Use cases:
- To know exact version of the library in Python's ctypes module,
Say, to find out if nl_cache_mngr_alloc() allow sk=NULL
- To make sure that the version of the loaded library corresponds to the
version of headers (for the paranoid). Say, to check:
Justin Mayfield [Fri, 17 Aug 2012 01:03:48 +0000 (19:03 -0600)]
single nexthop flags bug
I ran into a bug today related to how Linux handles a route's nexthop
flags when there is just one nexthop. Namely Linux expects the flags
to be OR'd into the rtm_flags field when there is only one nexthop and
so rtnl_route_build_msg needs to check the number of nexthops and
store the nexthops flags into this field prior to calling
nlmsg_append(...&rtmsg).
Conversely the rtnl_route_parse function needs to pull these lower
0xff bits when a single nexthop is detected.
Attached is my patch. I don't like the slight duplication of doing
the rtnl_route_get_nnexthops check twice but it seemed to be the least
turmoil of any solution I thought of.
Justin Mayfield [Sat, 18 Aug 2012 00:16:44 +0000 (18:16 -0600)]
nl_addr_parse handling of 'default', 'any', and 'all'
I found a small bug in the nl_addr_parse function when being passed the
strings "default", "any", or "all". Currently nl_addr_parse will create
a zeroed nl_addr with a length corresponding to the family/hint or
AF_INET if omitted. This behavior when used in conjunction with the
libnl-route library to add default routes to the system has the side
effect of creating a route to the host address 0.0.0.0/32.
Attached is a patch that matches the iproute2 behavior more closely
where we do set the family but the length of the nl_addr is set to 0.
Thomas Graf [Wed, 29 Aug 2012 10:05:51 +0000 (12:05 +0200)]
Fix build warning after const char ** convert
Commit 25d640da4a caused the following build warning:
../include/netlink/utils.h:47:15: note: expected 'const char **' but argument is of type 'char **'
route/link/inet6.c:300:11: warning: passing argument 2 of 'nl_cancel_down_bytes' from incompatible pointer type [enabled by default]
Коренберг Марк [Fri, 8 Jun 2012 14:15:06 +0000 (20:15 +0600)]
Fix types-related warnings based on clang diagnostics
1. Fix some places where unsigned value compared < 0
2. Fix obsolete %Z specifier to more portable %z
3. Some erroneous types substitution
4. nl_msec2str() - 64-bit msec is now properly used,
Only safe changes. I mean int <--> uint32_t and signed/unsigned fixes.
Some functinos require size_t argument instead of int, but changes of
signatures of that functions is terrible thing.
Also, I do not pretend for a full list of fixes.
Just to shut up clang -Wall -Wextra
One more thing. ifindex. I don't change that because changes will
be too big for simple fix.
Andrew Collins [Mon, 11 Jun 2012 16:44:42 +0000 (10:44 -0600)]
Add 'ingress' to the list of recognized TC handles.
Currently, rtnl_tc_handle2str understands the ingress handle but
rtnl_tc_str2handle does not. This change lets rtnl_tc_str2handle
recognize 'ingress' as a valid handle as well.
Flags properties description and implementation fixed
1. Address, Link and Vlan classes affected with same bug
2. Flags property are not designed as set class. Setting to property will
not replace flags, just add flags to set. So, jist document that, and
fixed obvious logick.
Neil Horman [Fri, 1 Jun 2012 16:59:24 +0000 (12:59 -0400)]
genl: modify genl_ctrl_resolve and friends to allow for module auto-loading
Generic netlink has the ability to autoload modules in response to a request for
a family. Currently libnl uses a GETFAMILY call with the NLM_F_DUMP flag to
list all the available families, but doing so neglects the possibility of an
autoloaded module. This patch modifies the genl code to probe the kernel for a
specific family rather than dumping a list of all the currenlty available ones,
making autoload work properly.
Signed-off-by: Neil Horman <nhorman@tuxdriver.com> CC: Thomas Graf <tgraf@redhat.com>
Thomas Graf [Fri, 1 Jun 2012 09:48:08 +0000 (11:48 +0200)]
genl: Support registration of families without depending on caches
Introduces the functions genl_register_family() and
genl_unregister_family() to register a Generic Netlink family
which does not implement a cachable type.
API users can direct received messages into genl_handle_msg() which
will validate the messages and call the callback functions defined
in the commands definition.
See test/test-genl.c for an example on how to use it.
Thomas Graf [Thu, 31 May 2012 11:37:57 +0000 (13:37 +0200)]
genl: Add genlmsg_user_hdr(), genlmsg_user_data(), and genlmsg_user_datalen()
These functions deprecate the function genlmsg_data() which did not
allow to specify the length of the user header. Use of the new API
will make code much clearer. The old function is still kept around
for backwards compatibility but marked deprecated in the API reference.
Andrew Collins [Sat, 26 May 2012 00:08:06 +0000 (18:08 -0600)]
correct HTB rtable/HZ calculations
The HTB implementation in libnl uses units of microseconds in a number
of places where it seems TC is expecting time in units of ticks, which
causes actual rates much higher than requested. Additionally, libnl
uses USER_HZ for calculating buffer and cbuffer sizes, which can
result in much larger buffers than necessary on systems with high
resolution timers.
Note that the TBF qdisc uses microseconds incorrectly in two spots as
well, I fixed this but did not test.
A C [Fri, 25 May 2012 19:45:45 +0000 (13:45 -0600)]
add fwmark mask support
The fw classifier allows a mask to be set, which is necessary for some
complex shaping/firewall scenarios. The attached patch adds support
for it to libnl.
Bushman, Jeff [Wed, 16 May 2012 15:50:25 +0000 (11:50 -0400)]
Fix for dumping objects to a buffer instead of file descriptor
Attached is a patch to fix two problems with dumping objects to a buffer in=
stead of a file descriptor.
One was a problem in detecting the end of the buffer in the newline code.
The other was a problem with clearing the whole buffer before printing each=
object.
Isaac [Sun, 13 May 2012 05:37:48 +0000 (22:37 -0700)]
FTBFS with musl libc: Missing includes
Hello,
libnl 3.2.9 does not build with musl libc, without patching.
I' using a current musl libc (http://www.etalabs.net/musl/)
with linux 2.6.32 headers.
At first there were a couple problems on the musl side, but those are
resolved.
However, I found some other issues:
First, two files were missing
#include <byteswap.h>:
lib/netfilter/log_msg.c
lib/netfilter/queue_msg.c
These files used __bswap_64 (which should be bswap_64), a macro
declared in byteswap.h
Second, I got this error after fixing that:
In file included from nf-queue.c:16:
./include/linux/netfilter.h:53: error: field in has incomplete type
./include/linux/netfilter.h:54: error: field in6 has incomplete type
I found that src/nf-queue.c is missing an
#include <netinet/in.h>
Attached is a patch which resolves these issues. I've tested with both
musl and glibc, and it builds cleanly on both.
Thomas Graf [Thu, 10 May 2012 10:03:59 +0000 (12:03 +0200)]
doc: documentation restructuring
- changes the modules hierarchy to better represent the set of libaries
- list the header file that needs to be included
- remove examples/doc from api ref that is included in the guide
- add references to the guide
- fix doxygen api linking for version 1.8.0
- readd doxygen mainpage to config file
- fix a couple of doxygen doc bugs
Justin Mayfield [Thu, 10 May 2012 03:08:30 +0000 (21:08 -0600)]
__str2flags fix
I found a minor bug in __str2flags where empty strings or short strings
will match all or many flags respectively. Basically the test needs to
ensure the test string is the same length as the table entry before
doing a strncasecmp to avoid doing just a prefix test.
Thomas Graf [Wed, 9 May 2012 13:02:40 +0000 (15:02 +0200)]
doc: add section about addressing
Adds a new section to netlink fundamentals explaining the common
addressing use cases and illustrates kernel->user, user->user,
and user->kernel communication.