aboutsummaryrefslogtreecommitdiff
blob: ac1b2dd5b47979ce4373abfe5858e968c9f36466 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# Copyright (c) 2011 by Gentoo Foundation
# Released under the 2-clause BSD license.
# shellcheck shell=sh disable=SC1008

_is_qmi() {
	[ -d "/sys/class/net/${IFACE}/qmi" ]
}

_get_state() {
	echo "/run/net.${IFACE}.qmi.state"
}

_get_device() {
	echo "/dev/cdc-$(echo "${IFACE}" | sed 's/wwan/wdm/')"
}

qmi_depend()
{
	program qmicli
	program ip
	before interface
}

qmi_pre_start() {

	_is_qmi || return 0

	local device
	local apn
	local auth
	local username
	local password
	local out
	local rc

	eval device=\$qmi_cdc_${IFVAR}
	eval apn=\$qmi_apn_${IFVAR}
	eval auth=\$qmi_auth_${IFVAR}
	eval username=\$qmi_username_${IFVAR}
	eval password=\$qmi_password_${IFVAR}

	[ -n "${apn}" ] || return 0

	[ -n "${device}" ] || device="$(_get_device)"
	[ -n "${auth}" ] || auth="none"
	[ -n "${username}" ] || username="dummy"
	[ -n "${password}" ] || password="dummy"

	if ! [ -c "${device}" ]; then
		ewarn "Cannot open device ${device} for ${IFACE}, aborting configuration"
		return 1
	fi

	if ! cat "/sys/class/net/${IFACE}/qmi/raw_ip" | grep -q Y; then
		ebegin "Configuring QMI raw IP"

		ip link set "${IFACE}" down
		if ! echo Y > "/sys/class/net/${IFACE}/qmi/raw_ip"; then
			eend 1 "Cannot set raw IP mode for ${IFACE}, aborting configuration"
			return 1
		else
			eend 0
		fi
	fi

	local wwan_connection="apn='${apn}',auth='${auth}',username='${username}',password='${password}',autoconnect=yes,ip-type=4"
	local n
	for n in 1 2 3; do
		ebegin "Connecting QMI APN '${apn}' using '${username}'"

		if out="$( \
			qmicli \
				--device="${device}" \
				--wds-start-network="${wwan_connection}" \
				--device-open-proxy \
				--client-no-release-cid \
		)"; then
			eend 0
			break
		elif echo "${out}" | grep -qi "timed out"; then
			eend 1 "QMI start network timeout"
		else
			eend 1 "QMI start network failed for ${IFACE}, aborting"
			return 1
		fi
	done

	local handle="$(echo "${out}" | grep "Packet data handle:" | sed "s/.*'\(.*\)'.*/\1/")"
	local cid="$(echo "${out}" | grep "CID:" | sed "s/.*'\(.*\)'.*/\1/")"

	if [ -z "${handle}" ]; then
		ewarn 1 "No QMI connection handle ${IFACE}, aborting configuration"
		return 1
	fi

	if [ -z "${cid}" ]; then
		ewarn "No QMI connection id ${IFACE}, aborting configuration"
		return 1
	fi

	cat > "$(_get_state)" << __EOF__
device="${device}"
handle="${handle}"
cid="${cid}"
__EOF__
}

qmi_post_stop() {

	_is_qmi || return 0

	local state="$(_get_state)"

	[ -f "${state}" ] || return 0

	ebegin "Disconnecting QMI ${IFACE}"

	local device
	local handle
	local cid

	. "${state}"

	qmicli \
		--device="${device}" \
		--client-cid="${cid}" \
		--wds-stop-network="${handle}"
	rc="$?"

	rm -f "${state}"

	eend "${rc}"
}