Custom netifd proto script

Good day!

I'm trying to add a new mobile connection protocol for netifd.
The system contains several proprietary binaries, to the source codes of which I don't have access.
When system booting, rmnet0 is up, rmnet_data0 is down.
I run some program with the necessary apn parameters and authentication to start mobile connection. After that, the interface rmnet_data0 becomes up and remains only to assign the default gateway.

I slightly modified the qmi.sh script for this connection:

#!/bin/sh

[ -n "$INCLUDE_ONLY" ] || {
	. /lib/functions.sh
	. ../netifd-proto.sh
	init_proto "$@"
}

proto_rmnet_init_config() {
	available=1
	no_device=0
        proto_config_add_string "device:device"
	proto_config_add_string apn
	proto_config_add_string auth
	proto_config_add_string username
	proto_config_add_string password
	proto_config_add_string pincode
	proto_config_add_int delay
	proto_config_add_string modes
	proto_config_add_string pdptype
	proto_config_add_int profile
	proto_config_add_boolean dhcpv6
	proto_config_add_boolean autoconnect
	proto_config_add_int plmn
	proto_config_add_defaults
}

proto_rmnet_setup() {
	local interface="$1"
	local dataformat connstat
	local device apn auth username password pincode delay modes pdptype profile dhcpv6 autoconnect plmn $PROTO_DEFAULT_OPTIONS
	local cid_4 pdh_4 cid_6 pdh_6
	local ip_6 ip_prefix_length gateway_6 dns1_6 dns2_6
	json_get_vars device apn auth username password pincode delay modes pdptype profile dhcpv6 autoconnect plmn $PROTO_DEFAULT_OPTIONS

	[ "$metric" = "" ] && metric="0"

	[ -n "$delay" ] && sleep "$delay"

	#check status SIM here
	while datacall_cli --get-pin-status | grep '"UIM uninitialized"' > /dev/null ; do
		sleep 1;
	done

	#apply pin code
	[ -n "$pincode" ] && {
		datacall_cli --verify-pin "$pincode" > /dev/null || {
			echo "Unable to verify PIN"
			proto_notify_error "$interface" PIN_FAILED
			proto_block_restart "$interface"
			return 1
		}
	}

	echo "Setting up $ifname"
	proto_init_update "$ifname" 1
	proto_set_keep 1
	proto_add_data

	proto_close_data
	proto_send_update "$interface"

	json_load "$(datacall ${apn:+--apn $apn} ${auth:+--auth-type $auth} ${username:+--user $username} ${password:+--psw $password})"
	if [ -z "$dhcpv6" -o "$dhcpv6" = 0 ]; then			
		json_select ipv6
		json_get_var ip_6 ip
		json_get_var gateway_6 gateway
		json_get_var dns1_6 dns1
		json_get_var dns2_6 dns2
		json_get_var ip_prefix_length ip-prefix-length

		proto_init_update "$ifname" 1
		proto_set_keep 1
		proto_add_ipv6_address "$ip_6" "128"
		proto_add_ipv6_prefix "${ip_6}/${ip_prefix_length}"
		proto_add_ipv6_route "$gateway_6" "128"
		[ "$defaultroute" = 0 ] || proto_add_ipv6_route "::0" 0 "$gateway_6" "" "" "${ip_6}/${ip_prefix_length}"
		[ "$peerdns" = 0 ] || {
			proto_add_dns_server "$dns1_6"
			proto_add_dns_server "$dns2_6"
		}
		proto_send_update "$interface"
	else
		json_init
		json_add_string name "${interface}_6"
		json_add_string ifname "@$interface"
		json_add_string proto "dhcpv6"
		proto_add_dynamic_defaults
		# RFC 7278: Extend an IPv6 /64 Prefix to LAN
		json_add_string extendprefix 1
		json_close_object
		ubus call network add_dynamic "$(json_dump)"
	fi

	json_init
	json_add_string name "${interface}_4"
	json_add_string ifname "@$interface"
	json_add_string proto "dhcp"
	proto_add_dynamic_defaults
	json_close_object
	ubus call network add_dynamic "$(json_dump)"

}

proto_rmnet_teardown() {
	echo "Stopping network"
	pkill datacall
}


[ -z "NOT_INCLUDED" ] || add_protocol rmnet

Interface settings:

config interface 'internet'
        option proto 'rmnet'
        option ifname 'rmnet_data0'
        option apn 'internet'

With these settings, the connection starts and stops, displays information about the connection on the main screen.
But if the sim card is not inserted, the connection still up when I restart network, which is not entirely correct (looks the same as when giving the command ifconfig rmnet_data0 up):
:/# ps | grep data0
18186 root 0:00 {rmnet.sh} /bin/sh ./rmnet.sh rmnet setup internet {"proto":"rmnet","ifname":"rmnet_data0","apn":"internet"} rmnet_data0
How can I solve this problem?