1f08c3bdfSopenharmony_ci#!/bin/sh
2f08c3bdfSopenharmony_ci# SPDX-License-Identifier: GPL-2.0-or-later
3f08c3bdfSopenharmony_ci# Copyright (c) 2017-2022 Petr Vorel <pvorel@suse.cz>
4f08c3bdfSopenharmony_ci# Copyright (c) 2015 Oracle and/or its affiliates. All Rights Reserved.
5f08c3bdfSopenharmony_ci# Copyright (c) International Business Machines  Corp., 2005
6f08c3bdfSopenharmony_ci# Author: Mitsuru Chinen <mitch@jp.ibm.com>
7f08c3bdfSopenharmony_ci
8f08c3bdfSopenharmony_ciIF_CMD='ifconfig'
9f08c3bdfSopenharmony_ci
10f08c3bdfSopenharmony_citest_body()
11f08c3bdfSopenharmony_ci{
12f08c3bdfSopenharmony_ci	local cmd="$CMD"
13f08c3bdfSopenharmony_ci	local num=$(($(od -A n -t u1 -N 1 /dev/random) * 253 / 255 + 2 ))
14f08c3bdfSopenharmony_ci	local iface=$(tst_iface)
15f08c3bdfSopenharmony_ci	if [ "$TST_IPV6" ]; then
16f08c3bdfSopenharmony_ci		local new_ip=${IPV6_NET32_UNUSED}::$num
17f08c3bdfSopenharmony_ci		local netmask=64
18f08c3bdfSopenharmony_ci	else
19f08c3bdfSopenharmony_ci		local new_ip=${IPV4_NET16_UNUSED}.1.$num
20f08c3bdfSopenharmony_ci		local netmask=24
21f08c3bdfSopenharmony_ci	fi
22f08c3bdfSopenharmony_ci
23f08c3bdfSopenharmony_ci	tst_res TINFO "'$cmd' add/del IPv$TST_IPVER '$new_ip' $NS_TIMES times"
24f08c3bdfSopenharmony_ci
25f08c3bdfSopenharmony_ci	if ! restore_ipaddr; then
26f08c3bdfSopenharmony_ci		tst_res TBROK "Failed to set default IP addresses"
27f08c3bdfSopenharmony_ci		return
28f08c3bdfSopenharmony_ci	fi
29f08c3bdfSopenharmony_ci
30f08c3bdfSopenharmony_ci	local cnt=1
31f08c3bdfSopenharmony_ci	while [ $cnt -le $NS_TIMES ]; do
32f08c3bdfSopenharmony_ci		make_background_tcp_traffic
33f08c3bdfSopenharmony_ci
34f08c3bdfSopenharmony_ci		case $cmd in
35f08c3bdfSopenharmony_ci		ifconfig)
36f08c3bdfSopenharmony_ci			if [ "$TST_IPV6" ]; then
37f08c3bdfSopenharmony_ci				ifconfig $iface add $new_ip/$netmask
38f08c3bdfSopenharmony_ci			else
39f08c3bdfSopenharmony_ci				ifconfig $iface:1 $new_ip netmask 255.255.255.0
40f08c3bdfSopenharmony_ci			fi
41f08c3bdfSopenharmony_ci		;;
42f08c3bdfSopenharmony_ci		ip) ip addr add $new_ip/$netmask dev $iface ;;
43f08c3bdfSopenharmony_ci		esac
44f08c3bdfSopenharmony_ci
45f08c3bdfSopenharmony_ci		if [ $? -ne 0 ]; then
46f08c3bdfSopenharmony_ci			tst_res TFAIL "command failed to add $new_ip to $iface"
47f08c3bdfSopenharmony_ci			return
48f08c3bdfSopenharmony_ci		fi
49f08c3bdfSopenharmony_ci
50f08c3bdfSopenharmony_ci		ip addr show $iface | grep -q $new_ip
51f08c3bdfSopenharmony_ci		if [ $? -ne 0 ]; then
52f08c3bdfSopenharmony_ci			ip addr show $iface
53f08c3bdfSopenharmony_ci			tst_res TFAIL "$new_ip not configured"
54f08c3bdfSopenharmony_ci			return
55f08c3bdfSopenharmony_ci		fi
56f08c3bdfSopenharmony_ci
57f08c3bdfSopenharmony_ci		check_connectivity_interval $cnt || return
58f08c3bdfSopenharmony_ci
59f08c3bdfSopenharmony_ci		cnt=$(($cnt + 1))
60f08c3bdfSopenharmony_ci
61f08c3bdfSopenharmony_ci		case $cmd in
62f08c3bdfSopenharmony_ci		ifconfig)
63f08c3bdfSopenharmony_ci			if [ "$TST_IPV6" ]; then
64f08c3bdfSopenharmony_ci				ifconfig $iface del $new_ip/$netmask
65f08c3bdfSopenharmony_ci			else
66f08c3bdfSopenharmony_ci				ifconfig $iface:1 down
67f08c3bdfSopenharmony_ci			fi
68f08c3bdfSopenharmony_ci		;;
69f08c3bdfSopenharmony_ci		ip) ip addr del $new_ip/$netmask dev $iface ;;
70f08c3bdfSopenharmony_ci		esac
71f08c3bdfSopenharmony_ci
72f08c3bdfSopenharmony_ci		if [ $? -ne 0 ]; then
73f08c3bdfSopenharmony_ci			tst_res TFAIL " delete command failed".
74f08c3bdfSopenharmony_ci			return
75f08c3bdfSopenharmony_ci		fi
76f08c3bdfSopenharmony_ci
77f08c3bdfSopenharmony_ci		ip addr show $iface | grep -q $new_ip
78f08c3bdfSopenharmony_ci		if [ $? -eq 0 ]; then
79f08c3bdfSopenharmony_ci			ip addr show $iface
80f08c3bdfSopenharmony_ci			tst_res TFAIL "Failed to remove '$new_ip' address"
81f08c3bdfSopenharmony_ci			return
82f08c3bdfSopenharmony_ci		fi
83f08c3bdfSopenharmony_ci	done
84f08c3bdfSopenharmony_ci
85f08c3bdfSopenharmony_ci	tst_res TPASS "Test is finished correctly"
86f08c3bdfSopenharmony_ci}
87f08c3bdfSopenharmony_ci
88f08c3bdfSopenharmony_ci. if-lib.sh
89f08c3bdfSopenharmony_ci
90f08c3bdfSopenharmony_ci# The interval of the check interface activity
91f08c3bdfSopenharmony_ciCHECK_INTERVAL=${CHECK_INTERVAL:-$(($NS_TIMES / 20))}
92f08c3bdfSopenharmony_ci
93f08c3bdfSopenharmony_citst_run
94