2020-11-09 05:23:58 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
2013-11-07 03:38:08 +01:00
|
|
|
#pragma once
|
|
|
|
|
2013-11-07 03:39:32 +01:00
|
|
|
#include <linux/netlink.h>
|
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
#include "sd-netlink.h"
|
2013-11-13 23:18:20 +01:00
|
|
|
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "list.h"
|
2015-06-12 16:31:33 +02:00
|
|
|
#include "netlink-types.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "prioq.h"
|
2019-03-27 11:32:41 +01:00
|
|
|
#include "time-util.h"
|
2014-03-26 19:25:01 +01:00
|
|
|
|
2014-03-08 01:05:02 +01:00
|
|
|
#define RTNL_DEFAULT_TIMEOUT ((usec_t) (25 * USEC_PER_SEC))
|
2014-01-29 21:20:30 +01:00
|
|
|
|
|
|
|
#define RTNL_RQUEUE_MAX 64*1024
|
|
|
|
|
|
|
|
#define RTNL_CONTAINER_DEPTH 32
|
|
|
|
|
2013-11-13 23:18:20 +01:00
|
|
|
struct reply_callback {
|
2015-06-12 16:31:33 +02:00
|
|
|
sd_netlink_message_handler_t callback;
|
2013-11-13 23:18:20 +01:00
|
|
|
usec_t timeout;
|
|
|
|
uint64_t serial;
|
|
|
|
unsigned prioq_idx;
|
|
|
|
};
|
2013-10-21 20:12:52 +02:00
|
|
|
|
2013-11-30 01:24:29 +01:00
|
|
|
struct match_callback {
|
2015-06-12 16:31:33 +02:00
|
|
|
sd_netlink_message_handler_t callback;
|
2013-12-06 15:20:36 +01:00
|
|
|
uint16_t type;
|
2013-11-30 01:24:29 +01:00
|
|
|
|
|
|
|
LIST_FIELDS(struct match_callback, match_callbacks);
|
|
|
|
};
|
|
|
|
|
2018-10-15 09:06:12 +02:00
|
|
|
typedef enum NetlinkSlotType {
|
|
|
|
NETLINK_REPLY_CALLBACK,
|
|
|
|
NETLINK_MATCH_CALLBACK,
|
|
|
|
_NETLINK_SLOT_INVALID = -1,
|
|
|
|
} NetlinkSlotType;
|
|
|
|
|
|
|
|
struct sd_netlink_slot {
|
|
|
|
unsigned n_ref;
|
|
|
|
sd_netlink *netlink;
|
|
|
|
void *userdata;
|
|
|
|
sd_netlink_destroy_t destroy_callback;
|
|
|
|
NetlinkSlotType type:2;
|
|
|
|
|
|
|
|
bool floating:1;
|
2018-10-15 10:49:53 +02:00
|
|
|
char *description;
|
2018-10-15 09:06:12 +02:00
|
|
|
|
|
|
|
LIST_FIELDS(sd_netlink_slot, slots);
|
|
|
|
|
|
|
|
union {
|
|
|
|
struct reply_callback reply_callback;
|
|
|
|
struct match_callback match_callback;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
struct sd_netlink {
|
2019-03-01 18:31:45 +01:00
|
|
|
unsigned n_ref;
|
2013-10-21 20:12:52 +02:00
|
|
|
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
union {
|
|
|
|
struct sockaddr sa;
|
|
|
|
struct sockaddr_nl nl;
|
|
|
|
} sockaddr;
|
|
|
|
|
2017-12-18 15:17:06 +01:00
|
|
|
int protocol;
|
|
|
|
|
2015-10-15 17:59:10 +02:00
|
|
|
Hashmap *broadcast_group_refs;
|
|
|
|
bool broadcast_group_dont_leave:1; /* until we can rely on 4.2 */
|
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
sd_netlink_message **rqueue;
|
2013-11-12 22:37:51 +01:00
|
|
|
unsigned rqueue_size;
|
2014-04-10 19:00:30 +02:00
|
|
|
size_t rqueue_allocated;
|
2013-11-12 22:37:51 +01:00
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
sd_netlink_message **rqueue_partial;
|
2014-04-17 21:32:25 +02:00
|
|
|
unsigned rqueue_partial_size;
|
|
|
|
size_t rqueue_partial_allocated;
|
|
|
|
|
2014-04-14 17:20:51 +02:00
|
|
|
struct nlmsghdr *rbuffer;
|
|
|
|
size_t rbuffer_allocated;
|
|
|
|
|
2013-11-12 22:37:51 +01:00
|
|
|
bool processing:1;
|
|
|
|
|
2013-10-29 21:20:25 +01:00
|
|
|
uint32_t serial;
|
|
|
|
|
2013-11-13 23:18:20 +01:00
|
|
|
struct Prioq *reply_callbacks_prioq;
|
|
|
|
Hashmap *reply_callbacks;
|
|
|
|
|
2013-11-30 01:24:29 +01:00
|
|
|
LIST_HEAD(struct match_callback, match_callbacks);
|
|
|
|
|
2018-10-15 09:06:12 +02:00
|
|
|
LIST_HEAD(sd_netlink_slot, slots);
|
|
|
|
|
2013-10-29 17:38:31 +01:00
|
|
|
pid_t original_pid;
|
2013-11-14 19:23:39 +01:00
|
|
|
|
|
|
|
sd_event_source *io_event_source;
|
|
|
|
sd_event_source *time_event_source;
|
2013-12-12 22:21:25 +01:00
|
|
|
sd_event_source *exit_event_source;
|
2013-11-14 19:23:39 +01:00
|
|
|
sd_event *event;
|
2019-07-23 08:54:06 +02:00
|
|
|
|
|
|
|
Hashmap *genl_family_to_nlmsg_type;
|
|
|
|
Hashmap *nlmsg_type_to_genl_family;
|
2013-10-21 20:12:52 +02:00
|
|
|
};
|
2013-10-29 16:35:37 +01:00
|
|
|
|
2015-06-26 00:02:55 +02:00
|
|
|
struct netlink_attribute {
|
2015-07-02 15:18:29 +02:00
|
|
|
size_t offset; /* offset from hdr to attribute */
|
2015-06-23 13:13:20 +02:00
|
|
|
bool nested:1;
|
|
|
|
bool net_byteorder:1;
|
2015-06-26 00:02:55 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct netlink_container {
|
|
|
|
const struct NLTypeSystem *type_system; /* the type system of the container */
|
|
|
|
size_t offset; /* offset from hdr to the start of the container */
|
|
|
|
struct netlink_attribute *attributes;
|
|
|
|
unsigned short n_attributes; /* number of attributes in container */
|
|
|
|
};
|
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
struct sd_netlink_message {
|
2019-03-01 18:31:45 +01:00
|
|
|
unsigned n_ref;
|
2013-11-12 22:37:51 +01:00
|
|
|
|
2017-12-18 15:17:06 +01:00
|
|
|
int protocol;
|
|
|
|
|
2014-01-29 21:20:30 +01:00
|
|
|
struct nlmsghdr *hdr;
|
2015-06-26 00:02:55 +02:00
|
|
|
struct netlink_container containers[RTNL_CONTAINER_DEPTH];
|
2014-01-29 21:20:30 +01:00
|
|
|
unsigned n_containers; /* number of containers */
|
|
|
|
bool sealed:1;
|
2015-03-13 16:12:57 +01:00
|
|
|
bool broadcast:1;
|
2014-04-13 21:37:53 +02:00
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
sd_netlink_message *next; /* next in a chain of multi-part messages */
|
2014-01-29 21:20:30 +01:00
|
|
|
};
|
2013-12-06 15:13:34 +01:00
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
int message_new(sd_netlink *rtnl, sd_netlink_message **ret, uint16_t type);
|
2015-06-13 21:10:39 +02:00
|
|
|
int message_new_empty(sd_netlink *rtnl, sd_netlink_message **ret);
|
2013-12-06 15:13:34 +01:00
|
|
|
|
2017-12-18 15:17:06 +01:00
|
|
|
int netlink_open_family(sd_netlink **ret, int family);
|
|
|
|
|
2015-06-13 21:25:05 +02:00
|
|
|
int socket_open(int family);
|
|
|
|
int socket_bind(sd_netlink *nl);
|
2015-10-15 17:59:10 +02:00
|
|
|
int socket_broadcast_group_ref(sd_netlink *nl, unsigned group);
|
|
|
|
int socket_broadcast_group_unref(sd_netlink *nl, unsigned group);
|
2015-06-12 16:31:33 +02:00
|
|
|
int socket_write_message(sd_netlink *nl, sd_netlink_message *m);
|
2020-12-18 05:11:06 +01:00
|
|
|
int socket_writev_message(sd_netlink *nl, sd_netlink_message **m, size_t msgcount);
|
2015-06-12 16:31:33 +02:00
|
|
|
int socket_read_message(sd_netlink *nl);
|
2014-04-10 19:40:48 +02:00
|
|
|
|
2015-06-12 16:31:33 +02:00
|
|
|
int rtnl_rqueue_make_room(sd_netlink *rtnl);
|
|
|
|
int rtnl_rqueue_partial_make_room(sd_netlink *rtnl);
|
2013-11-13 23:18:20 +01:00
|
|
|
|
|
|
|
/* Make sure callbacks don't destroy the rtnl connection */
|
tree-wide: expose "p"-suffix unref calls in public APIs to make gcc cleanup easy
GLIB has recently started to officially support the gcc cleanup
attribute in its public API, hence let's do the same for our APIs.
With this patch we'll define an xyz_unrefp() call for each public
xyz_unref() call, to make it easy to use inside a
__attribute__((cleanup())) expression. Then, all code is ported over to
make use of this.
The new calls are also documented in the man pages, with examples how to
use them (well, I only added docs where the _unref() call itself already
had docs, and the examples, only cover sd_bus_unrefp() and
sd_event_unrefp()).
This also renames sd_lldp_free() to sd_lldp_unref(), since that's how we
tend to call our destructors these days.
Note that this defines no public macro that wraps gcc's attribute and
makes it easier to use. While I think it's our duty in the library to
make our stuff easy to use, I figure it's not our duty to make gcc's own
features easy to use on its own. Most likely, client code which wants to
make use of this should define its own:
#define _cleanup_(function) __attribute__((cleanup(function)))
Or similar, to make the gcc feature easier to use.
Making this logic public has the benefit that we can remove three header
files whose only purpose was to define these functions internally.
See #2008.
2015-11-27 19:13:45 +01:00
|
|
|
#define NETLINK_DONT_DESTROY(rtnl) \
|
|
|
|
_cleanup_(sd_netlink_unrefp) _unused_ sd_netlink *_dont_destroy_##rtnl = sd_netlink_ref(rtnl)
|