1/* Socket timestamp conversion routines.
2 Copyright (C) 2021 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19#include <kernel-features.h>
20
21#ifndef __ASSUME_TIME64_SYSCALLS
22# include <stdint.h>
23# include <string.h>
24# include <sys/socket.h>
25# include <socket-constants-time64.h>
26
27/* It converts the first SO_TIMESTAMP or SO_TIMESTAMPNS with 32-bit time and
28 appends it to the control buffer. The 32-bit time field is kept as-is.
29
30 Calls with __TIMESIZE=32 will see the converted 64-bit time control
31 messages as spurious control message of unknown type.
32
33 Calls with __TIMESIZE=64 running on pre-time64 kernels will see the
34 original message as a spurious control ones of unknown typ while running
35 on kernel with native 64-bit time support will only see the time64 version
36 of the control message. */
37void
38__convert_scm_timestamps (struct msghdr *msg, socklen_t msgsize)
39{
40 if (msg->msg_control == NULL || msg->msg_controllen == 0)
41 return;
42
43 /* The returned control message format for SO_TIMESTAMP_NEW is a
44 'struct __kernel_sock_timeval' while for SO_TIMESTAMPNS_NEW is a
45 'struct __kernel_timespec'. In either case it is two uint64_t
46 members. */
47 int64_t tvts[2];
48 int32_t tmp[2];
49
50 struct cmsghdr *cmsg, *last = NULL;
51 int type = 0;
52
53 for (cmsg = CMSG_FIRSTHDR (msg);
54 cmsg != NULL;
55 cmsg = CMSG_NXTHDR (msg, cmsg))
56 {
57 if (cmsg->cmsg_level != SOL_SOCKET)
58 continue;
59
60 switch (cmsg->cmsg_type)
61 {
62 case COMPAT_SO_TIMESTAMP_OLD:
63 if (type != 0)
64 break;
65 type = COMPAT_SO_TIMESTAMP_NEW;
66 goto common;
67
68 case COMPAT_SO_TIMESTAMPNS_OLD:
69 type = COMPAT_SO_TIMESTAMPNS_NEW;
70
71 /* fallthrough */
72 common:
73 memcpy (tmp, CMSG_DATA (cmsg), sizeof (tmp));
74 tvts[0] = tmp[0];
75 tvts[1] = tmp[1];
76 break;
77 }
78
79 last = cmsg;
80 }
81
82 if (last == NULL || type == 0)
83 return;
84
85 if (CMSG_SPACE (sizeof tvts) > msgsize - msg->msg_controllen)
86 {
87 msg->msg_flags |= MSG_CTRUNC;
88 return;
89 }
90
91 msg->msg_controllen += CMSG_SPACE (sizeof tvts);
92 cmsg = CMSG_NXTHDR(msg, last);
93 if (cmsg == NULL)
94 return;
95 cmsg->cmsg_level = SOL_SOCKET;
96 cmsg->cmsg_type = type;
97 cmsg->cmsg_len = CMSG_LEN (sizeof tvts);
98 memcpy (CMSG_DATA (cmsg), tvts, sizeof tvts);
99}
100#endif
101