• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2017 Petr Vorel <pvorel@suse.cz>
4  * Copyright (c) 1997-2015 Red Hat, Inc. All rights reserved.
5  * Copyright (c) 2011-2013 Rich Felker, et al.
6  */
7 
8 #include <arpa/inet.h>
9 #include <arpa/nameser.h>
10 #include <assert.h>
11 #include <errno.h>
12 #include <linux/rtnetlink.h>
13 #include <net/if.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <time.h>
18 #include <stdarg.h>
19 
20 
21 #define TST_NO_DEFAULT_MAIN
22 #include "tst_test.h"
23 
24 #include "tst_net.h"
25 #include "tst_private.h"
26 
27 #define BASE_IPV4_PREFIX 8
28 #define BASE_IPV6_PREFIX 16
29 
30 #define MAX_IPV4_PREFIX 32
31 #define MAX_IPV6_PREFIX 128
32 
33 #define DEFAULT_IPV4_UNUSED_PART1 10
34 #define DEFAULT_IPV6_UNUSED_PART1 0xfd
35 
36 #define DEFAULT_IPV4_UNUSED_PART2 23
37 #define DEFAULT_IPV6_UNUSED_PART2 0x23
38 
39 struct ltp_net_variables {
40 	char *ipv4_lbroadcast;
41 	char *ipv4_rbroadcast;
42 	char *ipv4_lnetmask;
43 	char *ipv4_rnetmask;
44 	char *ipv4_lnetwork;
45 	char *ipv4_rnetwork;
46 	char *lhost_ipv4_host;
47 	char *rhost_ipv4_host;
48 	char *ipv6_lnetmask;
49 	char *ipv6_rnetmask;
50 	char *ipv6_lnetwork;
51 	char *ipv6_rnetwork;
52 	char *lhost_ipv6_host;
53 	char *rhost_ipv6_host;
54 	char *ipv4_net16_unused;
55 	char *ipv6_net32_unused;
56 };
57 static struct ltp_net_variables vars;
58 
usage(const char * cmd)59 static void usage(const char *cmd)
60 {
61 	fprintf(stderr, "USAGE:\n"
62 		"%s IP_LHOST[/PREFIX] IP_RHOST[/PREFIX]\n"
63 		"%s -h\n\n"
64 		"Exported variables:\n"
65 		"IPV4_LBROADCAST: IPv4 broadcast of the local host\n"
66 		"IPV4_RBROADCAST: IPv4 broadcast of the remote host\n"
67 		"IPV4_LNETMASK: IPv4 netmask of the local host\n"
68 		"IPV4_RNETMASK: IPv4 netmask of the remote host\n"
69 		"IPV4_LNETWORK: IPv4 network part of IPV4_LHOST\n"
70 		"IPV4_RNETWORK: IPv4 network part of IPV4_RHOST\n"
71 		"LHOST_IPV4_HOST IPv4 host part of IPV4_LHOST\n"
72 		"RHOST_IPV4_HOST IPv4 host part of IPV4_RHOST\n"
73 		"IPV4_NET16_UNUSED: IPv4 16 bit unused subnet\n"
74 		"IPV6_LNETMASK: IPv6 netmask of the local host\n"
75 		"IPV6_RNETMASK: IPv6 netmask of the remote host\n"
76 		"IPV6_LNETWORK: IPv6 network part of IPV6_LHOST\n"
77 		"IPV6_RNETWORK: IPv6 network part of IPV6_LHOST\n"
78 		"LHOST_IPV6_HOST: IPv6 unique part of IPV6_LHOST\n"
79 		"RHOST_IPV6_HOST: IPv6 unique part of IPV6_RHOST\n"
80 		"IPV6_NET32_UNUSED: IPv6 32 bit unused subnet\n\n"
81 		"NOTE: Prefixes, ifaces and lhosts are expected to be set by tst_net_iface_prefix.\n"
82 		"OPTIONS:\n"
83 		"-h this help\n",
84 		cmd, cmd);
85 }
86 
87 /*
88  * Function prefix2mask is from ipcalc project, ipcalc.c.
89  */
prefix2mask(unsigned int prefix)90 static struct in_addr prefix2mask(unsigned int prefix)
91 {
92 	struct in_addr mask;
93 
94 	memset(&mask, 0x0, sizeof(mask));
95 
96 	if (prefix)
97 		mask.s_addr = htonl(~((1 << (32 - prefix)) - 1));
98 	else
99 		mask.s_addr = htonl(0);
100 
101 	return mask;
102 }
103 
104 /*
105  * Function calc_network is based on ipcalc project,
106  * calc_network/ipcalc.c.
107  */
calc_network(const struct in_addr * ip,struct in_addr * mask)108 static struct in_addr calc_network(const struct in_addr *ip,
109 	struct in_addr *mask)
110 {
111 	struct in_addr network;
112 
113 	memset(&network, 0, sizeof(network));
114 
115 	network.s_addr = ip->s_addr & mask->s_addr;
116 	return network;
117 }
118 
is_in_subnet_ipv4(const struct in_addr * network,const struct in_addr * mask,const struct in_addr * ip)119 static int is_in_subnet_ipv4(const struct in_addr *network,
120 	const struct in_addr *mask, const struct in_addr *ip)
121 {
122 	return (ip->s_addr & mask->s_addr) ==
123 		(network->s_addr & mask->s_addr);
124 }
125 
is_in_subnet_ipv6(const struct in6_addr * network,const struct in6_addr * mask,const struct in6_addr * ip6)126 static int is_in_subnet_ipv6(const struct in6_addr *network,
127 	const struct in6_addr *mask, const struct in6_addr *ip6)
128 {
129 	unsigned int i;
130 
131 	for (i = 0; i < sizeof(struct in6_addr) / sizeof(int); i++) {
132 		if (((((int *) ip6)[i] & ((int *) mask)[i])) !=
133 			(((int *) network)[i] & ((int *) mask)[i]))
134 			return 0;
135 	}
136 	return 1;
137 }
138 
139 /*
140  * Function get_ipv4_netmask uses code from calc_network, which is from
141  * ipcalc project, ipcalc.c.
142  */
get_ipv4_netmask(unsigned int prefix)143 static char *get_ipv4_netmask(unsigned int prefix)
144 {
145 	char buf[INET_ADDRSTRLEN];
146 	struct in_addr mask = prefix2mask(prefix);
147 
148 	if (prefix > MAX_IPV4_PREFIX)
149 		return NULL;
150 
151 	if (!inet_ntop(AF_INET, &mask, buf, sizeof(buf)))
152 		tst_brk_comment("error calculating IPv4 address");
153 
154 	return strdup(buf);
155 }
156 
157 /*
158  * Function get_ipv4_netmask uses code from ipv6_prefix_to_mask and
159  * ipv6_mask_to_str, which are from ipcalc project, ipcalc.c.
160  */
get_ipv6_netmask(unsigned int prefix)161 static char *get_ipv6_netmask(unsigned int prefix)
162 {
163 	struct in6_addr in6;
164 	char buf[128];
165 	int i, j;
166 
167 	if (prefix > MAX_IPV6_PREFIX)
168 		return NULL;
169 
170 	memset(&in6, 0x0, sizeof(in6));
171 	for (i = prefix, j = 0; i > 0; i -= 8, j++) {
172 		if (i >= 8)
173 			in6.s6_addr[j] = 0xff;
174 		else
175 			in6.s6_addr[j] = (unsigned long)(0xffU << (8 - i));
176 	}
177 
178 	if (!inet_ntop(AF_INET6, &in6, buf, sizeof(buf)))
179 		tst_brk_comment("error calculating IPv6 address");
180 
181 	return strdup(buf);
182 }
183 
184 /*
185  * Function get_ipv4_broadcast uses code from calc_broadcast, which is from
186  * ipcalc project, ipcalc.c.
187  */
get_ipv4_broadcast(struct in_addr ip,unsigned int prefix)188 static char *get_ipv4_broadcast(struct in_addr ip, unsigned int prefix)
189 {
190 	struct in_addr mask = prefix2mask(prefix);
191 	struct in_addr broadcast;
192 	char buf[INET_ADDRSTRLEN];
193 
194 	memset(&broadcast, 0, sizeof(broadcast));
195 	broadcast.s_addr = (ip.s_addr & mask.s_addr) | ~mask.s_addr;
196 
197 	if (!inet_ntop(AF_INET, &broadcast, buf, sizeof(buf)))
198 		tst_brk_comment("error calculating IPv4 address");
199 
200 	return strdup(buf);
201 }
202 
203 /*
204  * For unused network we use
205  * DEFAULT_IPV4_UNUSED_PART1:DEFAULT_IPV4_UNUSED_PART2 or
206  * {DEFAULT_IPV4_UNUSED_PART1}.XY, when there is a collision with IP.
207  */
get_ipv4_net16_unused(const struct in_addr * ip,unsigned int prefix)208 static char *get_ipv4_net16_unused(const struct in_addr *ip,
209 	unsigned int prefix)
210 {
211 	struct in_addr mask, network;
212 	char buf[132], net_unused[128];
213 
214 	mask = prefix2mask(prefix);
215 	network = calc_network(ip, &mask);
216 
217 	sprintf(net_unused, "%d.%d", DEFAULT_IPV4_UNUSED_PART1,
218 			DEFAULT_IPV4_UNUSED_PART2);
219 	sprintf(buf, "%s.0.0", net_unused);
220 
221 	tst_get_in_addr(buf, &network);
222 
223 	if (!is_in_subnet_ipv4(ip, &mask, &network))
224 		return strdup(net_unused);
225 
226 	srand(time(NULL));
227 
228 	/* try to randomize second group */
229 	sprintf(net_unused, "%d.%d", DEFAULT_IPV4_UNUSED_PART1,
230 		(rand() % 128) + (((ip->s_addr >> 8) & 0xff) < 128 ? 128 : 0));
231 	sprintf(buf, "%s.0.0", net_unused);
232 
233 	tst_get_in_addr(buf, &network);
234 
235 	if (!is_in_subnet_ipv4(ip, &mask, &network))
236 		return strdup(net_unused);
237 
238 	/* try to randomize first group */
239 	sprintf(net_unused, "%d.%d", (rand() % 128) + (((ip->s_addr) & 0xff)
240 			< 128 ? 128 : 0), DEFAULT_IPV4_UNUSED_PART2);
241 	sprintf(buf, "%s.0.0", net_unused);
242 
243 	tst_get_in_addr(buf, &network);
244 
245 	if (!is_in_subnet_ipv4(ip, &mask, &network))
246 		return strdup(net_unused);
247 
248 	return NULL;
249 }
250 
251 /*
252  * Function get_ipv6_net32_unused is inspired by ipcalc project,
253  * get_ipv6_info/ipcalc.c.
254  *
255  * For unused network we use DEFAULT_IPV6_UNUSED_PART1:DEFAULT_IPV6_UNUSED_PART2
256  * if no collision with existing IP range.
257  * Otherwise we try to use
258  * {DEFAULT_IPV6_UNUSED_PART1}XY:DEFAULT_IPV6_UNUSED_PART2 or
259  * XY:DEFAULT_IPV6_UNUSED_PART2.
260  */
get_ipv6_net32_unused(const struct in6_addr * ip6,unsigned int prefix)261 static char *get_ipv6_net32_unused(const struct in6_addr *ip6,
262 	unsigned int prefix)
263 {
264 	int i, j;
265 	struct in6_addr mask, network;
266 	char buf[130], net_unused[128];
267 
268 	memset(&mask, 0x0, sizeof(mask));
269 
270 	if (prefix > 128)
271 		return NULL;
272 
273 
274 	for (i = prefix, j = 0; i > 0; i -= 8, j++) {
275 		if (i >= 8)
276 			mask.s6_addr[j] = 0xff;
277 		else
278 			mask.s6_addr[j] = (unsigned long)(0xffU << (8 - i));
279 	}
280 
281 	sprintf(net_unused, "%x:%x", 256 * DEFAULT_IPV6_UNUSED_PART1,
282 			DEFAULT_IPV6_UNUSED_PART2);
283 	sprintf(buf, "%s::", net_unused);
284 
285 	tst_get_in6_addr(buf, &network);
286 
287 	if (!is_in_subnet_ipv6(ip6, &mask, &network))
288 		return strdup(net_unused);
289 
290 	srand(time(NULL));
291 
292 	/* try to randomize second group */
293 	sprintf(net_unused, "%x:%x", 256 * DEFAULT_IPV6_UNUSED_PART1 +
294 			(rand() % 128) + (ip6->s6_addr[1] < 128 ? 128 : 0),
295 			DEFAULT_IPV6_UNUSED_PART2);
296 	sprintf(buf, "%s::", net_unused);
297 
298 	tst_get_in6_addr(buf, &network);
299 
300 	if (!is_in_subnet_ipv6(ip6, &mask, &network))
301 		return strdup(net_unused);
302 
303 	/* try to randomize first group */
304 	sprintf(net_unused, "%x:%x",
305 			256 * (rand() % 128) + (256 * ip6->s6_addr[0] < 128 ?
306 			128 : 0), DEFAULT_IPV6_UNUSED_PART2);
307 	sprintf(buf, "%s::", net_unused);
308 
309 	tst_get_in6_addr(buf, &network);
310 
311 	if (!is_in_subnet_ipv6(ip6, &mask, &network))
312 		return strdup(net_unused);
313 
314 	return NULL;
315 }
316 
317 /*
318  * Function get_ipv6_network is based on musl libc project,
319  * inet_ntop/inet_ntop.c.
320  */
get_ipv6_network(const unsigned char * a0,unsigned int prefix)321 static char *get_ipv6_network(const unsigned char *a0, unsigned int prefix)
322 {
323 	const unsigned char *a = a0;
324 	unsigned int i, j, max, best, border = 0;
325 	char buf[100];
326 	char ret[100];
327 	char tmp[100];
328 	char *p_ret = ret;
329 	char *p_tmp = tmp;
330 	size_t offset;
331 
332 	if (prefix > MAX_IPV6_PREFIX)
333 		return NULL;
334 
335 	if (prefix == MAX_IPV6_PREFIX)
336 		return strdup("\0");
337 
338 	snprintf(buf, sizeof(buf),
339 		"%x:%x:%x:%x:%x:%x:%x:%x",
340 		256 * a[0] + a[1], 256 * a[2] + a[3],
341 		256 * a[4] + a[5], 256 * a[6] + a[7],
342 		256 * a[8] + a[9], 256 * a[10] + a[11],
343 		256 * a[12] + a[13], 256 * a[14] + a[15]);
344 
345 	for (i = 0; i < 8; i++) {
346 		if (i < prefix >> 4) {
347 			border += sprintf(p_tmp, "%x", 256 * a[2 * i] +
348 				a[2 * i + 1]);
349 			if (i > 0)
350 				border++;
351 		}
352 
353 		if (i >= prefix >> 4)
354 			break;
355 
356 		/* ':' only if no leading in host or ending in net */
357 		if (i > 0)
358 			*p_ret++ = ':';
359 
360 		offset = sprintf(p_ret, "%x", 256 * a[2 * i] + a[2 * i + 1]);
361 		p_ret += offset;
362 	}
363 
364 	*p_ret = '\0';
365 
366 	/* Find longest /(^0|:)[:0]{2,}/ */
367 	for (i = best = 0, max = 2; buf[i]; i++) {
368 		if (i && buf[i] != ':')
369 			continue;
370 		j = strspn(buf + i, ":0");
371 
372 		if (j > max)
373 			best = i, max = j;
374 	}
375 
376 	size_t length = strlen(ret);
377 	size_t best_end = best + max - 1;
378 
379 	if (max > 2 && best < border) {
380 		p_ret = ret;
381 		/* Replace longest /(^0|:)[:0]{2,}/ with "::" */
382 		if (best == 0 && best_end >= border) {
383 			/* zeros in whole net part or continue to host */
384 			ret[0] = ':';
385 			ret[1] = '\0';
386 		} else if (best == 0 && best_end < border) {
387 			/* zeros on beginning, not whole part */
388 			ret[0] = ':';
389 			memmove(p_ret + 1, p_ret + best_end, border - best_end
390 				+ 1);
391 		} else if (best > 0 && best_end >= border) {
392 			/*
393 			 * zeros not from beginning to border or continue to
394 			 * host
395 			 */
396 			ret[best] = ':';
397 			ret[best + 1] = '\0';
398 		} else {
399 			/* zeros somewhere in the middle */
400 			ret[best] = ':';
401 			memmove(p_ret + best + 1, p_ret + best_end,
402 					border - best + 1);
403 		}
404 	}
405 
406 	if (length < INET6_ADDRSTRLEN)
407 		return strdup(ret);
408 
409 	return NULL;
410 }
411 
412 /*
413  * Strip host part from ip address.
414  */
get_host_from_ip(const char * ip,const char * net)415 static char *get_host_from_ip(const char *ip, const char *net)
416 {
417 	if (ip == NULL || net == NULL)
418 		return NULL;
419 
420 	char *result = strstr(ip, net);
421 
422 	if (!result || result != ip)
423 		return NULL;
424 
425 	char *buf = strdup(ip);
426 	unsigned int index = strlen(net);
427 	int len;
428 
429 	/* prefix < 8 (IPv4) or 128 (IPv6) */
430 	if (index == strlen(ip))
431 		return strdup("\0");
432 
433 	/* prefix > 0 && prefix < 32 (IPv4) or 128 (IPv6) */
434 	if (index > 0 && index < strlen(ip)) {
435 		len = strlen(ip) - index - 1;
436 		assert(ip[index] == ':' || ip[index] == '.');
437 		memmove(buf, buf + index + 1, len);
438 		buf[len] = '\0';
439 	}
440 
441 	return buf;
442 }
443 
check_prefix_range(unsigned int prefix,int is_ipv6,int is_lhost)444 static void check_prefix_range(unsigned int prefix, int is_ipv6, int is_lhost)
445 {
446 	unsigned int base_prefix = is_ipv6 ? BASE_IPV6_PREFIX :
447 		BASE_IPV4_PREFIX;
448 	unsigned int max_prefix = is_ipv6 ? MAX_IPV6_PREFIX : MAX_IPV4_PREFIX;
449 
450 	if (prefix < base_prefix || (is_ipv6 && prefix == 128) ||
451 		(!is_ipv6 && prefix == 32))
452 		tst_res_comment(TWARN,
453 			"prefix %d for %s will be unsuitable for some stress tests which need %s variable. To avoid this use prefix >= %d and prefix < %d.",
454 			prefix, is_ipv6 ?  "IPv6" : "IPv4",
455 			is_ipv6 ?
456 				(is_lhost ? "IPV6_LNETWORK" : "IPV6_RNETWORK") :
457 				(is_lhost ? "IPV4_LNETWORK" : "IPV4_RNETWORK"),
458 			base_prefix, max_prefix);
459 }
460 
get_ipv4_network(int ip,unsigned int prefix)461 static char *get_ipv4_network(int ip, unsigned int prefix)
462 {
463 	char buf[INET_ADDRSTRLEN];
464 	char *p_buf = buf;
465 	unsigned char byte;
466 	unsigned int i;
467 
468 	ip = htonl(ip);
469 
470 	if (prefix > MAX_IPV4_PREFIX)
471 		return NULL;
472 
473 	if (prefix == MAX_IPV4_PREFIX)
474 		return strdup("\0");
475 
476 	prefix &= MAX_IPV4_PREFIX - 8;
477 
478 	for (i = prefix; i > 0; i -= 8) {
479 		byte = (ip >> i) & 0xff;
480 		sprintf(p_buf, i < prefix ? ".%d" : "%d", byte);
481 		p_buf += strlen(p_buf);
482 	}
483 
484 	return strdup(buf);
485 }
486 
487 /*
488  * Round down prefix.
489  */
round_down_prefix(unsigned int prefix,int is_ipv6)490 static int round_down_prefix(unsigned int prefix, int is_ipv6)
491 {
492 	unsigned int base_prefix = is_ipv6 ? BASE_IPV6_PREFIX :
493 		BASE_IPV4_PREFIX;
494 
495 	return prefix / base_prefix * base_prefix;
496 }
497 
get_ipv4_info(const char * lip_str,const char * rip_str,int lprefix,int rprefix)498 static void get_ipv4_info(const char *lip_str, const char *rip_str, int lprefix,
499 	int rprefix)
500 {
501 	struct in_addr lip, rip;
502 	int lprefix_round, rprefix_round;
503 
504 	lprefix_round = round_down_prefix(lprefix, 0);
505 	rprefix_round = round_down_prefix(rprefix, 0);
506 
507 	tst_get_in_addr(lip_str, &lip);
508 	tst_get_in_addr(rip_str, &rip);
509 
510 	vars.ipv4_lbroadcast = get_ipv4_broadcast(lip, lprefix);
511 	vars.ipv4_rbroadcast = get_ipv4_broadcast(rip, rprefix);
512 
513 	vars.ipv4_lnetmask = get_ipv4_netmask(lprefix);
514 	vars.ipv4_rnetmask = get_ipv4_netmask(rprefix);
515 
516 	vars.ipv4_lnetwork = get_ipv4_network(lip.s_addr, lprefix_round);
517 	vars.ipv4_rnetwork = get_ipv4_network(rip.s_addr, rprefix_round);
518 
519 	vars.lhost_ipv4_host = get_host_from_ip(lip_str, vars.ipv4_lnetwork);
520 	vars.rhost_ipv4_host = get_host_from_ip(rip_str, vars.ipv4_rnetwork);
521 
522 	vars.ipv4_net16_unused = get_ipv4_net16_unused(&lip, lprefix_round);
523 }
524 
get_ipv6_info(const char * lip_str,const char * rip_str,int lprefix,int rprefix)525 static void get_ipv6_info(const char *lip_str, const char *rip_str,
526 	int lprefix, int rprefix)
527 {
528 	struct in6_addr lip, rip;
529 	int lprefix_round, rprefix_round;
530 
531 	lprefix_round = round_down_prefix(lprefix, 1);
532 	rprefix_round = round_down_prefix(rprefix, 1);
533 
534 	tst_get_in6_addr(lip_str, &lip);
535 	tst_get_in6_addr(rip_str, &rip);
536 
537 	vars.ipv6_lnetmask = get_ipv6_netmask(lprefix);
538 	vars.ipv6_rnetmask = get_ipv6_netmask(rprefix);
539 
540 	vars.ipv6_lnetwork = get_ipv6_network(lip.s6_addr, lprefix_round);
541 	vars.ipv6_rnetwork = get_ipv6_network(rip.s6_addr, rprefix_round);
542 
543 	vars.lhost_ipv6_host = get_host_from_ip(lip_str, vars.ipv6_lnetwork);
544 	vars.rhost_ipv6_host = get_host_from_ip(rip_str, vars.ipv6_rnetwork);
545 
546 	vars.ipv6_net32_unused = get_ipv6_net32_unused(&lip, lprefix_round);
547 }
548 
print_vars(int is_ipv6)549 static void print_vars(int is_ipv6)
550 {
551 	if (is_ipv6) {
552 		tst_print_svar("IPV6_LNETMASK", vars.ipv6_lnetmask);
553 		tst_print_svar_change("IPV6_RNETMASK", vars.ipv6_rnetmask);
554 		tst_print_svar("IPV6_LNETWORK", vars.ipv6_lnetwork);
555 		tst_print_svar("IPV6_RNETWORK", vars.ipv6_rnetwork);
556 		tst_print_svar("LHOST_IPV6_HOST", vars.lhost_ipv6_host);
557 		tst_print_svar("RHOST_IPV6_HOST", vars.rhost_ipv6_host);
558 		tst_print_svar("IPV6_NET32_UNUSED", vars.ipv6_net32_unused);
559 	} else {
560 		tst_print_svar("IPV4_LBROADCAST", vars.ipv4_lbroadcast);
561 		tst_print_svar_change("IPV4_RBROADCAST", vars.ipv4_rbroadcast);
562 		tst_print_svar("IPV4_LNETMASK", vars.ipv4_lnetmask);
563 		tst_print_svar_change("IPV4_RNETMASK", vars.ipv4_rnetmask);
564 		tst_print_svar("IPV4_LNETWORK", vars.ipv4_lnetwork);
565 		tst_print_svar("IPV4_RNETWORK", vars.ipv4_rnetwork);
566 		tst_print_svar("LHOST_IPV4_HOST", vars.lhost_ipv4_host);
567 		tst_print_svar("RHOST_IPV4_HOST", vars.rhost_ipv4_host);
568 		tst_print_svar("IPV4_NET16_UNUSED", vars.ipv4_net16_unused);
569 	}
570 }
571 
main(int argc,char * argv[])572 int main(int argc, char *argv[])
573 {
574 	char *lip_str = NULL, *rip_str = NULL;
575 	int is_ipv6, lprefix, rprefix, tmp;
576 	struct in_addr ip;
577 	struct in6_addr ip6;
578 
579 	int is_usage = argc > 1 && (!strcmp(argv[1], "-h") ||
580 		!strcmp(argv[1], "--help"));
581 	if (argc < 3 || is_usage) {
582 		usage(argv[0]);
583 		exit(is_usage ? EXIT_SUCCESS : EXIT_FAILURE);
584 	}
585 
586 	lip_str = argv[1];
587 	rip_str = argv[2];
588 
589 	is_ipv6 = !!strchr(lip_str, ':');
590 	lprefix = tst_get_prefix(lip_str, is_ipv6);
591 	rprefix = tst_get_prefix(rip_str, is_ipv6);
592 
593 	if (is_ipv6)
594 		tst_get_in6_addr(lip_str, &ip6);
595 	else
596 		tst_get_in_addr(lip_str, &ip);
597 
598 	tmp = !!strchr(rip_str, ':');
599 	if (tmp)
600 		tst_get_in6_addr(rip_str, &ip6);
601 	else
602 		tst_get_in_addr(rip_str, &ip);
603 
604 	if (is_ipv6 != tmp)
605 		tst_brk_comment("mixed IPv4 and IPv6 addresses ('%s', '%s')",
606 				lip_str, rip_str);
607 
608 	check_prefix_range(lprefix, is_ipv6, 1);
609 	check_prefix_range(rprefix, is_ipv6, 0);
610 
611 	if (!strcmp(lip_str, rip_str))
612 		tst_brk_comment("IP addresses cannot be the same ('%s', '%s')",
613 				lip_str, rip_str);
614 
615 	if (is_ipv6)
616 		get_ipv6_info(lip_str, rip_str, lprefix, rprefix);
617 	else
618 		get_ipv4_info(lip_str, rip_str, lprefix, rprefix);
619 
620 	print_vars(is_ipv6);
621 
622 	exit(EXIT_SUCCESS);
623 }
624