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>
14 #include <rte_malloc.h>
15 #include <tap_netlink.h>
16 #include <rte_random.h>
20 /* Compatibility with glibc < 2.24 */
22 #define SOL_NETLINK 270
25 /* Must be quite large to support dumping a huge list of QDISC or filters. */
26 #define BUF_SIZE (32 * 1024) /* Size of the buffer to receive kernel messages */
27 #define SNDBUF_SIZE 32768 /* Send buffer size for the netlink socket */
28 #define RCVBUF_SIZE 32768 /* Receive buffer size for the netlink socket */
32 struct nested_tail *prev;
36 * Initialize a netlink socket for communicating with the kernel.
39 * Set it to a netlink group value (e.g. RTMGRP_LINK) to receive messages for
40 * specific netlink multicast groups. Otherwise, no subscription will be made.
43 * netlink socket file descriptor on success, -1 otherwise.
46 tap_nl_init(uint32_t nl_groups)
48 int fd, sndbuf_size = SNDBUF_SIZE, rcvbuf_size = RCVBUF_SIZE;
49 struct sockaddr_nl local = {
50 .nl_family = AF_NETLINK,
51 .nl_groups = nl_groups,
53 #ifdef NETLINK_EXT_ACK
57 fd = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE);
59 TAP_LOG(ERR, "Unable to create a netlink socket");
62 if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &sndbuf_size, sizeof(int))) {
63 TAP_LOG(ERR, "Unable to set socket buffer send size");
67 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf_size, sizeof(int))) {
68 TAP_LOG(ERR, "Unable to set socket buffer receive size");
73 #ifdef NETLINK_EXT_ACK
74 /* Ask for extended ACK response. on older kernel will ignore request. */
75 setsockopt(fd, SOL_NETLINK, NETLINK_EXT_ACK, &one, sizeof(one));
78 if (bind(fd, (struct sockaddr *)&local, sizeof(local)) < 0) {
79 TAP_LOG(ERR, "Unable to bind to the netlink socket");
87 * Clean up a netlink socket once all communicating with the kernel is finished.
90 * The netlink socket file descriptor used for communication.
93 * 0 on success, -1 otherwise.
96 tap_nl_final(int nlsk_fd)
99 TAP_LOG(ERR, "Failed to close netlink socket: %s (%d)",
100 strerror(errno), errno);
107 * Send a message to the kernel on the netlink socket.
110 * The netlink socket file descriptor used for communication.
112 * The netlink message send to the kernel.
115 * the number of sent bytes on success, -1 otherwise.
118 tap_nl_send(int nlsk_fd, struct nlmsghdr *nh)
122 nh->nlmsg_pid = 0; /* communication with the kernel uses pid 0 */
123 nh->nlmsg_seq = (uint32_t)rte_rand();
126 send_bytes = send(nlsk_fd, nh, nh->nlmsg_len, 0);
127 if (send_bytes < 0) {
131 TAP_LOG(ERR, "Failed to send netlink message: %s (%d)",
132 strerror(errno), errno);
138 #ifdef NETLINK_EXT_ACK
139 static const struct nlattr *
140 tap_nl_attr_first(const struct nlmsghdr *nh, size_t offset)
142 return (const struct nlattr *)((const char *)nh + NLMSG_SPACE(offset));
145 static const struct nlattr *
146 tap_nl_attr_next(const struct nlattr *attr)
148 return (const struct nlattr *)((const char *)attr
149 + NLMSG_ALIGN(attr->nla_len));
153 tap_nl_attr_ok(const struct nlattr *attr, int len)
155 if (len < (int)sizeof(struct nlattr))
156 return false; /* missing header */
157 if (attr->nla_len < sizeof(struct nlattr))
158 return false; /* attribute length should include itself */
159 if ((int)attr->nla_len > len)
160 return false; /* attribute is truncated */
165 /* Decode extended errors from kernel */
167 tap_nl_dump_ext_ack(const struct nlmsghdr *nh, const struct nlmsgerr *err)
169 const struct nlattr *attr;
170 const char *tail = (const char *)nh + NLMSG_ALIGN(nh->nlmsg_len);
171 size_t hlen = sizeof(*err);
173 /* no TLVs, no extended response */
174 if (!(nh->nlmsg_flags & NLM_F_ACK_TLVS))
177 if (!(nh->nlmsg_flags & NLM_F_CAPPED))
178 hlen += err->msg.nlmsg_len - NLMSG_HDRLEN;
180 for (attr = tap_nl_attr_first(nh, hlen);
181 tap_nl_attr_ok(attr, tail - (const char *)attr);
182 attr = tap_nl_attr_next(attr)) {
183 uint16_t type = attr->nla_type & NLA_TYPE_MASK;
185 if (type == NLMSGERR_ATTR_MSG) {
186 const char *msg = (const char *)attr
187 + NLMSG_ALIGN(sizeof(*attr));
190 TAP_LOG(ERR, "%s", msg);
193 TAP_LOG(WARNING, "%s", msg);
200 * External ACK support was added in Linux kernel 4.17
201 * on older kernels, just ignore that part of message
203 #define tap_nl_dump_ext_ack(nh, err) do { } while (0)
207 * Check that the kernel sends an appropriate ACK in response
208 * to an tap_nl_send().
211 * The netlink socket file descriptor used for communication.
214 * 0 on success, -1 otherwise with errno set.
217 tap_nl_recv_ack(int nlsk_fd)
219 return tap_nl_recv(nlsk_fd, NULL, NULL);
223 * Receive a message from the kernel on the netlink socket, following an
227 * The netlink socket file descriptor used for communication.
229 * The callback function to call for each netlink message received.
230 * @param[in, out] arg
231 * Custom arguments for the callback.
234 * 0 on success, -1 otherwise with errno set.
237 tap_nl_recv(int nlsk_fd, int (*cb)(struct nlmsghdr *, void *arg), void *arg)
248 recv_bytes = recv(nlsk_fd, buf, sizeof(buf), 0);
249 if (recv_bytes < 0) {
255 for (nh = (struct nlmsghdr *)buf;
256 NLMSG_OK(nh, (unsigned int)recv_bytes);
257 nh = NLMSG_NEXT(nh, recv_bytes)) {
258 if (nh->nlmsg_type == NLMSG_ERROR) {
259 struct nlmsgerr *err_data = NLMSG_DATA(nh);
261 tap_nl_dump_ext_ack(nh, err_data);
262 if (err_data->error < 0) {
263 errno = -err_data->error;
269 /* Multi-part msgs and their trailing DONE message. */
270 if (nh->nlmsg_flags & NLM_F_MULTI) {
271 if (nh->nlmsg_type == NLMSG_DONE)
283 * Append a netlink attribute to a message.
286 * The netlink message to parse, received from the kernel.
288 * The type of attribute to append.
289 * @param[in] data_len
290 * The length of the data to append.
292 * The data to append.
295 tap_nlattr_add(struct nlmsghdr *nh, unsigned short type,
296 unsigned int data_len, const void *data)
298 /* see man 3 rtnetlink */
301 rta = (struct rtattr *)NLMSG_TAIL(nh);
302 rta->rta_len = RTA_LENGTH(data_len);
303 rta->rta_type = type;
304 memcpy(RTA_DATA(rta), data, data_len);
305 nh->nlmsg_len = NLMSG_ALIGN(nh->nlmsg_len) + RTA_ALIGN(rta->rta_len);
309 * Append a uint8_t netlink attribute to a message.
312 * The netlink message to parse, received from the kernel.
314 * The type of attribute to append.
316 * The data to append.
319 tap_nlattr_add8(struct nlmsghdr *nh, unsigned short type, uint8_t data)
321 tap_nlattr_add(nh, type, sizeof(uint8_t), &data);
325 * Append a uint16_t netlink attribute to a message.
328 * The netlink message to parse, received from the kernel.
330 * The type of attribute to append.
332 * The data to append.
335 tap_nlattr_add16(struct nlmsghdr *nh, unsigned short type, uint16_t data)
337 tap_nlattr_add(nh, type, sizeof(uint16_t), &data);
341 * Append a uint16_t netlink attribute to a message.
344 * The netlink message to parse, received from the kernel.
346 * The type of attribute to append.
348 * The data to append.
351 tap_nlattr_add32(struct nlmsghdr *nh, unsigned short type, uint32_t data)
353 tap_nlattr_add(nh, type, sizeof(uint32_t), &data);
357 * Start a nested netlink attribute.
358 * It must be followed later by a call to tap_nlattr_nested_finish().
360 * @param[in, out] msg
361 * The netlink message where to edit the nested_tails metadata.
363 * The nested attribute type to append.
366 * -1 if adding a nested netlink attribute failed, 0 otherwise.
369 tap_nlattr_nested_start(struct nlmsg *msg, uint16_t type)
371 struct nested_tail *tail;
373 tail = rte_zmalloc(NULL, sizeof(struct nested_tail), 0);
376 "Couldn't allocate memory for nested netlink attribute");
380 tail->tail = (struct rtattr *)NLMSG_TAIL(&msg->nh);
382 tap_nlattr_add(&msg->nh, type, 0, NULL);
384 tail->prev = msg->nested_tails;
386 msg->nested_tails = tail;
392 * End a nested netlink attribute.
393 * It follows a call to tap_nlattr_nested_start().
394 * In effect, it will modify the nested attribute length to include every bytes
395 * from the nested attribute start, up to here.
397 * @param[in, out] msg
398 * The netlink message where to edit the nested_tails metadata.
401 tap_nlattr_nested_finish(struct nlmsg *msg)
403 struct nested_tail *tail = msg->nested_tails;
405 tail->tail->rta_len = (char *)NLMSG_TAIL(&msg->nh) - (char *)tail->tail;
408 msg->nested_tails = tail->prev;