• 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 <arpa/inet.h>
33 #include "test_nlattr.h"
34 #ifdef HAVE_LINUX_IF_ADDR_H
35 # include <linux/if_addr.h>
36 #endif
37 #include <linux/rtnetlink.h>
38 
39 #define IFA_FLAGS 8
40 
41 #define SET_IFA_FAMILY(af)		\
42 	do {				\
43 		ifa_family = af;	\
44 		ifa_family_str = #af;	\
45 	}				\
46 	while (0)
47 
48 uint8_t ifa_family;
49 const char *ifa_family_str;
50 
51 static void
init_ifaddrmsg(struct nlmsghdr * const nlh,const unsigned int msg_len)52 init_ifaddrmsg(struct nlmsghdr *const nlh, const unsigned int msg_len)
53 {
54 	SET_STRUCT(struct nlmsghdr, nlh,
55 		.nlmsg_len = msg_len,
56 		.nlmsg_type = RTM_GETADDR,
57 		.nlmsg_flags = NLM_F_DUMP
58 	);
59 
60 	struct ifaddrmsg *const msg = NLMSG_DATA(nlh);
61 	SET_STRUCT(struct ifaddrmsg, msg,
62 		.ifa_family = ifa_family,
63 		.ifa_flags = IFA_F_SECONDARY,
64 		.ifa_scope = RT_SCOPE_UNIVERSE,
65 		.ifa_index = ifindex_lo()
66 	);
67 }
68 
69 static void
print_ifaddrmsg(const unsigned int msg_len)70 print_ifaddrmsg(const unsigned int msg_len)
71 {
72 	printf("{len=%u, type=RTM_GETADDR, flags=NLM_F_DUMP"
73 	       ", seq=0, pid=0}, {ifa_family=%s"
74 	       ", ifa_prefixlen=0"
75 	       ", ifa_flags=IFA_F_SECONDARY"
76 	       ", ifa_scope=RT_SCOPE_UNIVERSE"
77 	       ", ifa_index=" IFINDEX_LO_STR "}",
78 	       msg_len, ifa_family_str);
79 }
80 
81 int
main(void)82 main(void)
83 {
84 	skip_if_unavailable("/proc/self/fd/");
85 
86 	static const char address4[] = "12.34.56.78";
87 	static const char address6[] = "12:34:56:78:90:ab:cd:ef";
88 	static const struct ifa_cacheinfo ci = {
89 		.ifa_prefered = 0xabcdefac,
90 		.ifa_valid = 0xbcdadbca,
91 		.cstamp = 0xcdabedba,
92 		.tstamp = 0xdebabdac
93 	};
94 
95 	struct in_addr a4;
96 	struct in6_addr a6;
97 	const uint32_t ifa_flags = IFA_F_SECONDARY | IFA_F_PERMANENT;
98 
99 	const int fd = create_nl_socket(NETLINK_ROUTE);
100 	const unsigned int hdrlen = sizeof(struct ifaddrmsg);
101 	void *nlh0 = midtail_alloc(NLMSG_SPACE(hdrlen),
102 				   NLA_HDRLEN + MAX(sizeof(ci), sizeof(a6)));
103 
104 	static char pattern[4096];
105 	fill_memory_ex(pattern, sizeof(pattern), 'a', 'z' - 'a' + 1);
106 
107 	SET_IFA_FAMILY(AF_UNSPEC);
108 	const unsigned int nla_type = 0xffff & NLA_TYPE_MASK;
109 	char nla_type_str[256];
110 	sprintf(nla_type_str, "%#x /* IFA_??? */", nla_type);
111 	TEST_NLATTR_(fd, nlh0, hdrlen,
112 		     init_ifaddrmsg, print_ifaddrmsg,
113 		     nla_type, nla_type_str,
114 		     4, pattern, 4,
115 		     print_quoted_hex(pattern, 4));
116 
117 	TEST_NLATTR(fd, nlh0, hdrlen,
118 		    init_ifaddrmsg, print_ifaddrmsg,
119 		    IFA_ADDRESS, 4, pattern, 4,
120 		    print_quoted_hex(pattern, 4));
121 
122 	SET_IFA_FAMILY(AF_INET);
123 
124 	if (!inet_pton(AF_INET, address4, &a4))
125 		perror_msg_and_skip("inet_pton");
126 
127 	TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
128 			   init_ifaddrmsg, print_ifaddrmsg,
129 			   IFA_ADDRESS, pattern, a4,
130 			   printf("%s", address4));
131 
132 	SET_IFA_FAMILY(AF_INET6);
133 
134 	if (!inet_pton(AF_INET6, address6, &a6))
135 		perror_msg_and_skip("inet_pton");
136 
137 	TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
138 			   init_ifaddrmsg, print_ifaddrmsg,
139 			   IFA_ADDRESS, pattern, a6,
140 			   printf("%s", address6));
141 
142 	TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
143 			   init_ifaddrmsg, print_ifaddrmsg,
144 			   IFA_CACHEINFO, pattern, ci,
145 			   PRINT_FIELD_U("{", ci, ifa_prefered);
146 			   PRINT_FIELD_U(", ", ci, ifa_valid);
147 			   PRINT_FIELD_U(", ", ci, cstamp);
148 			   PRINT_FIELD_U(", ", ci, tstamp);
149 			   printf("}"));
150 
151 	TEST_NLATTR_OBJECT(fd, nlh0, hdrlen,
152 			   init_ifaddrmsg, print_ifaddrmsg,
153 			   IFA_FLAGS, pattern, ifa_flags,
154 			   printf("IFA_F_SECONDARY|IFA_F_PERMANENT"));
155 
156 	puts("+++ exited with 0 +++");
157 	return 0;
158 }
159