1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
|
/*
* Check decoding of getsockopt and setsockopt for SOL_NETLINK level.
*
* Copyright (c) 2017-2023 Dmitry V. Levin <ldv@strace.io>
* Copyright (c) 2017-2024 The strace developers.
* All rights reserved.
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "tests.h"
#include "netlink.h"
#include <stdio.h>
#ifndef SOL_NETLINK
# define SOL_NETLINK 270
#endif
static int rc;
static const char *errstr;
static int
get_sockopt(int fd, int name, void *val, socklen_t *len)
{
rc = getsockopt(fd, SOL_NETLINK, name, val, len);
errstr = sprintrc(rc);
return rc;
}
static int
set_sockopt(int fd, int name, void *val, socklen_t len)
{
rc = setsockopt(fd, SOL_NETLINK, name, val, len);
errstr = sprintrc(rc);
return rc;
}
int
main(void)
{
static const struct strival32 names[] = {
#ifdef NETLINK_ADD_MEMBERSHIP
{ ARG_STR(NETLINK_ADD_MEMBERSHIP) },
#endif
#ifdef NETLINK_DROP_MEMBERSHIP
{ ARG_STR(NETLINK_DROP_MEMBERSHIP) },
#endif
#ifdef NETLINK_PKTINFO
{ ARG_STR(NETLINK_PKTINFO) },
#endif
#ifdef NETLINK_BROADCAST_ERROR
{ ARG_STR(NETLINK_BROADCAST_ERROR) },
#endif
#ifdef NETLINK_NO_ENOBUFS
{ ARG_STR(NETLINK_NO_ENOBUFS) },
#endif
#ifdef NETLINK_RX_RING
{ ARG_STR(NETLINK_RX_RING) },
#endif
#ifdef NETLINK_TX_RING
{ ARG_STR(NETLINK_TX_RING) },
#endif
#ifdef NETLINK_LISTEN_ALL_NSID
{ ARG_STR(NETLINK_LISTEN_ALL_NSID) },
#endif
#ifdef NETLINK_LIST_MEMBERSHIPS
{ ARG_STR(NETLINK_LIST_MEMBERSHIPS) },
#endif
#ifdef NETLINK_CAP_ACK
{ ARG_STR(NETLINK_CAP_ACK) },
#endif
#ifdef NETLINK_EXT_ACK
{ ARG_STR(NETLINK_EXT_ACK) },
#endif
};
TAIL_ALLOC_OBJECT_CONST_PTR(int, val);
TAIL_ALLOC_OBJECT_CONST_PTR(socklen_t, len);
void *const efault = val + 1;
int fd = socket(AF_NETLINK, SOCK_RAW, 0);
if (fd < 0)
perror_msg_and_skip("socket AF_NETLINK SOCK_RAW");
for (unsigned int i = 0; i < ARRAY_SIZE(names); ++i) {
/* getsockopt */
/* classic */
*len = sizeof(*val);
get_sockopt(fd, names[i].val, val, len);
printf("getsockopt(%d, SOL_NETLINK, %s, ", fd, names[i].str);
if (rc)
printf("%p", val);
else
printf("[%d]", *val);
printf(", [%d", (int) sizeof(*val));
if ((int) sizeof(*val) != *len)
printf(" => %d", *len);
printf("]) = %s\n", errstr);
/* optlen larger than necessary - shortened */
*len = sizeof(*val) + 1;
get_sockopt(fd, names[i].val, val, len);
printf("getsockopt(%d, SOL_NETLINK, %s, ", fd, names[i].str);
if (rc)
printf("%p", val);
else
printf("[%d]", *val);
printf(", [%d", (int) sizeof(*val) + 1);
if ((int) sizeof(*val) + 1 != *len)
printf(" => %d", *len);
printf("]) = %s\n", errstr);
/* zero optlen - print returned optlen */
*len = 0;
get_sockopt(fd, names[i].val, NULL, len);
printf("getsockopt(%d, SOL_NETLINK, %s, NULL, [0",
fd, names[i].str);
if (*len)
printf(" => %d", *len);
printf("]) = %s\n", errstr);
#ifdef NETLINK_LIST_MEMBERSHIPS
if (names[i].val != NETLINK_LIST_MEMBERSHIPS) {
#endif
/* optlen shorter than necessary - print address */
*len = sizeof(*val) - 1;
get_sockopt(fd, names[i].val, val, len);
printf("getsockopt(%d, SOL_NETLINK, %s, %p, [%d",
fd, names[i].str, val, (int) sizeof(*val) - 1);
if ((int) sizeof(*val) - 1 != *len)
printf(" => %d", *len);
printf("]) = %s\n", errstr);
#ifdef NETLINK_LIST_MEMBERSHIPS
} else {
/* optlen shorter than required for the first element */
*len = sizeof(*val) - 1;
get_sockopt(fd, names[i].val, efault, len);
printf("getsockopt(%d, SOL_NETLINK, %s, ",
fd, names[i].str);
if (rc)
printf("%p", efault);
else
printf("[]");
printf(", [%d", (int) sizeof(*val) - 1);
if ((int) sizeof(*val) - 1 != *len)
printf(" => %d", *len);
printf("]) = %s\n", errstr);
}
#endif
/* optval EFAULT - print address */
*len = sizeof(*val);
get_sockopt(fd, names[i].val, efault, len);
printf("getsockopt(%d, SOL_NETLINK, %s, %p",
fd, names[i].str, efault);
printf(", [%d", (int) sizeof(*val));
if ((int) sizeof(*val) != *len)
printf(" => %d", *len);
printf("]) = %s\n", errstr);
/* optlen EFAULT - print address */
get_sockopt(fd, names[i].val, val, len + 1);
printf("getsockopt(%d, SOL_NETLINK, %s, %p, %p) = %s\n",
fd, names[i].str, val, len + 1, errstr);
/* setsockopt */
/* classic */
*val = 0xdefaced;
set_sockopt(fd, names[i].val, val, sizeof(*val));
printf("setsockopt(%d, SOL_NETLINK, %s, [%d], %d) = %s\n",
fd, names[i].str, *val, (int) sizeof(*val), errstr);
/* optlen larger than necessary - shortened */
set_sockopt(fd, names[i].val, val, sizeof(*val) + 1);
printf("setsockopt(%d, SOL_NETLINK, %s, [%d], %d) = %s\n",
fd, names[i].str, *val, (int) sizeof(*val) + 1, errstr);
/* optlen < 0 - print address */
set_sockopt(fd, names[i].val, val, -1U);
printf("setsockopt(%d, SOL_NETLINK, %s, %p, -1) = %s\n",
fd, names[i].str, val, errstr);
/* optlen smaller than necessary - print address */
set_sockopt(fd, names[i].val, val, sizeof(*val) - 1);
printf("setsockopt(%d, SOL_NETLINK, %s, %p, %d) = %s\n",
fd, names[i].str, val, (int) sizeof(*val) - 1, errstr);
/* optval EFAULT - print address */
set_sockopt(fd, names[i].val, efault, sizeof(*val));
printf("setsockopt(%d, SOL_NETLINK, %s, %p, %d) = %s\n",
fd, names[i].str, efault, (int) sizeof(*val), errstr);
}
puts("+++ exited with 0 +++");
return 0;
}
|