]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libsystemd/sd-netlink/netlink-internal.h
tree-wide: drop 'This file is part of systemd' blurb
[thirdparty/systemd.git] / src / libsystemd / sd-netlink / netlink-internal.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 #pragma once
3
4 /***
5 Copyright 2013 Tom Gundersen <teg@jklm.no>
6 ***/
7
8 #include <linux/netlink.h>
9
10 #include "sd-netlink.h"
11
12 #include "list.h"
13 #include "netlink-types.h"
14 #include "prioq.h"
15 #include "refcnt.h"
16
17 #define RTNL_DEFAULT_TIMEOUT ((usec_t) (25 * USEC_PER_SEC))
18
19 #define RTNL_WQUEUE_MAX 1024
20 #define RTNL_RQUEUE_MAX 64*1024
21
22 #define RTNL_CONTAINER_DEPTH 32
23
24 struct reply_callback {
25 sd_netlink_message_handler_t callback;
26 void *userdata;
27 usec_t timeout;
28 uint64_t serial;
29 unsigned prioq_idx;
30 };
31
32 struct match_callback {
33 sd_netlink_message_handler_t callback;
34 uint16_t type;
35 void *userdata;
36
37 LIST_FIELDS(struct match_callback, match_callbacks);
38 };
39
40 struct sd_netlink {
41 RefCount n_ref;
42
43 int fd;
44
45 union {
46 struct sockaddr sa;
47 struct sockaddr_nl nl;
48 } sockaddr;
49
50 int protocol;
51
52 Hashmap *broadcast_group_refs;
53 bool broadcast_group_dont_leave:1; /* until we can rely on 4.2 */
54
55 sd_netlink_message **rqueue;
56 unsigned rqueue_size;
57 size_t rqueue_allocated;
58
59 sd_netlink_message **rqueue_partial;
60 unsigned rqueue_partial_size;
61 size_t rqueue_partial_allocated;
62
63 struct nlmsghdr *rbuffer;
64 size_t rbuffer_allocated;
65
66 bool processing:1;
67
68 uint32_t serial;
69
70 struct Prioq *reply_callbacks_prioq;
71 Hashmap *reply_callbacks;
72
73 LIST_HEAD(struct match_callback, match_callbacks);
74
75 pid_t original_pid;
76
77 sd_event_source *io_event_source;
78 sd_event_source *time_event_source;
79 sd_event_source *exit_event_source;
80 sd_event *event;
81 };
82
83 struct netlink_attribute {
84 size_t offset; /* offset from hdr to attribute */
85 bool nested:1;
86 bool net_byteorder:1;
87 };
88
89 struct netlink_container {
90 const struct NLTypeSystem *type_system; /* the type system of the container */
91 size_t offset; /* offset from hdr to the start of the container */
92 struct netlink_attribute *attributes;
93 unsigned short n_attributes; /* number of attributes in container */
94 };
95
96 struct sd_netlink_message {
97 RefCount n_ref;
98
99 sd_netlink *rtnl;
100
101 int protocol;
102
103 struct nlmsghdr *hdr;
104 struct netlink_container containers[RTNL_CONTAINER_DEPTH];
105 unsigned n_containers; /* number of containers */
106 bool sealed:1;
107 bool broadcast:1;
108
109 sd_netlink_message *next; /* next in a chain of multi-part messages */
110 };
111
112 int message_new(sd_netlink *rtnl, sd_netlink_message **ret, uint16_t type);
113 int message_new_empty(sd_netlink *rtnl, sd_netlink_message **ret);
114
115 int netlink_open_family(sd_netlink **ret, int family);
116
117 int socket_open(int family);
118 int socket_bind(sd_netlink *nl);
119 int socket_broadcast_group_ref(sd_netlink *nl, unsigned group);
120 int socket_broadcast_group_unref(sd_netlink *nl, unsigned group);
121 int socket_write_message(sd_netlink *nl, sd_netlink_message *m);
122 int socket_read_message(sd_netlink *nl);
123
124 int rtnl_rqueue_make_room(sd_netlink *rtnl);
125 int rtnl_rqueue_partial_make_room(sd_netlink *rtnl);
126
127 /* Make sure callbacks don't destroy the rtnl connection */
128 #define NETLINK_DONT_DESTROY(rtnl) \
129 _cleanup_(sd_netlink_unrefp) _unused_ sd_netlink *_dont_destroy_##rtnl = sd_netlink_ref(rtnl)