1 /*
2  * Copyright (c) International Business Machines Corp., 2001
3  * Copyright (c) 2017 Petr Vorel <pvorel@suse.cz>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of
8  * the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it would be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * Author: David L Stevens
19  */
20 
21 /*
22  * Description: Verify that in6 and sockaddr fields are present.
23  */
24 
25 #include <errno.h>
26 #include <arpa/inet.h>
27 #include <netinet/in.h>
28 #include <sys/socket.h>
29 
30 #include "tst_test.h"
31 #include "tst_safe_macros.h"
32 
33 static struct {
34 	char *addr;
35 	int ismap;
36 } maptab[] = {
37 	{ "2002::1", 0 },
38 	{ "::ffff:10.0.0.1", 1 },
39 	{ "::fffe:10.0.0.1", 0 },
40 	{ "::7fff:10.0.0.1", 0 },
41 	{ "0:0:0:0:0:0:ffff:a001", 0 },
42 	{ "0:0:1:0:0:0:ffff:a001", 0 },
43 };
44 
45 static struct {
46 	char *addr;
47 } sstab[] = {
48 	{ "2002::1" },
49 	{ "10.0.0.1" },
50 	{ "::ffff:10.0.0.1" },
51 	{ "::1" },
52 	{ "::" },
53 };
54 
55 static void test_in6_addr(void);
56 static void test_sockaddr_in6(void);
57 static void test_global_in6_def(void);
58 static void test_in6_is_addr_v4mapped(void);
59 static void test_sockaddr_storage(void);
60 
61 static void (*testfunc[])(void) = { test_in6_addr,
62 	test_sockaddr_in6, test_global_in6_def,
63 	test_in6_is_addr_v4mapped, test_sockaddr_storage };
64 
65 /* struct in6_addr tests */
test_in6_addr(void)66 static void test_in6_addr(void)
67 {
68 	uint8_t ui8 = 1;
69 	struct in6_addr in6;
70 
71 	in6.s6_addr[0] = ui8;
72 	tst_res(TINFO, "type of in6.s6_addr[0] is uint8_t");
73 	if (sizeof(in6.s6_addr) != 16)
74 		tst_res(TFAIL, "sizeof(in6.s6_addr) != 16");
75 	else
76 		tst_res(TPASS, "sizeof(in6.s6_addr) == 16");
77 }
78 
79 /* struct sockaddr_in6 tests */
test_sockaddr_in6(void)80 static void test_sockaddr_in6(void)
81 {
82 	uint8_t ui8 = 1;
83 	uint32_t ui16 = 2;
84 	uint32_t ui32 = 3;
85 	struct in6_addr in6;
86 	struct sockaddr_in6 sin6;
87 	int sd;
88 
89 	in6.s6_addr[0] = ui8;
90 	sin6.sin6_family = AF_INET6;
91 	sin6.sin6_port = ui16;
92 	sin6.sin6_flowinfo = ui32;
93 	sin6.sin6_addr = in6;
94 	sin6.sin6_scope_id = ui32;
95 
96 	sd = SAFE_SOCKET(AF_INET6, SOCK_STREAM, 0);
97 	bind(sd, (struct sockaddr *)&sin6, sizeof(sin6));
98 	SAFE_CLOSE(sd);
99 
100 	tst_res(TPASS, "all sockaddr_in6 fields present and correct");
101 }
102 
103 /* initializers and global in6 definitions tests */
test_global_in6_def(void)104 static void test_global_in6_def(void)
105 {
106 	struct in6_addr ina6 = IN6ADDR_ANY_INIT;
107 	struct in6_addr inl6 = IN6ADDR_LOOPBACK_INIT;
108 
109 	tst_res(TINFO, "IN6ADDR_ANY_INIT present");
110 	if (memcmp(&ina6, &in6addr_any, sizeof(ina6)) == 0)
111 		tst_res(TINFO, "in6addr_any present and correct");
112 	else {
113 		tst_res(TFAIL, "in6addr_any incorrect value");
114 		return;
115 	}
116 
117 	tst_res(TINFO, "IN6ADDR_LOOPBACK_INIT present");
118 	if (memcmp(&inl6, &in6addr_loopback, sizeof(inl6)) == 0)
119 		tst_res(TINFO, "in6addr_loopback present and correct");
120 	else {
121 		tst_res(TFAIL, "in6addr_loopback incorrect value");
122 		return;
123 	}
124 
125 	if (inet_pton(AF_INET6, "::1", &inl6) <= 0)
126 		tst_brk(TBROK | TERRNO, "inet_pton(\"::1\")");
127 
128 	if (memcmp(&inl6, &in6addr_loopback, sizeof(inl6)) == 0)
129 		tst_res(TINFO, "in6addr_loopback in network byte order");
130 	else {
131 		tst_res(TFAIL, "in6addr_loopback has wrong byte order");
132 		return;
133 	}
134 
135 	tst_res(TPASS, "global in6 definitions tests succeed");
136 }
137 
138 /* IN6_IS_ADDR_V4MAPPED tests */
test_in6_is_addr_v4mapped(void)139 static void test_in6_is_addr_v4mapped(void)
140 {
141 	unsigned int i;
142 	struct in6_addr in6;
143 
144 	for (i = 0; i < ARRAY_SIZE(maptab); ++i) {
145 		if (inet_pton(AF_INET6, maptab[i].addr, &in6) <= 0)
146 			tst_brk(TBROK | TERRNO,
147 				"\"%s\" is not a valid IPv6 address",
148 				maptab[i].addr);
149 		TEST(IN6_IS_ADDR_V4MAPPED(in6.s6_addr));
150 		if (maptab[i].ismap == TST_RET)
151 			tst_res(TINFO, "IN6_IS_ADDR_V4MAPPED(\"%s\") %ld",
152 				maptab[i].addr, TST_RET);
153 		else {
154 			tst_res(TFAIL, "IN6_IS_ADDR_V4MAPPED(\"%s\") %ld",
155 				maptab[i].addr, TST_RET);
156 			return;
157 		}
158 	}
159 
160 	tst_res(TPASS, "IN6_IS_ADDR_V4MAPPED tests succeed");
161 }
162 
163 /* sockaddr_storage tests */
test_sockaddr_storage(void)164 static void test_sockaddr_storage(void)
165 {
166 	unsigned int i;
167 	struct sockaddr_storage ss;
168 
169 	if (sizeof(ss) <= sizeof(struct sockaddr_in) ||
170 		sizeof(ss) <= sizeof(struct sockaddr_in6))
171 		tst_brk(TBROK, "sockaddr_storage too small");
172 
173 	for (i = 0; i < ARRAY_SIZE(sstab); ++i) {
174 		struct sockaddr_in *psin = (struct sockaddr_in *)&ss;
175 		struct sockaddr_in6 *psin6 = (struct sockaddr_in6 *)&ss;
176 		int rv;
177 		uint8_t af;
178 
179 		af = psin->sin_family = AF_INET;
180 		rv = inet_pton(AF_INET, sstab[i].addr, &psin->sin_addr);
181 		if (rv == 0) {
182 			af = psin6->sin6_family = AF_INET6;
183 			rv = inet_pton(AF_INET6, sstab[i].addr,
184 				&psin6->sin6_addr);
185 		}
186 		if (rv <= 0)
187 			tst_brk(TBROK,
188 				"\"%s\" is not a valid address", sstab[i].addr);
189 		if (ss.ss_family == af)
190 			tst_res(TINFO, "\"%s\" is AF_INET%s",
191 				sstab[i].addr, af == AF_INET ? "" : "6");
192 		else
193 			tst_res(TFAIL, "\"%s\" ss_family (%d) != AF_INET%s",
194 				sstab[i].addr, af, af == AF_INET ? "" : "6");
195 	}
196 
197 	tst_res(TPASS, "sockaddr_storage tests succeed");
198 }
199 
do_test(unsigned int i)200 static void do_test(unsigned int i)
201 {
202 	testfunc[i]();
203 }
204 
205 static struct tst_test test = {
206 	.tcnt = ARRAY_SIZE(testfunc),
207 	.test = do_test,
208 };
209