net/tap: use SPDX tags in 6WIND copyrighted files
[dpdk.git] / drivers / net / tap / tap_netlink.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2017 6WIND S.A.
3  * Copyright 2017 Mellanox.
4  */
5
6 #include <errno.h>
7 #include <inttypes.h>
8 #include <linux/netlink.h>
9 #include <string.h>
10 #include <sys/socket.h>
11 #include <unistd.h>
12
13 #include <rte_malloc.h>
14 #include <tap_netlink.h>
15 #include <rte_random.h>
16
17 /* Must be quite large to support dumping a huge list of QDISC or filters. */
18 #define BUF_SIZE (32 * 1024) /* Size of the buffer to receive kernel messages */
19 #define SNDBUF_SIZE 32768 /* Send buffer size for the netlink socket */
20 #define RCVBUF_SIZE 32768 /* Receive buffer size for the netlink socket */
21
22 struct nested_tail {
23         struct rtattr *tail;
24         struct nested_tail *prev;
25 };
26
27 /**
28  * Initialize a netlink socket for communicating with the kernel.
29  *
30  * @param nl_groups
31  *   Set it to a netlink group value (e.g. RTMGRP_LINK) to receive messages for
32  *   specific netlink multicast groups. Otherwise, no subscription will be made.
33  *
34  * @return
35  *   netlink socket file descriptor on success, -1 otherwise.
36  */
37 int
38 tap_nl_init(uint32_t nl_groups)
39 {
40         int fd, sndbuf_size = SNDBUF_SIZE, rcvbuf_size = RCVBUF_SIZE;
41         struct sockaddr_nl local = {
42                 .nl_family = AF_NETLINK,
43                 .nl_groups = nl_groups,
44         };
45
46         fd = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE);
47         if (fd < 0) {
48                 RTE_LOG(ERR, PMD, "Unable to create a netlink socket\n");
49                 return -1;
50         }
51         if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &sndbuf_size, sizeof(int))) {
52                 RTE_LOG(ERR, PMD, "Unable to set socket buffer send size\n");
53                 return -1;
54         }
55         if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf_size, sizeof(int))) {
56                 RTE_LOG(ERR, PMD, "Unable to set socket buffer receive size\n");
57                 return -1;
58         }
59         if (bind(fd, (struct sockaddr *)&local, sizeof(local)) < 0) {
60                 RTE_LOG(ERR, PMD, "Unable to bind to the netlink socket\n");
61                 return -1;
62         }
63         return fd;
64 }
65
66 /**
67  * Clean up a netlink socket once all communicating with the kernel is finished.
68  *
69  * @param[in] nlsk_fd
70  *   The netlink socket file descriptor used for communication.
71  *
72  * @return
73  *   0 on success, -1 otherwise.
74  */
75 int
76 tap_nl_final(int nlsk_fd)
77 {
78         if (close(nlsk_fd)) {
79                 RTE_LOG(ERR, PMD, "Failed to close netlink socket: %s (%d)\n",
80                         strerror(errno), errno);
81                 return -1;
82         }
83         return 0;
84 }
85
86 /**
87  * Send a message to the kernel on the netlink socket.
88  *
89  * @param[in] nlsk_fd
90  *   The netlink socket file descriptor used for communication.
91  * @param[in] nh
92  *   The netlink message send to the kernel.
93  *
94  * @return
95  *   the number of sent bytes on success, -1 otherwise.
96  */
97 int
98 tap_nl_send(int nlsk_fd, struct nlmsghdr *nh)
99 {
100         /* man 7 netlink EXAMPLE */
101         struct sockaddr_nl sa = {
102                 .nl_family = AF_NETLINK,
103         };
104         struct iovec iov = {
105                 .iov_base = nh,
106                 .iov_len = nh->nlmsg_len,
107         };
108         struct msghdr msg = {
109                 .msg_name = &sa,
110                 .msg_namelen = sizeof(sa),
111                 .msg_iov = &iov,
112                 .msg_iovlen = 1,
113         };
114         int send_bytes;
115
116         nh->nlmsg_pid = 0; /* communication with the kernel uses pid 0 */
117         nh->nlmsg_seq = (uint32_t)rte_rand();
118         send_bytes = sendmsg(nlsk_fd, &msg, 0);
119         if (send_bytes < 0) {
120                 RTE_LOG(ERR, PMD, "Failed to send netlink message: %s (%d)\n",
121                         strerror(errno), errno);
122                 return -1;
123         }
124         return send_bytes;
125 }
126
127 /**
128  * Check that the kernel sends an appropriate ACK in response
129  * to an tap_nl_send().
130  *
131  * @param[in] nlsk_fd
132  *   The netlink socket file descriptor used for communication.
133  *
134  * @return
135  *   0 on success, -1 otherwise with errno set.
136  */
137 int
138 tap_nl_recv_ack(int nlsk_fd)
139 {
140         return tap_nl_recv(nlsk_fd, NULL, NULL);
141 }
142
143 /**
144  * Receive a message from the kernel on the netlink socket, following an
145  * tap_nl_send().
146  *
147  * @param[in] nlsk_fd
148  *   The netlink socket file descriptor used for communication.
149  * @param[in] cb
150  *   The callback function to call for each netlink message received.
151  * @param[in, out] arg
152  *   Custom arguments for the callback.
153  *
154  * @return
155  *   0 on success, -1 otherwise with errno set.
156  */
157 int
158 tap_nl_recv(int nlsk_fd, int (*cb)(struct nlmsghdr *, void *arg), void *arg)
159 {
160         /* man 7 netlink EXAMPLE */
161         struct sockaddr_nl sa;
162         char buf[BUF_SIZE];
163         struct iovec iov = {
164                 .iov_base = buf,
165                 .iov_len = sizeof(buf),
166         };
167         struct msghdr msg = {
168                 .msg_name = &sa,
169                 .msg_namelen = sizeof(sa),
170                 .msg_iov = &iov,
171                 /* One message at a time */
172                 .msg_iovlen = 1,
173         };
174         int multipart = 0;
175         int ret = 0;
176
177         do {
178                 struct nlmsghdr *nh;
179                 int recv_bytes = 0;
180
181                 recv_bytes = recvmsg(nlsk_fd, &msg, 0);
182                 if (recv_bytes < 0)
183                         return -1;
184                 for (nh = (struct nlmsghdr *)buf;
185                      NLMSG_OK(nh, (unsigned int)recv_bytes);
186                      nh = NLMSG_NEXT(nh, recv_bytes)) {
187                         if (nh->nlmsg_type == NLMSG_ERROR) {
188                                 struct nlmsgerr *err_data = NLMSG_DATA(nh);
189
190                                 if (err_data->error < 0) {
191                                         errno = -err_data->error;
192                                         return -1;
193                                 }
194                                 /* Ack message. */
195                                 return 0;
196                         }
197                         /* Multi-part msgs and their trailing DONE message. */
198                         if (nh->nlmsg_flags & NLM_F_MULTI) {
199                                 if (nh->nlmsg_type == NLMSG_DONE)
200                                         return 0;
201                                 multipart = 1;
202                         }
203                         if (cb)
204                                 ret = cb(nh, arg);
205                 }
206         } while (multipart);
207         return ret;
208 }
209
210 /**
211  * Append a netlink attribute to a message.
212  *
213  * @param[in, out] nh
214  *   The netlink message to parse, received from the kernel.
215  * @param[in] type
216  *   The type of attribute to append.
217  * @param[in] data_len
218  *   The length of the data to append.
219  * @param[in] data
220  *   The data to append.
221  */
222 void
223 tap_nlattr_add(struct nlmsghdr *nh, unsigned short type,
224            unsigned int data_len, const void *data)
225 {
226         /* see man 3 rtnetlink */
227         struct rtattr *rta;
228
229         rta = (struct rtattr *)NLMSG_TAIL(nh);
230         rta->rta_len = RTA_LENGTH(data_len);
231         rta->rta_type = type;
232         memcpy(RTA_DATA(rta), data, data_len);
233         nh->nlmsg_len = NLMSG_ALIGN(nh->nlmsg_len) + RTA_ALIGN(rta->rta_len);
234 }
235
236 /**
237  * Append a uint8_t netlink attribute to a message.
238  *
239  * @param[in, out] nh
240  *   The netlink message to parse, received from the kernel.
241  * @param[in] type
242  *   The type of attribute to append.
243  * @param[in] data
244  *   The data to append.
245  */
246 void
247 tap_nlattr_add8(struct nlmsghdr *nh, unsigned short type, uint8_t data)
248 {
249         tap_nlattr_add(nh, type, sizeof(uint8_t), &data);
250 }
251
252 /**
253  * Append a uint16_t netlink attribute to a message.
254  *
255  * @param[in, out] nh
256  *   The netlink message to parse, received from the kernel.
257  * @param[in] type
258  *   The type of attribute to append.
259  * @param[in] data
260  *   The data to append.
261  */
262 void
263 tap_nlattr_add16(struct nlmsghdr *nh, unsigned short type, uint16_t data)
264 {
265         tap_nlattr_add(nh, type, sizeof(uint16_t), &data);
266 }
267
268 /**
269  * Append a uint16_t netlink attribute to a message.
270  *
271  * @param[in, out] nh
272  *   The netlink message to parse, received from the kernel.
273  * @param[in] type
274  *   The type of attribute to append.
275  * @param[in] data
276  *   The data to append.
277  */
278 void
279 tap_nlattr_add32(struct nlmsghdr *nh, unsigned short type, uint32_t data)
280 {
281         tap_nlattr_add(nh, type, sizeof(uint32_t), &data);
282 }
283
284 /**
285  * Start a nested netlink attribute.
286  * It must be followed later by a call to tap_nlattr_nested_finish().
287  *
288  * @param[in, out] msg
289  *   The netlink message where to edit the nested_tails metadata.
290  * @param[in] type
291  *   The nested attribute type to append.
292  *
293  * @return
294  *   -1 if adding a nested netlink attribute failed, 0 otherwise.
295  */
296 int
297 tap_nlattr_nested_start(struct nlmsg *msg, uint16_t type)
298 {
299         struct nested_tail *tail;
300
301         tail = rte_zmalloc(NULL, sizeof(struct nested_tail), 0);
302         if (!tail) {
303                 RTE_LOG(ERR, PMD,
304                         "Couldn't allocate memory for nested netlink"
305                         " attribute\n");
306                 return -1;
307         }
308
309         tail->tail = (struct rtattr *)NLMSG_TAIL(&msg->nh);
310
311         tap_nlattr_add(&msg->nh, type, 0, NULL);
312
313         tail->prev = msg->nested_tails;
314
315         msg->nested_tails = tail;
316
317         return 0;
318 }
319
320 /**
321  * End a nested netlink attribute.
322  * It follows a call to tap_nlattr_nested_start().
323  * In effect, it will modify the nested attribute length to include every bytes
324  * from the nested attribute start, up to here.
325  *
326  * @param[in, out] msg
327  *   The netlink message where to edit the nested_tails metadata.
328  */
329 void
330 tap_nlattr_nested_finish(struct nlmsg *msg)
331 {
332         struct nested_tail *tail = msg->nested_tails;
333
334         tail->tail->rta_len = (char *)NLMSG_TAIL(&msg->nh) - (char *)tail->tail;
335
336         if (tail->prev)
337                 msg->nested_tails = tail->prev;
338
339         rte_free(tail);
340 }