• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <netinet/in.h>
33 #include <arpa/inet.h>
34 #include "test_nlattr.h"
35 #ifdef HAVE_LINUX_NEIGHBOUR_H
36 # include <linux/neighbour.h>
37 #endif
38 #include <linux/rtnetlink.h>
39 
40 #define NDA_PORT 6
41 
42 static void
init_ndmsg(struct nlmsghdr * const nlh,const unsigned int msg_len)43 init_ndmsg(struct nlmsghdr *const nlh, const unsigned int msg_len)
44 {
45 	SET_STRUCT(struct nlmsghdr, nlh,
46 		.nlmsg_len = msg_len,
47 		.nlmsg_type = RTM_GETNEIGH,
48 		.nlmsg_flags = NLM_F_DUMP
49 	);
50 
51 	struct ndmsg *const msg = NLMSG_DATA(nlh);
52 	SET_STRUCT(struct ndmsg, msg,
53 		.ndm_family = AF_UNIX,
54 		.ndm_ifindex = ifindex_lo(),
55 		.ndm_state = NUD_PERMANENT,
56 		.ndm_flags = NTF_PROXY,
57 		.ndm_type = RTN_UNSPEC
58 	);
59 }
60 
61 static void
print_ndmsg(const unsigned int msg_len)62 print_ndmsg(const unsigned int msg_len)
63 {
64 	printf("{len=%u, type=RTM_GETNEIGH, flags=NLM_F_DUMP"
65 	       ", seq=0, pid=0}, {ndm_family=AF_UNIX"
66 	       ", ndm_ifindex=" IFINDEX_LO_STR
67 	       ", ndm_state=NUD_PERMANENT"
68 	       ", ndm_flags=NTF_PROXY"
69 	       ", ndm_type=RTN_UNSPEC}",
70 	       msg_len);
71 }
72 
73 int
main(void)74 main(void)
75 {
76 	skip_if_unavailable("/proc/self/fd/");
77 
78 	const int fd = create_nl_socket(NETLINK_ROUTE);
79 	const unsigned int hdrlen = sizeof(struct ndmsg);
80 	void *nlh0 = midtail_alloc(NLMSG_SPACE(hdrlen),
81 				   NLA_HDRLEN + sizeof(struct nda_cacheinfo));
82 
83 	static char pattern[4096];
84 	fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1);
85 
86 	const unsigned int nla_type = 0xffff & NLA_TYPE_MASK;
87 	char nla_type_str[256];
88 	sprintf(nla_type_str, "%#x /* NDA_??? */", nla_type);
89 	TEST_NLATTR_(fd, nlh0, hdrlen,
90 		     init_ndmsg, print_ndmsg,
91 		     nla_type, nla_type_str,
92 		     4, pattern, 4,
93 		     print_quoted_hex(pattern, 4));
94 
95 	TEST_NLATTR(fd, nlh0, hdrlen,
96 		    init_ndmsg, print_ndmsg,
97 		    NDA_DST, 4, pattern, 4,
98 		    print_quoted_hex(pattern, 4));
99 
100 	static const struct nda_cacheinfo ci = {
101 		.ndm_confirmed = 0xabcdedad,
102 		.ndm_used = 0xbcdaedad,
103 		.ndm_updated = 0xcdbadeda,
104 		.ndm_refcnt = 0xdeadbeda
105 	};
106 
107 	TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
108 			   init_ndmsg, print_ndmsg,
109 			   NDA_CACHEINFO, pattern, ci,
110 			   PRINT_FIELD_U("{", ci, ndm_confirmed);
111 			   PRINT_FIELD_U(", ", ci, ndm_used);
112 			   PRINT_FIELD_U(", ", ci, ndm_updated);
113 			   PRINT_FIELD_U(", ", ci, ndm_refcnt);
114 			   printf("}"));
115 
116 	const uint16_t port = 0xabcd;
117 	TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
118 			   init_ndmsg, print_ndmsg,
119 			   NDA_PORT, pattern, port,
120 			   printf("htons(%u)", ntohs(port)));
121 
122 	puts("+++ exited with 0 +++");
123 	return 0;
124 }
125