Cong Wang [Fri, 8 Nov 2013 19:32:13 +0000 (11:32 -0800)]
do not compile some address families when not available
This fixes the following error while compiling libnl3 on CentOS5:
addr.c:1027: error: 'AF_RDS' undeclared here (not in a function)
addr.c:1033: error: 'AF_CAN' undeclared here (not in a function)
addr.c:1034: error: 'AF_TIPC' undeclared here (not in a function)
addr.c:1036: error: 'AF_IUCV' undeclared here (not in a function)
addr.c:1037: error: 'AF_RXRPC' undeclared here (not in a function)
addr.c:1038: error: 'AF_ISDN' undeclared here (not in a function)
addr.c:1039: error: 'AF_PHONET' undeclared here (not in a function)
make[2]: *** [addr.lo] Error 1
Signed-off-by: Cong Wang <xiyou.wangcong@gmail.com> Signed-off-by: Thomas Graf <tgraf@suug.ch>
Nicolas PLANEL [Thu, 10 Oct 2013 20:51:38 +0000 (20:51 +0000)]
route/link: fixup link->l_af_ops must be set for some kind of links
Lookup on IFLA_INFO_KIND information to setup the right link->l_af_ops callbacks
For example, if you setup a bridge this is avoid to trig IS_BRIDGE_LINK_ASSERT() on rtnl_link_bridge_get_flags() call.
line=<optimized out>, function=<optimized out>) at assert.c:94
0x7ffff5dc39a0 "rtnl_link_bridge_get_flags") at assert.c:103
Signed-off-by: Nicolas PLANEL <nicolas.planel@enovance.com> Signed-off-by: Thomas Graf <tgraf@suug.ch>
Julien BERNARD [Tue, 8 Oct 2013 11:04:30 +0000 (13:04 +0200)]
nl-link-set: Add --state option
I modified the nl-link-set.c file to be able to set the interface up or
down. I joined the patch.
With the new nl-link-set binary I can set the tap interface down.
I am working with Ubuntu 12.04 LTS 64bits and the libnl and libnl-route
packages built from libnl3-3.2.3 from ubuntu reprositories.
Teto [Wed, 2 Oct 2013 20:18:31 +0000 (22:18 +0200)]
Fixed ObjIterator for python3, fixed output of _color and added missing parameter to nl_cache_resync
Here are a few things I fixed and that provoked a python error.
I canno't answer to this thread but one solution I found while using
the python binding is to iterate over all and filter via python
http://list-archives.org/2013/09/09/libnl-lists-infradead-org/missing-feature-for-retrieving-cached-
address-objects/f/5031600704
python: remove use of PyArg_ParseTuple() for callback result
The message receive callback handler in the netlink api processes
the result object from the python callback. It used PyArg_ParseTuple()
to get the value, but this does not work as intended (see ref [1]).
Instead check the type and convert it accordingly.
The property name used in __str__ should be local_port
instead of localPort to get rid of the AttributeError.
>>> str(s)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File ".../netlink/core.py", line 172, in __str__
return 'nlsock<{0}>'.format(self.localPort)
AttributeError: 'Socket' object has no attribute 'localPort'
Signed-off-by: Arend van Spriel <arend@broadcom.com> Signed-off-by: Thomas Graf <tgraf@suug.ch>
python: allow callback function to be a class method
The callback functionality only worked for regular Python
functions. With this patch it also allows the callback
to be a class method (bounded or unbounded) as show in
example below.
class test_class(object):
def my_callback(self, msg, arg):
print('handling %s' % str(msg))
The initial commit adding netlink callback handling also introduced
memory leak issue. The python callback info was stored in an allocated
structure, but that was never freed.
Only exposing nl_cb_alloc() as is. nl_cb_get() is removed as it is
not very useful to use reference counting mechanism. Python uses
that itself internally. To deal properly with Python callback info
the function nl_cb_put() and nl_cb_clone() have a custom wrapper
taking care of Python reference counting.
This commit also adds a Callback python class using the netlink
callback functions.
Signed-off-by: Arend van Spriel <arend@broadcom.com> Signed-off-by: Thomas Graf <tgraf@suug.ch>
The application could use the same handler for multiple
nl_cb_type events. This patch stores the nl_cb_type in
the nl_cb struct during the callback. This allows the
application to obtain that information using the new
nl_cb_active_type() function. This way the callback
signature remains as is so existing applications are
not affected.
Signed-off-by: Arend van Spriel <arend@broadcom.com> Signed-off-by: Thomas Graf <tgraf@suug.ch>
netfilter/ct: support optional CTA_TIMESTAMP attribute
Recent kernels support conntrack time stamping, which is a helpful
feature to determine the duration of a flow without building a flow
cache in your user space application, just to keep the 'start' time of
your flow.
Timestamps are recorded with nanosecond resolution once this feature
is enabled.
This patch adds optional support for the CTA_TIMESTAMP, then
modifies the dump routine to write that info in a format similar
to /proc/net/nf_conntrack. This is an example output when using
NL_DUMP_LINE:
Joe Damato [Thu, 25 Jul 2013 23:36:16 +0000 (16:36 -0700)]
Add idiag-socket-details
This small program lists all sockets on the system seen by netlink and serves
as a simple example showing how to alloc an idiag msg cache and dump the
objects in it.
Joe Damato [Thu, 4 Jul 2013 01:40:58 +0000 (18:40 -0700)]
Add support for inet diag Netlink protocol.
- Inet diag allows users to gather low-level socket information.
- This library provides a higher-level API for creating inetdiag requests (via
idiagnl_connect and idiagnl_send_simple) and parsing the replies (via
idiagnl_msg_parse). A cache is also provided (via idiagnl_msg_alloc_cache).
- Request and message objects provide APIs for accessing and setting the
various properties of each.
- This library also allows the user to parse the inetdiag response attributes
which contain information about traffic class, TOS, congestion, socket
memory info, and more depending on the kernel version used.
- Includes doxygen documentation.
Provide wrapper function for generic netlink library functions. The
genlmsg_parse() function is handled similar to nla_parse_nested() so
it returns tuple with error code and dictionary of parsed attributes.
Signed-off-by: Arend van Spriel <arend@broadcom.com> Signed-off-by: Thomas Graf <tgraf@suug.ch>
add support functions for attributes and callback handlers
added support functions to access the netlink attributes and use
custom callback handlers. Most is wrapped as is, but there are
a couple of special cases handled.
1) void *nla_data(struct nlattr *);
The return value is changed to a Python byte array so it includes
the lenght of the data stream.
2) int nla_parse_nested(...);
This returns a tuple (err, dict). 'err' is the error code and 'dict'
is a dictionary with attribute identifier as key and value represents
a struct nlattr object.
3) macro nla_for_each_nested()
Provide nla_get_nested() which returns a Python list of struct nlattr
objects that is iterable.
4) allocate struct nla_policy array
Provide nla_policy_array() function that allocates consecutive space
in memory for struct nla_policy array entries. Each entry is put in
a Python list so the entry fields can be modified in Python. This
array object can be passed to the nla_parse_nested() function.
Signed-off-by: Arend van Spriel <arend@broadcom.com> Signed-off-by: Thomas Graf <tgraf@suug.ch>
Thomas Graf [Thu, 27 Jun 2013 16:29:17 +0000 (18:29 +0200)]
msg: Avoid returning a negative value for nlmsg_attrlen()
If a hdrlen was provided that was greather than the actual
message length, a negative attributes length would result.
This was typically happening for RTM_GETLINK requests where
we can get a away with a 4 bytes header on the request side
but the response would use a 16 bytes header.
This resulted in strange -8 bytes leftover debug messages.
Andrew Collins [Mon, 17 Jun 2013 21:58:43 +0000 (15:58 -0600)]
Handle -NLE_AGAIN in nl_cache_mngr_data_ready
Since commit "nl: Return -NLE_AGAIN if non-blocking socket would block",
nl_cache_mngr_data_ready returns -NLE_AGAIN to callers on non-blocking sockets.
Change it to consider -NLE_AGAIN as a non-error case as it is expected behavior
with the nl_recv changes.
Andrew Collins [Mon, 17 Jun 2013 21:24:05 +0000 (15:24 -0600)]
Default to comparing all attributes if no oo_id_attrs defined
Since commit: "cache pickup: Avoid duplicates during cache pickup",
nfnl_ct_alloc_cache no longer properly fills the cache, as it doesn't
define oo_id_attrs so all items are considered duplicates.
Instead of adding a ~0 oo_id_attrs to ct_obj, this changes
nl_object_identical to default to comparing all attributes if
neither oo_id_attrs_get or oo_id_attrs are provided.
Thomas Graf [Wed, 8 May 2013 11:52:27 +0000 (13:52 +0200)]
nl: Increase receive buffer size to 4 pages
Assuming that the kernel does not send more than a page is no longer valid,
and enabling MSG_PEEK'ing by default to figure out the exact message buffer
requirements can have a negative influence on the performance of existing
applications. Bumping the default receive buffer space to 4 pages seems
a sane default.
rtnl_link_af_unregister() attempts to write-lock info_lock twice
instead of releasing it before returning. It also will return with
info_lock write-locked if passed a NULL ops.
There seams to be an error in the calculation of needed space for the message in nla_reserve. The current size of the message is counted twice: Once in NLMSG_ALIGN, once in the condition below.
This causes nla_put_* calls to be rejected if the allocation size of the message has been strictly calculated by the caller.
Fix leak of cb if nl_socket_alloc_cb() failed to allocate socket
- each *_get() should have corresponding *_put(). That rule was broken in nl_socket_alloc()
- Also, check if cb is NULL in nl_socket_set_cb (calls BUG())
Passing a NULL pointer would cause a NULL pointer dereference within
nl_object_free().
Returning early on NULL pointer is the behavior free(3) and other
nl*_free() functions.
Thomas Graf [Tue, 2 Apr 2013 09:58:18 +0000 (11:58 +0200)]
nl: Return -NLE_AGAIN if non-blocking socket would block
Previously 0 was returned which gave the caller no chance of detecting
when a non-blocking socket would block. If a caller intends to never
see an error message it should utilize poll()/select() to only read
when the socket has pending data or information.
Reported-by: Holger Eitzenberger <holger@eitzenberger.org> Signed-off-by: Thomas Graf <tgraf@suug.ch>
Add ICMPv6 type, code and ID (if set) by using the already available
conntrack atttributes.
Currently the ICMPv6 conntrack objects in libnl are without type, code
and ID. This e. g. is the output of nl_object_dump() without the patch:
ipv6-icmp ::1 <-> ::1
id 0xdd0871f0 family inet6 timeout 30s <NOREPLY,SNAT_INIT,DNAT_INIT>
The attached patch tries to solve that. It then looks like
ipv6-icmp ::1 <-> ::1 icmp type 128 code 0 id 28253
id 0xdf3a11f0 family inet6 timeout 30s <SNAT_INIT,DNAT_INIT>
It is the 'small' approach, because it reuses the existing ICMP
attributes of the conntrack object (currently only used for IPv4).
This way I can avoid to add new _icmp6_get_, _icmp6_set_ and
_icmp6_test_ functions.
Signed-off-by: Holger Eitzenberger <holger@eitzenberger.org> Signed-off-by: Thomas Graf <tgraf@suug.ch>
Thomas Graf [Mon, 1 Apr 2013 09:16:34 +0000 (11:16 +0200)]
genl: Update mt_id of cache ops when resolving genl id
The cache layer uses the message type array stored in the cache
ops to lookup which cache a message belongs to. Update to the
the message array with the resolved generic netlink id to make
it compatible with the caching API.
Allows to use nl_cache_refill() and others for generic netlink
based caches with dynamic generic netlink ids.