1/* Check recvmsg results for netlink sockets.
2 Copyright (C) 2015-2020 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 <errno.h>
20#include <fcntl.h>
21#include <stdio.h>
22#include <sys/socket.h>
23
24#include "netlinkaccess.h"
25
26static int
27get_address_family (int fd)
28{
29 struct sockaddr_storage sa;
30 socklen_t sa_len = sizeof (sa);
31 if (__getsockname (fd, (struct sockaddr *) &sa, &sa_len) < 0)
32 return -1;
33 /* Check that the socket family number is preserved despite in-band
34 signaling. */
35 _Static_assert (sizeof (sa.ss_family) < sizeof (int), "address family size");
36 _Static_assert (0 < (__typeof__ (sa.ss_family)) -1,
37 "address family unsigned");
38 return sa.ss_family;
39}
40
41void
42__netlink_assert_response (int fd, ssize_t result)
43{
44 if (result < 0)
45 {
46 /* Check if the error is unexpected. */
47 bool terminate = false;
48 int error_code = errno;
49 int family = get_address_family (fd);
50 if (family != AF_NETLINK)
51 /* If the address family does not match (or getsockname
52 failed), report the original error. */
53 terminate = true;
54 else if (error_code == EBADF
55 || error_code == ENOTCONN
56 || error_code == ENOTSOCK
57 || error_code == ECONNREFUSED)
58 /* These errors indicate that the descriptor is not a
59 connected socket. */
60 terminate = true;
61 else if (error_code == EAGAIN || error_code == EWOULDBLOCK)
62 {
63 /* The kernel might return EAGAIN for other reasons than a
64 non-blocking socket. But if the socket is not blocking,
65 it is not ours, so report the error. */
66 int mode = __fcntl (fd, F_GETFL, 0);
67 if (mode < 0 || (mode & O_NONBLOCK) != 0)
68 terminate = true;
69 }
70 if (terminate)
71 {
72 char message[200];
73 if (family < 0)
74 __snprintf (message, sizeof (message),
75 "Unexpected error %d on netlink descriptor %d.\n",
76 error_code, fd);
77 else
78 __snprintf (message, sizeof (message),
79 "Unexpected error %d on netlink descriptor %d"
80 " (address family %d).\n",
81 error_code, fd, family);
82 __libc_fatal (message);
83 }
84 else
85 /* Restore orignal errno value. */
86 __set_errno (error_code);
87 }
88 else if (result < sizeof (struct nlmsghdr))
89 {
90 char message[200];
91 int family = get_address_family (fd);
92 if (family < 0)
93 __snprintf (message, sizeof (message),
94 "Unexpected netlink response of size %zd"
95 " on descriptor %d\n",
96 result, fd);
97 else
98 __snprintf (message, sizeof (message),
99 "Unexpected netlink response of size %zd"
100 " on descriptor %d (address family %d)\n",
101 result, fd, family);
102 __libc_fatal (message);
103 }
104}
105libc_hidden_def (__netlink_assert_response)
106