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