]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/nl.h
start: check event loop type before closing fd
[mirror_lxc.git] / src / lxc / nl.h
CommitLineData
cc73685d
CB
1/* SPDX-License-Identifier: LGPL-2.1+ */
2
f1a4a029
ÇO
3#ifndef __LXC_NL_H
4#define __LXC_NL_H
0ad19a3f 5
1160ce89
CB
6#include "config.h"
7
cc6119a0
CB
8#include <stdio.h>
9
6822ba9b 10#include "compiler.h"
d16bda44
CB
11#include "memory_utils.h"
12
0ad19a3f 13/*
14 * Use this as a good size to allocate generic netlink messages
15 */
16#ifndef PAGE_SIZE
17#define PAGE_SIZE 4096
18#endif
19#define NLMSG_GOOD_SIZE (2*PAGE_SIZE)
eab15c1e 20#define NLMSG_TAIL(nmsg) ((struct rtattr *) (((void *) (nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len)))
0ad19a3f 21#define NLA_DATA(na) ((void *)((char*)(na) + NLA_HDRLEN))
22#define NLA_NEXT_ATTR(attr) ((void *)((char *)attr) + NLA_ALIGN(attr->nla_len))
23
24/*
25 * struct nl_handler : the handler for netlink sockets, this structure
26 * is used all along the netlink socket life cycle to specify the
27 * netlink socket to be used.
f79d43bb 28 *
0ad19a3f 29 * @fd: the file descriptor of the netlink socket
30 * @seq: the sequence number of the netlink messages
31 * @local: the bind address
32 * @peer: the peer address
33 */
34struct nl_handler {
d028235d 35 int fd;
0ad19a3f 36 int seq;
d028235d
SG
37 struct sockaddr_nl local;
38 struct sockaddr_nl peer;
0ad19a3f 39};
40
41/*
06f976ca 42 * struct nlmsg : the netlink message structure. This message is to be used to
0ad19a3f 43 * be allocated with netlink_alloc.
06f976ca
SZ
44 *
45 * @nlmsghdr: a pointer to a netlink message header
46 * @cap: capacity of the netlink message, this is the initially allocated size
47 * and later operations (e.g. reserve and put) can not exceed this limit.
0ad19a3f 48 */
49struct nlmsg {
06f976ca
SZ
50 struct nlmsghdr *nlmsghdr;
51 ssize_t cap;
0ad19a3f 52};
53
54/*
55 * netlink_open : open a netlink socket, the function will
56 * fill the handler with the right value
57 *
58 * @handler: a netlink handler to be used all along the netlink
59 * socket life cycle
60 * @protocol: specify the protocol to be used when opening the
61 * netlink socket
62 *
63 * Return 0 on success, < 0 otherwise
64 */
6822ba9b 65__hidden extern int netlink_open(struct nl_handler *handler, int protocol);
0ad19a3f 66
67/*
f79d43bb 68 * netlink_close : close a netlink socket, after this call,
0ad19a3f 69 * the handler is no longer valid
70 *
71 * @handler: a handler to the netlink socket
0ad19a3f 72 */
6822ba9b 73__hidden extern void netlink_close(struct nl_handler *handler);
d16bda44 74define_cleanup_function(struct nl_handler *, netlink_close);
0ad19a3f 75
76/*
f79d43bb
SG
77 * netlink_rcv : receive a netlink message from the kernel.
78 * It is up to the caller to manage the allocation of the
0ad19a3f 79 * netlink message
80 *
81 * @handler: a handler to the netlink socket
82 * @nlmsg: a netlink message
83 *
84 * Returns 0 on success, < 0 otherwise
85 */
6822ba9b
CB
86__hidden extern int netlink_rcv(struct nl_handler *handler, struct nlmsg *nlmsg);
87__hidden extern int __netlink_recv(struct nl_handler *handler, struct nlmsghdr *nlmsg);
0ad19a3f 88
89/*
90 * netlink_send: send a netlink message to the kernel. It is up
91 * to the caller to manage the allocate of the netlink message
92 *
93 * @handler: a handler to the netlink socket
94 * @nlmsg: a netlink message
95 *
96 * Returns 0 on success, < 0 otherwise
97 */
6822ba9b
CB
98__hidden extern int netlink_send(struct nl_handler *handler, struct nlmsg *nlmsg);
99__hidden extern int __netlink_send(struct nl_handler *handler, struct nlmsghdr *nlmsg);
0ad19a3f 100
101/*
f79d43bb
SG
102 * netlink_transaction: send a request to the kernel and read the response.
103 * This is useful for transactional protocol. It is up to the caller
0ad19a3f 104 * to manage the allocation of the netlink message.
105 *
106 * @handler: a handler to a opened netlink socket
107 * @request: a netlink message pointer containing the request
108 * @answer: a netlink message pointer to receive the result
109 *
110 * Returns 0 on success, < 0 otherwise
111 */
6822ba9b
CB
112__hidden extern int netlink_transaction(struct nl_handler *handler, struct nlmsg *request,
113 struct nlmsg *answer);
114__hidden extern int __netlink_transaction(struct nl_handler *handler, struct nlmsghdr *request,
115 struct nlmsghdr *answer);
0ad19a3f 116
117/*
f79d43bb 118 * nla_put_string: copy a null terminated string to a netlink message
0ad19a3f 119 * attribute
120 *
121 * @nlmsg: the netlink message to be filled
122 * @attr: the attribute name of the string
123 * @string: a null terminated string to be copied to the netlink message
124 *
125 * Returns 0 on success, < 0 otherwise
126 */
59eac805 127__hidden extern int nla_put_string(struct nlmsg *nlmsg, int attr, const char *string);
0ad19a3f 128
129/*
130 * nla_put_buffer: copy a buffer with a specified size to a netlink
131 * message attribute
132 *
133 * @nlmsg: the netlink message to be filled
134 * @attr: the attribute name of the string
135 * @data: a pointer to a buffer
136 * @size: the size of the buffer
137 *
138 * Returns 0 on success, < 0 otherwise
139 */
59eac805 140__hidden extern int nla_put_buffer(struct nlmsg *nlmsg, int attr, const void *data, size_t size);
0ad19a3f 141
142/*
143 * nla_put_u32: copy an integer to a netlink message attribute
144 *
145 * @nlmsg: the netlink message to be filled
146 * @attr: the attribute name of the integer
147 * @string: an integer to be copied to the netlink message
148 *
149 * Returns 0 on success, < 0 otherwise
150 */
59eac805 151__hidden extern int nla_put_u32(struct nlmsg *nlmsg, int attr, int value);
0ad19a3f 152
9ddaf3bf
JHS
153/*
154 * nla_put_u16: copy an integer to a netlink message attribute
155 *
156 * @nlmsg: the netlink message to be filled
157 * @attr: the attribute name of the unsigned 16-bit value
158 * @value: 16-bit attribute data value to be copied to the netlink message
159 *
160 * Returns 0 on success, < 0 otherwise
161 */
59eac805 162__hidden extern int nla_put_u16(struct nlmsg *nlmsg, int attr, unsigned short value);
9ddaf3bf 163
0ad19a3f 164/*
f79d43bb 165 * nla_put_attr: add an attribute name to a netlink
0ad19a3f 166 *
167 * @nlmsg: the netlink message to be filled
168 * @attr: the attribute name of the integer
169 *
170 * Returns 0 on success, < 0 otherwise
171 */
59eac805 172__hidden extern int nla_put_attr(struct nlmsg *nlmsg, int attr);
0ad19a3f 173
174/*
175 * nla_begin_nested: begin the nesting attribute
176 *
177 * @nlmsg: the netlink message to be filled
f79d43bb 178 * @attr: the netsted attribute name
0ad19a3f 179 *
180 * Returns current nested pointer to be reused
181 * to nla_end_nested.
182 */
6822ba9b 183__hidden extern struct rtattr *nla_begin_nested(struct nlmsg *nlmsg, int attr);
0ad19a3f 184
185/*
186 * nla_end_nested: end the nesting attribute
187 *
188 * @nlmsg: the netlink message
189 * @nested: the nested pointer
190 *
f79d43bb 191 * Returns the current
0ad19a3f 192 */
6822ba9b 193__hidden extern void nla_end_nested(struct nlmsg *nlmsg, struct rtattr *attr);
0ad19a3f 194
195/*
f79d43bb 196 * nlmsg_allocate : allocate a netlink message. The netlink format message
0ad19a3f 197 * is a header, a padding, a payload and a padding again.
f79d43bb 198 * When a netlink message is allocated, the size specify the
0ad19a3f 199 * payload we want. So the real size of the allocated message
200 * is sizeof(header) + sizeof(padding) + payloadsize + sizeof(padding),
f79d43bb 201 * in other words, the function will allocate more than specified. When
0ad19a3f 202 * the buffer is allocated, the content is zeroed.
06f976ca 203 * The function will also fill the field nlmsg_len with NLMSG_HDRLEN.
0ad19a3f 204 * If the allocation must be for the specified size, just use malloc.
205 *
06f976ca 206 * @size: the capacity of the payload to be allocated
0ad19a3f 207 *
208 * Returns a pointer to the newly allocated netlink message, NULL otherwise
209 */
6822ba9b 210__hidden extern struct nlmsg *nlmsg_alloc(size_t size);
0ad19a3f 211
06f976ca
SZ
212/*
213 * nlmsg_alloc_reserve: like nlmsg_alloc(), but reserve the whole payload
214 * after allocated, that is, the field nlmsg_len be set to the capacity
215 * of nlmsg. Often used to allocate a message for the reply.
216 *
217 * @size: the capacity of the payload to be allocated.
218 */
6822ba9b 219__hidden extern struct nlmsg *nlmsg_alloc_reserve(size_t size);
06f976ca
SZ
220
221/*
222 * Reserve room for additional data at the tail of a netlink message
223 *
224 * @nlmsg: the netlink message
225 * @len: length of additional data to reserve room for
226 *
227 * Returns a pointer to newly reserved room or NULL
228 */
6822ba9b 229__hidden extern void *nlmsg_reserve(struct nlmsg *nlmsg, size_t len);
06f976ca 230
0ad19a3f 231/*
232 * nlmsg_free : free a previously allocate message
233 *
234 * @nlmsg: the netlink message to be freed
235 */
6822ba9b 236__hidden extern void nlmsg_free(struct nlmsg *nlmsg);
d16bda44 237define_cleanup_function(struct nlmsg *, nlmsg_free);
0ad19a3f 238
239/*
240 * nlmsg_data : returns a pointer to the data contained in the netlink message
f79d43bb 241 *
0ad19a3f 242 * @nlmsg : the netlink message to get the data
243 *
244 * Returns a pointer to the netlink data or NULL if there is no data
245 */
6822ba9b 246__hidden extern void *nlmsg_data(struct nlmsg *nlmsg);
0ad19a3f 247
6822ba9b
CB
248__hidden extern int addattr(struct nlmsghdr *n, size_t maxlen, int type,
249 const void *data, size_t alen);
0ad19a3f 250
251#endif