1- /*
2- * Netlink operations for tlshd
1+ /**
2+ * @file netlink.c
3+ * @brief Handle communication with the kernel via netlink
34 *
5+ * @copyright
46 * Copyright (c) 2023 Oracle and/or its affiliates.
5- *
7+ */
8+
9+ /*
610 * ktls-utils is free software; you can redistribute it and/or
711 * modify it under the terms of the GNU General Public License as
812 * published by the Free Software Foundation; version 2.
5155#include "tlshd.h"
5256#include "netlink.h"
5357
58+ /**
59+ * @var unsigned int tlshd_delay_done
60+ * Global number of seconds to delay each handshake completion
61+ */
5462unsigned int tlshd_delay_done ;
5563
64+ /**
65+ * @brief Open a netlink socket
66+ * @param [out] sock A netlink socket descriptor
67+ *
68+ * @retval 0 Success; caller must close "sock" with tlshd_genl_sock_close
69+ * @retval ENOMEM Failed to allocate the socket
70+ * @retval ENOLINK Failed to connect to the netlink service
71+ */
5672static int tlshd_genl_sock_open (struct nl_sock * * sock )
5773{
5874 struct nl_sock * nls ;
@@ -79,6 +95,10 @@ static int tlshd_genl_sock_open(struct nl_sock **sock)
7995 return ret ;
8096}
8197
98+ /**
99+ * @brief Close a netlink socket
100+ * @param[in] nls A netlink socket descriptor
101+ */
82102static void tlshd_genl_sock_close (struct nl_sock * nls )
83103{
84104 if (!nls )
@@ -88,6 +108,10 @@ static void tlshd_genl_sock_close(struct nl_sock *nls)
88108 nl_socket_free (nls );
89109}
90110
111+ /**
112+ * @var struct nla_policy tlshd_accept_nl_policy
113+ * Netlink policies for ACCEPT arguments
114+ */
91115#if LIBNL_VER_NUM >= LIBNL_VER (3 ,5 )
92116static const struct nla_policy
93117#else
@@ -105,11 +129,33 @@ tlshd_accept_nl_policy[HANDSHAKE_A_ACCEPT_MAX + 1] = {
105129 [HANDSHAKE_A_ACCEPT_KEYRING ] = { .type = NLA_U32 , },
106130};
107131
132+ /**
133+ * @var struct nl_sock *tlshd_notification_nls
134+ * Netlink socket on which notification events arrive
135+ */
108136static struct nl_sock * tlshd_notification_nls ;
109137
138+ /**
139+ * @var sigset_t tlshd_sig_poll_mask
140+ * Daemon's signal poll mask
141+ */
110142static sigset_t tlshd_sig_poll_mask ;
143+
144+ /**
145+ * @var int tlshd_sig_poll_fd
146+ * Daemon's signal poll file descriptor
147+ */
111148static int tlshd_sig_poll_fd ;
112149
150+ /**
151+ * @brief Process one netlink notification event
152+ * @param[in] msg A netlink event to be handled
153+ * @param[in] arg Additional arguments
154+ *
155+ * @retval NL_OK Proceed with the next message
156+ * @retval NL_SKIP Skip this message.
157+ * @retval NL_STOP Stop and discard remaining messages.
158+ */
113159static int tlshd_genl_event_handler (struct nl_msg * msg ,
114160 __attribute__ ((unused )) void * arg )
115161{
@@ -148,8 +194,7 @@ static int tlshd_genl_event_handler(struct nl_msg *msg,
148194}
149195
150196/**
151- * tlshd_genl_dispatch - handle notification events
152- *
197+ * @brief Handle notification events
153198 */
154199void tlshd_genl_dispatch (void )
155200{
@@ -231,6 +276,11 @@ void tlshd_genl_dispatch(void)
231276 tlshd_genl_sock_close (tlshd_notification_nls );
232277}
233278
279+ /**
280+ * @brief Extract the key serial number of the key with the remote peerid
281+ * @param[in] parms Handshake parameters
282+ * @param[in] head List of nlattrs to parse
283+ */
234284static void tlshd_parse_peer_identity (struct tlshd_handshake_parms * parms ,
235285 struct nlattr * head )
236286{
@@ -245,6 +295,10 @@ static void tlshd_parse_peer_identity(struct tlshd_handshake_parms *parms,
245295 g_array_append_val (parms -> peerids , peerid );
246296}
247297
298+ /**
299+ * @var struct nla_policy tlshd_x509_nl_policy
300+ * Netlink policies for x.509 key serial numbers
301+ */
248302#if LIBNL_VER_NUM >= LIBNL_VER (3 ,5 )
249303static const struct nla_policy
250304#else
@@ -255,6 +309,11 @@ tlshd_x509_nl_policy[HANDSHAKE_A_X509_MAX + 1] = {
255309 [HANDSHAKE_A_X509_PRIVKEY ] = { .type = NLA_U32 , },
256310};
257311
312+ /**
313+ * @brief Extract the key serial number of the key with the cert / privkey
314+ * @param[in] parms Handshake parameters
315+ * @param[in] head List of nlattrs to parse
316+ */
258317static void tlshd_parse_certificate (struct tlshd_handshake_parms * parms ,
259318 struct nlattr * head )
260319{
@@ -277,6 +336,15 @@ static void tlshd_parse_certificate(struct tlshd_handshake_parms *parms,
277336 parms -> x509_privkey = nla_get_s32 (tb [HANDSHAKE_A_X509_PRIVKEY ]);
278337}
279338
339+ /**
340+ * @brief Process an ACCESS argument
341+ * @param[in] msg Message to be processed
342+ * @param[out] arg Handshake parms to be filled in
343+ *
344+ * @retval NL_OK Proceed with the next message
345+ * @retval NL_SKIP Skip this message.
346+ * @retval NL_STOP Stop and discard remaining messages.
347+ */
280348static int tlshd_genl_valid_handler (struct nl_msg * msg , void * arg )
281349{
282350 struct nlattr * tb [HANDSHAKE_A_ACCEPT_MAX + 1 ];
@@ -363,6 +431,10 @@ static int tlshd_genl_valid_handler(struct nl_msg *msg, void *arg)
363431 return NL_SKIP ;
364432}
365433
434+ /**
435+ * @var struct tlshd_handshake_parms tlshd_default_handshake_parms
436+ * Starting parameter values for each handshake
437+ */
366438static const struct tlshd_handshake_parms tlshd_default_handshake_parms = {
367439 .peername = NULL ,
368440 .peeraddr = NULL ,
@@ -379,13 +451,15 @@ static const struct tlshd_handshake_parms tlshd_default_handshake_parms = {
379451};
380452
381453/**
382- * tlshd_genl_get_handshake_parms - Retrieve handshake parameters
383- * @parms: buffer to fill in with parameters
454+ * @brief Retrieve handshake parameters
455+ * @param[in] parms Buffer to fill in with parameters
384456 *
385- * Returns 0 if handshake parameters were retrieved successfully.
457+ * Caller must release handshake resources by calling
458+ * tlshd_genl_put_handshake_parms when finished.
386459 *
460+ * @returns 0 if handshake parameters were retrieved successfully.
387461 * Otherwise a positive errno is returned, and the content of
388- * @ parms is indeterminant.
462+ * " parms" is indeterminant.
389463 */
390464int tlshd_genl_get_handshake_parms (struct tlshd_handshake_parms * parms )
391465{
@@ -460,9 +534,8 @@ int tlshd_genl_get_handshake_parms(struct tlshd_handshake_parms *parms)
460534}
461535
462536/**
463- * tlshd_genl_put_handshake_parms - Release handshake resources
464- * @parms: handshake parameters to be released
465- *
537+ * @brief Release handshake resources
538+ * @param[in] parms Handshake parameters to be released
466539 */
467540void tlshd_genl_put_handshake_parms (struct tlshd_handshake_parms * parms )
468541{
@@ -474,6 +547,14 @@ void tlshd_genl_put_handshake_parms(struct tlshd_handshake_parms *parms)
474547 free (parms -> peeraddr );
475548}
476549
550+ /**
551+ * @brief Format all remote peerid arguments
552+ * @param[in] msg
553+ * @param[in] parms Handshake parameters
554+ *
555+ * retval 0 Formatted all remote peerid arguments successfully
556+ * retval -1 Failed to format
557+ */
477558static int tlshd_genl_put_remote_peerids (struct nl_msg * msg ,
478559 struct tlshd_handshake_parms * parms )
479560{
@@ -494,9 +575,8 @@ static int tlshd_genl_put_remote_peerids(struct nl_msg *msg,
494575}
495576
496577/**
497- * tlshd_genl_done - Indicate handshake has completed successfully
498- * @parms: buffer filled in with parameters
499- *
578+ * @brief Indicate handshake has completed successfully
579+ * @param[in] parms Buffer filled in with parameters
500580 */
501581void tlshd_genl_done (struct tlshd_handshake_parms * parms )
502582{
0 commit comments