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