1/*
2 * Copyright (c) 2017 JingPiao Chen <chenjingpiao@gmail.com>
3 * Copyright (c) 2017 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
38static void
39init_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
56static void
57print_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
68static void
69print_xlong(const unsigned long *p)
70{
71	printf("%#lx", *p);
72}
73
74int
75main(void)
76{
77	skip_if_unavailable("/proc/self/fd/");
78
79	const int fd = create_nl_socket(NETLINK_SOCK_DIAG);
80	const unsigned int hdrlen = sizeof(struct netlink_diag_msg);
81	void *const nlh0 = tail_alloc(NLMSG_SPACE(hdrlen));
82
83	static char pattern[4096];
84	fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1);
85
86	static const unsigned long groups[] = {
87		(unsigned long) 0xdeadbeefbadc0dedULL,
88		(unsigned long) 0xdeadbeefbadc0dedULL
89	};
90	TEST_NLATTR_ARRAY(fd, nlh0, hdrlen,
91			  init_netlink_diag_msg, print_netlink_diag_msg,
92			  NETLINK_DIAG_GROUPS, pattern, groups, print_xlong);
93
94	static const struct netlink_diag_ring ndr = {
95		.ndr_block_size = 0xfabfabdc,
96		.ndr_block_nr = 0xabcdabda,
97		.ndr_frame_size = 0xcbadbafa,
98		.ndr_frame_nr = 0xdbcafadb
99	};
100	TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
101			   init_netlink_diag_msg, print_netlink_diag_msg,
102			   NETLINK_DIAG_RX_RING, pattern, ndr,
103			   PRINT_FIELD_U("{", ndr, ndr_block_size);
104			   PRINT_FIELD_U(", ", ndr, ndr_block_nr);
105			   PRINT_FIELD_U(", ", ndr, ndr_frame_size);
106			   PRINT_FIELD_U(", ", ndr, ndr_frame_nr);
107			   printf("}"));
108
109	static const uint32_t flags =
110		NDIAG_FLAG_CB_RUNNING | NDIAG_FLAG_PKTINFO;
111	TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
112			   init_netlink_diag_msg, print_netlink_diag_msg,
113			   NETLINK_DIAG_FLAGS, pattern, flags,
114			   printf("NDIAG_FLAG_CB_RUNNING|NDIAG_FLAG_PKTINFO"));
115
116	puts("+++ exited with 0 +++");
117	return 0;
118}
119