1 /*
2  * Copyright (c) 2017 JingPiao Chen <chenjingpiao@gmail.com>
3  * Copyright (c) 2017-2018 The strace developers.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include "tests.h"
30 
31 #include <stdio.h>
32 #include <string.h>
33 #include <stdint.h>
34 #include "test_nlattr.h"
35 #include <linux/netlink_diag.h>
36 #include <linux/sock_diag.h>
37 
38 static void
init_netlink_diag_msg(struct nlmsghdr * const nlh,const unsigned int msg_len)39 init_netlink_diag_msg(struct nlmsghdr *const nlh, const unsigned int msg_len)
40 {
41 	SET_STRUCT(struct nlmsghdr, nlh,
42 		.nlmsg_len = msg_len,
43 		.nlmsg_type = SOCK_DIAG_BY_FAMILY,
44 		.nlmsg_flags = NLM_F_DUMP
45 	);
46 
47 	struct netlink_diag_msg *const msg = NLMSG_DATA(nlh);
48 	SET_STRUCT(struct netlink_diag_msg, msg,
49 		.ndiag_family = AF_NETLINK,
50 		.ndiag_type = SOCK_RAW,
51 		.ndiag_protocol = NETLINK_ROUTE,
52 		.ndiag_state = NETLINK_CONNECTED
53 	);
54 }
55 
56 static void
print_netlink_diag_msg(const unsigned int msg_len)57 print_netlink_diag_msg(const unsigned int msg_len)
58 {
59 	printf("{len=%u, type=SOCK_DIAG_BY_FAMILY"
60 	       ", flags=NLM_F_DUMP, seq=0, pid=0}, {ndiag_family=AF_NETLINK"
61 	       ", ndiag_type=SOCK_RAW, ndiag_protocol=NETLINK_ROUTE"
62 	       ", ndiag_state=NETLINK_CONNECTED, ndiag_portid=0"
63 	       ", ndiag_dst_portid=0, ndiag_dst_group=0, ndiag_ino=0"
64 	       ", ndiag_cookie=[0, 0]}",
65 	       msg_len);
66 }
67 
68 static void
print_xlong(const unsigned long * p,size_t i)69 print_xlong(const unsigned long *p, size_t i)
70 {
71 	printf("%#lx", *p);
72 }
73 
74 int
main(void)75 main(void)
76 {
77 	skip_if_unavailable("/proc/self/fd/");
78 
79 	static const unsigned long groups[] = {
80 		(unsigned long) 0xdeadbeefbadc0dedULL,
81 		(unsigned long) 0xdeadbeefbadc0dedULL
82 	};
83 	static const struct netlink_diag_ring ndr = {
84 		.ndr_block_size = 0xfabfabdc,
85 		.ndr_block_nr = 0xabcdabda,
86 		.ndr_frame_size = 0xcbadbafa,
87 		.ndr_frame_nr = 0xdbcafadb
88 	};
89 	static const uint32_t flags =
90 		NDIAG_FLAG_CB_RUNNING | NDIAG_FLAG_PKTINFO;
91 
92 	const int fd = create_nl_socket(NETLINK_SOCK_DIAG);
93 	const unsigned int hdrlen = sizeof(struct netlink_diag_msg);
94 	void *const nlh0 = midtail_alloc(NLMSG_SPACE(hdrlen),
95 					 NLA_HDRLEN +
96 					 MAX(sizeof(groups), sizeof(ndr)));
97 
98 	static char pattern[4096];
99 	fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1);
100 
101 	TEST_NLATTR_ARRAY(fd, nlh0, hdrlen,
102 			  init_netlink_diag_msg, print_netlink_diag_msg,
103 			  NETLINK_DIAG_GROUPS, pattern, groups, print_xlong);
104 
105 	TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
106 			   init_netlink_diag_msg, print_netlink_diag_msg,
107 			   NETLINK_DIAG_RX_RING, pattern, ndr,
108 			   PRINT_FIELD_U("{", ndr, ndr_block_size);
109 			   PRINT_FIELD_U(", ", ndr, ndr_block_nr);
110 			   PRINT_FIELD_U(", ", ndr, ndr_frame_size);
111 			   PRINT_FIELD_U(", ", ndr, ndr_frame_nr);
112 			   printf("}"));
113 
114 	TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
115 			   init_netlink_diag_msg, print_netlink_diag_msg,
116 			   NETLINK_DIAG_FLAGS, pattern, flags,
117 			   printf("NDIAG_FLAG_CB_RUNNING|NDIAG_FLAG_PKTINFO"));
118 
119 	puts("+++ exited with 0 +++");
120 	return 0;
121 }
122