]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/nl.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
11 #include "memory_utils.h"
14 * Use this as a good size to allocate generic netlink messages
17 #define PAGE_SIZE 4096
19 #define NLMSG_GOOD_SIZE (2*PAGE_SIZE)
20 #define NLMSG_TAIL(nmsg) ((struct rtattr *) (((void *) (nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len)))
21 #define NLA_DATA(na) ((void *)((char*)(na) + NLA_HDRLEN))
22 #define NLA_NEXT_ATTR(attr) ((void *)((char *)attr) + NLA_ALIGN(attr->nla_len))
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.
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
37 struct sockaddr_nl local
;
38 struct sockaddr_nl peer
;
41 #define NL_HANDLER_INIT { .fd = -EBADF }
44 * struct nlmsg : the netlink message structure. This message is to be used to
45 * be allocated with netlink_alloc.
47 * @nlmsghdr: a pointer to a netlink message header
48 * @cap: capacity of the netlink message, this is the initially allocated size
49 * and later operations (e.g. reserve and put) can not exceed this limit.
52 struct nlmsghdr
*nlmsghdr
;
57 * netlink_open : open a netlink socket, the function will
58 * fill the handler with the right value
60 * @handler: a netlink handler to be used all along the netlink
62 * @protocol: specify the protocol to be used when opening the
65 * Return 0 on success, < 0 otherwise
67 __hidden
extern int netlink_open(struct nl_handler
*handler
, int protocol
);
70 * netlink_close : close a netlink socket, after this call,
71 * the handler is no longer valid
73 * @handler: a handler to the netlink socket
75 __hidden
extern void netlink_close(struct nl_handler
*handler
);
76 define_cleanup_function(struct nl_handler
*, netlink_close
);
79 * netlink_rcv : receive a netlink message from the kernel.
80 * It is up to the caller to manage the allocation of the
83 * @handler: a handler to the netlink socket
84 * @nlmsg: a netlink message
86 * Returns 0 on success, < 0 otherwise
88 __hidden
extern int netlink_rcv(struct nl_handler
*handler
, struct nlmsg
*nlmsg
);
89 __hidden
extern int __netlink_recv(struct nl_handler
*handler
, struct nlmsghdr
*nlmsg
);
92 * netlink_send: send a netlink message to the kernel. It is up
93 * to the caller to manage the allocate of the netlink message
95 * @handler: a handler to the netlink socket
96 * @nlmsg: a netlink message
98 * Returns 0 on success, < 0 otherwise
100 __hidden
extern int netlink_send(struct nl_handler
*handler
, struct nlmsg
*nlmsg
);
101 __hidden
extern int __netlink_send(struct nl_handler
*handler
, struct nlmsghdr
*nlmsg
);
104 * netlink_transaction: send a request to the kernel and read the response.
105 * This is useful for transactional protocol. It is up to the caller
106 * to manage the allocation of the netlink message.
108 * @handler: a handler to a opened netlink socket
109 * @request: a netlink message pointer containing the request
110 * @answer: a netlink message pointer to receive the result
112 * Returns 0 on success, < 0 otherwise
114 __hidden
extern int netlink_transaction(struct nl_handler
*handler
, struct nlmsg
*request
,
115 struct nlmsg
*answer
);
116 __hidden
extern int __netlink_transaction(struct nl_handler
*handler
, struct nlmsghdr
*request
,
117 struct nlmsghdr
*answer
);
120 * nla_put_string: copy a null terminated string to a netlink message
123 * @nlmsg: the netlink message to be filled
124 * @attr: the attribute name of the string
125 * @string: a null terminated string to be copied to the netlink message
127 * Returns 0 on success, < 0 otherwise
129 __hidden
extern int nla_put_string(struct nlmsg
*nlmsg
, int attr
, const char *string
);
132 * nla_put_buffer: copy a buffer with a specified size to a netlink
135 * @nlmsg: the netlink message to be filled
136 * @attr: the attribute name of the string
137 * @data: a pointer to a buffer
138 * @size: the size of the buffer
140 * Returns 0 on success, < 0 otherwise
142 __hidden
extern int nla_put_buffer(struct nlmsg
*nlmsg
, int attr
, const void *data
, size_t size
);
145 * nla_put_u32: copy an integer to a netlink message attribute
147 * @nlmsg: the netlink message to be filled
148 * @attr: the attribute name of the integer
149 * @string: an integer to be copied to the netlink message
151 * Returns 0 on success, < 0 otherwise
153 __hidden
extern int nla_put_u32(struct nlmsg
*nlmsg
, int attr
, int value
);
156 * nla_put_u16: copy an integer to a netlink message attribute
158 * @nlmsg: the netlink message to be filled
159 * @attr: the attribute name of the unsigned 16-bit value
160 * @value: 16-bit attribute data value to be copied to the netlink message
162 * Returns 0 on success, < 0 otherwise
164 __hidden
extern int nla_put_u16(struct nlmsg
*nlmsg
, int attr
, unsigned short value
);
167 * nla_put_attr: add an attribute name to a netlink
169 * @nlmsg: the netlink message to be filled
170 * @attr: the attribute name of the integer
172 * Returns 0 on success, < 0 otherwise
174 __hidden
extern int nla_put_attr(struct nlmsg
*nlmsg
, int attr
);
177 * nla_begin_nested: begin the nesting attribute
179 * @nlmsg: the netlink message to be filled
180 * @attr: the netsted attribute name
182 * Returns current nested pointer to be reused
185 __hidden
extern struct rtattr
*nla_begin_nested(struct nlmsg
*nlmsg
, int attr
);
188 * nla_end_nested: end the nesting attribute
190 * @nlmsg: the netlink message
191 * @nested: the nested pointer
193 * Returns the current
195 __hidden
extern void nla_end_nested(struct nlmsg
*nlmsg
, struct rtattr
*attr
);
198 * nlmsg_allocate : allocate a netlink message. The netlink format message
199 * is a header, a padding, a payload and a padding again.
200 * When a netlink message is allocated, the size specify the
201 * payload we want. So the real size of the allocated message
202 * is sizeof(header) + sizeof(padding) + payloadsize + sizeof(padding),
203 * in other words, the function will allocate more than specified. When
204 * the buffer is allocated, the content is zeroed.
205 * The function will also fill the field nlmsg_len with NLMSG_HDRLEN.
206 * If the allocation must be for the specified size, just use malloc.
208 * @size: the capacity of the payload to be allocated
210 * Returns a pointer to the newly allocated netlink message, NULL otherwise
212 __hidden
extern struct nlmsg
*nlmsg_alloc(size_t size
);
215 * nlmsg_alloc_reserve: like nlmsg_alloc(), but reserve the whole payload
216 * after allocated, that is, the field nlmsg_len be set to the capacity
217 * of nlmsg. Often used to allocate a message for the reply.
219 * @size: the capacity of the payload to be allocated.
221 __hidden
extern struct nlmsg
*nlmsg_alloc_reserve(size_t size
);
224 * Reserve room for additional data at the tail of a netlink message
226 * @nlmsg: the netlink message
227 * @len: length of additional data to reserve room for
229 * Returns a pointer to newly reserved room or NULL
231 __hidden
extern void *nlmsg_reserve(struct nlmsg
*nlmsg
, size_t len
);
234 * nlmsg_free : free a previously allocate message
236 * @nlmsg: the netlink message to be freed
238 __hidden
extern void nlmsg_free(struct nlmsg
*nlmsg
);
239 define_cleanup_function(struct nlmsg
*, nlmsg_free
);
242 * nlmsg_data : returns a pointer to the data contained in the netlink message
244 * @nlmsg : the netlink message to get the data
246 * Returns a pointer to the netlink data or NULL if there is no data
248 __hidden
extern void *nlmsg_data(struct nlmsg
*nlmsg
);
250 __hidden
extern int addattr(struct nlmsghdr
*n
, size_t maxlen
, int type
,
251 const void *data
, size_t alen
);