• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * This file is part of net-yy-netlink strace test.
3  *
4  * Copyright (c) 2014-2016 Dmitry V. Levin <ldv@altlinux.org>
5  * Copyright (c) 2016 Fabien Siron <fabien.siron@epita.fr>
6  * Copyright (c) 2016-2018 The strace developers.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. The name of the author may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include "tests.h"
33 #include <errno.h>
34 #include <string.h>
35 #include <unistd.h>
36 #include <netinet/in.h>
37 #include "netlink.h"
38 #include <linux/sock_diag.h>
39 #include <linux/netlink_diag.h>
40 
41 static void
send_query(const int fd)42 send_query(const int fd)
43 {
44 	struct sockaddr_nl nladdr = {
45 		.nl_family = AF_NETLINK
46 	};
47 	struct {
48 		struct nlmsghdr nlh;
49 		struct netlink_diag_req ndr;
50 	} req = {
51 		.nlh = {
52 			.nlmsg_len = sizeof(req),
53 			.nlmsg_type = SOCK_DIAG_BY_FAMILY,
54 			.nlmsg_flags = NLM_F_DUMP | NLM_F_REQUEST
55 		},
56 		.ndr = {
57 			.sdiag_family = AF_NETLINK,
58 			.sdiag_protocol = NDIAG_PROTO_ALL
59 		}
60 	};
61 	struct iovec iov = {
62 		.iov_base = &req,
63 		.iov_len = sizeof(req)
64 	};
65 	struct msghdr msg = {
66 		.msg_name = (void *) &nladdr,
67 		.msg_namelen = sizeof(nladdr),
68 		.msg_iov = &iov,
69 		.msg_iovlen = 1
70 	};
71 
72 	if (sendmsg(fd, &msg, 0) <= 0)
73 		perror_msg_and_skip("sendmsg");
74 }
75 
76 static void
check_responses(const int fd)77 check_responses(const int fd)
78 {
79 	static union {
80 		struct nlmsghdr hdr;
81 		long buf[8192 / sizeof(long)];
82 	} hdr_buf;
83 
84 	struct sockaddr_nl nladdr = {
85 		.nl_family = AF_NETLINK
86 	};
87 	struct iovec iov = {
88 		.iov_base = hdr_buf.buf,
89 		.iov_len = sizeof(hdr_buf.buf)
90 	};
91 	struct msghdr msg = {
92 		.msg_name = (void *) &nladdr,
93 		.msg_namelen = sizeof(nladdr),
94 		.msg_iov = &iov,
95 		.msg_iovlen = 1
96 	};
97 
98 	ssize_t ret = recvmsg(fd, &msg, 0);
99 	if (ret <= 0)
100 		perror_msg_and_skip("recvmsg");
101 
102 	struct nlmsghdr *h = &hdr_buf.hdr;
103 	if (!is_nlmsg_ok(h, ret))
104 		error_msg_and_skip("!is_nlmsg_ok");
105 	if (h->nlmsg_type == NLMSG_ERROR) {
106 		const struct nlmsgerr *err = NLMSG_DATA(h);
107 		if (h->nlmsg_len < NLMSG_LENGTH(sizeof(*err)))
108 			error_msg_and_skip("NLMSG_ERROR");
109 		errno = -err->error;
110 		perror_msg_and_skip("NLMSG_ERROR");
111 	}
112 	if (h->nlmsg_type != SOCK_DIAG_BY_FAMILY)
113 		error_msg_and_skip("unexpected nlmsg_type %u",
114 				   (unsigned) h->nlmsg_type);
115 
116 	const struct netlink_diag_msg *diag = NLMSG_DATA(h);
117 	if (h->nlmsg_len < NLMSG_LENGTH(sizeof(*diag)))
118 		error_msg_and_skip("short response");
119 }
120 
main(void)121 int main(void)
122 {
123 	struct sockaddr_nl addr;
124 	socklen_t len = sizeof(addr);
125 
126 	memset(&addr, 0, sizeof(addr));
127 	addr.nl_family = AF_NETLINK;
128 
129 	close(0);
130 	close(1);
131 
132 	if (socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG))
133 		perror_msg_and_skip("socket AF_NETLINK");
134 	if (bind(0, (struct sockaddr *) &addr, len))
135 		perror_msg_and_skip("bind");
136 
137 	if (socket(AF_NETLINK, SOCK_RAW, NETLINK_SOCK_DIAG) != 1)
138 		perror_msg_and_skip("socket AF_NETLINK");
139 
140 	send_query(1);
141 	check_responses(1);
142 	return 0;
143 }
144