1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2017 6WIND S.A.
3 * Copyright 2017 Mellanox Technologies, Ltd
8 #include <linux/netlink.h>
10 #include <sys/socket.h>
13 #include <rte_malloc.h>
14 #include <tap_netlink.h>
15 #include <rte_random.h>
18 /* Must be quite large to support dumping a huge list of QDISC or filters. */
19 #define BUF_SIZE (32 * 1024) /* Size of the buffer to receive kernel messages */
20 #define SNDBUF_SIZE 32768 /* Send buffer size for the netlink socket */
21 #define RCVBUF_SIZE 32768 /* Receive buffer size for the netlink socket */
25 struct nested_tail *prev;
29 * Initialize a netlink socket for communicating with the kernel.
32 * Set it to a netlink group value (e.g. RTMGRP_LINK) to receive messages for
33 * specific netlink multicast groups. Otherwise, no subscription will be made.
36 * netlink socket file descriptor on success, -1 otherwise.
39 tap_nl_init(uint32_t nl_groups)
41 int fd, sndbuf_size = SNDBUF_SIZE, rcvbuf_size = RCVBUF_SIZE;
42 struct sockaddr_nl local = {
43 .nl_family = AF_NETLINK,
44 .nl_groups = nl_groups,
47 fd = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE);
49 TAP_LOG(ERR, "Unable to create a netlink socket");
52 if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &sndbuf_size, sizeof(int))) {
53 TAP_LOG(ERR, "Unable to set socket buffer send size");
57 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf_size, sizeof(int))) {
58 TAP_LOG(ERR, "Unable to set socket buffer receive size");
62 if (bind(fd, (struct sockaddr *)&local, sizeof(local)) < 0) {
63 TAP_LOG(ERR, "Unable to bind to the netlink socket");
71 * Clean up a netlink socket once all communicating with the kernel is finished.
74 * The netlink socket file descriptor used for communication.
77 * 0 on success, -1 otherwise.
80 tap_nl_final(int nlsk_fd)
83 TAP_LOG(ERR, "Failed to close netlink socket: %s (%d)",
84 strerror(errno), errno);
91 * Send a message to the kernel on the netlink socket.
94 * The netlink socket file descriptor used for communication.
96 * The netlink message send to the kernel.
99 * the number of sent bytes on success, -1 otherwise.
102 tap_nl_send(int nlsk_fd, struct nlmsghdr *nh)
104 /* man 7 netlink EXAMPLE */
105 struct sockaddr_nl sa = {
106 .nl_family = AF_NETLINK,
110 .iov_len = nh->nlmsg_len,
112 struct msghdr msg = {
114 .msg_namelen = sizeof(sa),
120 nh->nlmsg_pid = 0; /* communication with the kernel uses pid 0 */
121 nh->nlmsg_seq = (uint32_t)rte_rand();
122 send_bytes = sendmsg(nlsk_fd, &msg, 0);
123 if (send_bytes < 0) {
124 TAP_LOG(ERR, "Failed to send netlink message: %s (%d)",
125 strerror(errno), errno);
132 * Check that the kernel sends an appropriate ACK in response
133 * to an tap_nl_send().
136 * The netlink socket file descriptor used for communication.
139 * 0 on success, -1 otherwise with errno set.
142 tap_nl_recv_ack(int nlsk_fd)
144 return tap_nl_recv(nlsk_fd, NULL, NULL);
148 * Receive a message from the kernel on the netlink socket, following an
152 * The netlink socket file descriptor used for communication.
154 * The callback function to call for each netlink message received.
155 * @param[in, out] arg
156 * Custom arguments for the callback.
159 * 0 on success, -1 otherwise with errno set.
162 tap_nl_recv(int nlsk_fd, int (*cb)(struct nlmsghdr *, void *arg), void *arg)
164 /* man 7 netlink EXAMPLE */
165 struct sockaddr_nl sa;
169 .iov_len = sizeof(buf),
171 struct msghdr msg = {
173 .msg_namelen = sizeof(sa),
175 /* One message at a time */
185 recv_bytes = recvmsg(nlsk_fd, &msg, 0);
188 for (nh = (struct nlmsghdr *)buf;
189 NLMSG_OK(nh, (unsigned int)recv_bytes);
190 nh = NLMSG_NEXT(nh, recv_bytes)) {
191 if (nh->nlmsg_type == NLMSG_ERROR) {
192 struct nlmsgerr *err_data = NLMSG_DATA(nh);
194 if (err_data->error < 0) {
195 errno = -err_data->error;
201 /* Multi-part msgs and their trailing DONE message. */
202 if (nh->nlmsg_flags & NLM_F_MULTI) {
203 if (nh->nlmsg_type == NLMSG_DONE)
215 * Append a netlink attribute to a message.
218 * The netlink message to parse, received from the kernel.
220 * The type of attribute to append.
221 * @param[in] data_len
222 * The length of the data to append.
224 * The data to append.
227 tap_nlattr_add(struct nlmsghdr *nh, unsigned short type,
228 unsigned int data_len, const void *data)
230 /* see man 3 rtnetlink */
233 rta = (struct rtattr *)NLMSG_TAIL(nh);
234 rta->rta_len = RTA_LENGTH(data_len);
235 rta->rta_type = type;
236 memcpy(RTA_DATA(rta), data, data_len);
237 nh->nlmsg_len = NLMSG_ALIGN(nh->nlmsg_len) + RTA_ALIGN(rta->rta_len);
241 * Append a uint8_t netlink attribute to a message.
244 * The netlink message to parse, received from the kernel.
246 * The type of attribute to append.
248 * The data to append.
251 tap_nlattr_add8(struct nlmsghdr *nh, unsigned short type, uint8_t data)
253 tap_nlattr_add(nh, type, sizeof(uint8_t), &data);
257 * Append a uint16_t netlink attribute to a message.
260 * The netlink message to parse, received from the kernel.
262 * The type of attribute to append.
264 * The data to append.
267 tap_nlattr_add16(struct nlmsghdr *nh, unsigned short type, uint16_t data)
269 tap_nlattr_add(nh, type, sizeof(uint16_t), &data);
273 * Append a uint16_t netlink attribute to a message.
276 * The netlink message to parse, received from the kernel.
278 * The type of attribute to append.
280 * The data to append.
283 tap_nlattr_add32(struct nlmsghdr *nh, unsigned short type, uint32_t data)
285 tap_nlattr_add(nh, type, sizeof(uint32_t), &data);
289 * Start a nested netlink attribute.
290 * It must be followed later by a call to tap_nlattr_nested_finish().
292 * @param[in, out] msg
293 * The netlink message where to edit the nested_tails metadata.
295 * The nested attribute type to append.
298 * -1 if adding a nested netlink attribute failed, 0 otherwise.
301 tap_nlattr_nested_start(struct nlmsg *msg, uint16_t type)
303 struct nested_tail *tail;
305 tail = rte_zmalloc(NULL, sizeof(struct nested_tail), 0);
308 "Couldn't allocate memory for nested netlink attribute");
312 tail->tail = (struct rtattr *)NLMSG_TAIL(&msg->nh);
314 tap_nlattr_add(&msg->nh, type, 0, NULL);
316 tail->prev = msg->nested_tails;
318 msg->nested_tails = tail;
324 * End a nested netlink attribute.
325 * It follows a call to tap_nlattr_nested_start().
326 * In effect, it will modify the nested attribute length to include every bytes
327 * from the nested attribute start, up to here.
329 * @param[in, out] msg
330 * The netlink message where to edit the nested_tails metadata.
333 tap_nlattr_nested_finish(struct nlmsg *msg)
335 struct nested_tail *tail = msg->nested_tails;
337 tail->tail->rta_len = (char *)NLMSG_TAIL(&msg->nh) - (char *)tail->tail;
340 msg->nested_tails = tail->prev;