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)
106 nh->nlmsg_pid = 0; /* communication with the kernel uses pid 0 */
107 nh->nlmsg_seq = (uint32_t)rte_rand();
110 send_bytes = send(nlsk_fd, nh, nh->nlmsg_len, 0);
111 if (send_bytes < 0) {
115 TAP_LOG(ERR, "Failed to send netlink message: %s (%d)",
116 strerror(errno), errno);
123 * Check that the kernel sends an appropriate ACK in response
124 * to an tap_nl_send().
127 * The netlink socket file descriptor used for communication.
130 * 0 on success, -1 otherwise with errno set.
133 tap_nl_recv_ack(int nlsk_fd)
135 return tap_nl_recv(nlsk_fd, NULL, NULL);
139 * Receive a message from the kernel on the netlink socket, following an
143 * The netlink socket file descriptor used for communication.
145 * The callback function to call for each netlink message received.
146 * @param[in, out] arg
147 * Custom arguments for the callback.
150 * 0 on success, -1 otherwise with errno set.
153 tap_nl_recv(int nlsk_fd, int (*cb)(struct nlmsghdr *, void *arg), void *arg)
164 recv_bytes = recv(nlsk_fd, buf, sizeof(buf), 0);
165 if (recv_bytes < 0) {
171 for (nh = (struct nlmsghdr *)buf;
172 NLMSG_OK(nh, (unsigned int)recv_bytes);
173 nh = NLMSG_NEXT(nh, recv_bytes)) {
174 if (nh->nlmsg_type == NLMSG_ERROR) {
175 struct nlmsgerr *err_data = NLMSG_DATA(nh);
177 if (err_data->error < 0) {
178 errno = -err_data->error;
184 /* Multi-part msgs and their trailing DONE message. */
185 if (nh->nlmsg_flags & NLM_F_MULTI) {
186 if (nh->nlmsg_type == NLMSG_DONE)
198 * Append a netlink attribute to a message.
201 * The netlink message to parse, received from the kernel.
203 * The type of attribute to append.
204 * @param[in] data_len
205 * The length of the data to append.
207 * The data to append.
210 tap_nlattr_add(struct nlmsghdr *nh, unsigned short type,
211 unsigned int data_len, const void *data)
213 /* see man 3 rtnetlink */
216 rta = (struct rtattr *)NLMSG_TAIL(nh);
217 rta->rta_len = RTA_LENGTH(data_len);
218 rta->rta_type = type;
219 memcpy(RTA_DATA(rta), data, data_len);
220 nh->nlmsg_len = NLMSG_ALIGN(nh->nlmsg_len) + RTA_ALIGN(rta->rta_len);
224 * Append a uint8_t netlink attribute to a message.
227 * The netlink message to parse, received from the kernel.
229 * The type of attribute to append.
231 * The data to append.
234 tap_nlattr_add8(struct nlmsghdr *nh, unsigned short type, uint8_t data)
236 tap_nlattr_add(nh, type, sizeof(uint8_t), &data);
240 * Append a uint16_t netlink attribute to a message.
243 * The netlink message to parse, received from the kernel.
245 * The type of attribute to append.
247 * The data to append.
250 tap_nlattr_add16(struct nlmsghdr *nh, unsigned short type, uint16_t data)
252 tap_nlattr_add(nh, type, sizeof(uint16_t), &data);
256 * Append a uint16_t netlink attribute to a message.
259 * The netlink message to parse, received from the kernel.
261 * The type of attribute to append.
263 * The data to append.
266 tap_nlattr_add32(struct nlmsghdr *nh, unsigned short type, uint32_t data)
268 tap_nlattr_add(nh, type, sizeof(uint32_t), &data);
272 * Start a nested netlink attribute.
273 * It must be followed later by a call to tap_nlattr_nested_finish().
275 * @param[in, out] msg
276 * The netlink message where to edit the nested_tails metadata.
278 * The nested attribute type to append.
281 * -1 if adding a nested netlink attribute failed, 0 otherwise.
284 tap_nlattr_nested_start(struct nlmsg *msg, uint16_t type)
286 struct nested_tail *tail;
288 tail = rte_zmalloc(NULL, sizeof(struct nested_tail), 0);
291 "Couldn't allocate memory for nested netlink attribute");
295 tail->tail = (struct rtattr *)NLMSG_TAIL(&msg->nh);
297 tap_nlattr_add(&msg->nh, type, 0, NULL);
299 tail->prev = msg->nested_tails;
301 msg->nested_tails = tail;
307 * End a nested netlink attribute.
308 * It follows a call to tap_nlattr_nested_start().
309 * In effect, it will modify the nested attribute length to include every bytes
310 * from the nested attribute start, up to here.
312 * @param[in, out] msg
313 * The netlink message where to edit the nested_tails metadata.
316 tap_nlattr_nested_finish(struct nlmsg *msg)
318 struct nested_tail *tail = msg->nested_tails;
320 tail->tail->rta_len = (char *)NLMSG_TAIL(&msg->nh) - (char *)tail->tail;
323 msg->nested_tails = tail->prev;