uqmi: do not block proto handler if SIM is uninitialized
[openwrt/staging/mkresin.git] / package / network / utils / uqmi / files / lib / netifd / proto / qmi.sh
1 #!/bin/sh
2
3 [ -n "$INCLUDE_ONLY" ] || {
4 . /lib/functions.sh
5 . ../netifd-proto.sh
6 init_proto "$@"
7 }
8
9 proto_qmi_init_config() {
10 available=1
11 no_device=1
12 proto_config_add_string "device:device"
13 proto_config_add_string apn
14 proto_config_add_string auth
15 proto_config_add_string username
16 proto_config_add_string password
17 proto_config_add_string pincode
18 proto_config_add_int delay
19 proto_config_add_string modes
20 proto_config_add_string pdptype
21 proto_config_add_int profile
22 proto_config_add_boolean dhcpv6
23 proto_config_add_boolean autoconnect
24 proto_config_add_int plmn
25 proto_config_add_int timeout
26 proto_config_add_defaults
27 }
28
29 proto_qmi_setup() {
30 local interface="$1"
31 local dataformat connstat
32 local device apn auth username password pincode delay modes pdptype profile dhcpv6 autoconnect plmn timeout $PROTO_DEFAULT_OPTIONS
33 local ip4table ip6table
34 local cid_4 pdh_4 cid_6 pdh_6
35 local ip_6 ip_prefix_length gateway_6 dns1_6 dns2_6
36 json_get_vars device apn auth username password pincode delay modes pdptype profile dhcpv6 autoconnect plmn ip4table ip6table timeout $PROTO_DEFAULT_OPTIONS
37
38 [ "$timeout" = "" ] && timeout="10"
39
40 [ "$metric" = "" ] && metric="0"
41
42 [ -n "$ctl_device" ] && device=$ctl_device
43
44 [ -n "$device" ] || {
45 echo "No control device specified"
46 proto_notify_error "$interface" NO_DEVICE
47 proto_set_available "$interface" 0
48 return 1
49 }
50
51 [ -n "$delay" ] && sleep "$delay"
52
53 device="$(readlink -f $device)"
54 [ -c "$device" ] || {
55 echo "The specified control device does not exist"
56 proto_notify_error "$interface" NO_DEVICE
57 proto_set_available "$interface" 0
58 return 1
59 }
60
61 devname="$(basename "$device")"
62 devpath="$(readlink -f /sys/class/usbmisc/$devname/device/)"
63 ifname="$( ls "$devpath"/net )"
64 [ -n "$ifname" ] || {
65 echo "The interface could not be found."
66 proto_notify_error "$interface" NO_IFACE
67 proto_set_available "$interface" 0
68 return 1
69 }
70
71 while uqmi -s -d "$device" --get-pin-status | grep '"UIM uninitialized"' > /dev/null; do
72 [ -e "$device" ] || return 1
73 if [ "$uninitialized_timeout" -lt "$timeout" ]; then
74 let uninitialized_timeout++
75 sleep 1;
76 else
77 echo "SIM not initialized"
78 proto_notify_error "$interface" SIM_NOT_INITIALIZED
79 proto_block_restart "$interface"
80 return 1
81 fi
82 done
83
84 [ -n "$pincode" ] && {
85 uqmi -s -d "$device" --verify-pin1 "$pincode" > /dev/null || uqmi -s -d "$device" --uim-verify-pin1 "$pincode" > /dev/null || {
86 echo "Unable to verify PIN"
87 proto_notify_error "$interface" PIN_FAILED
88 proto_block_restart "$interface"
89 return 1
90 }
91 }
92
93 [ -n "$plmn" ] && {
94 local mcc mnc
95 if [ "$plmn" = 0 ]; then
96 mcc=0
97 mnc=0
98 echo "Setting PLMN to auto"
99 else
100 mcc=${plmn:0:3}
101 mnc=${plmn:3}
102 echo "Setting PLMN to $plmn"
103 fi
104 uqmi -s -d "$device" --set-plmn --mcc "$mcc" --mnc "$mnc" > /dev/null 2>&1 || {
105 echo "Unable to set PLMN"
106 proto_notify_error "$interface" PLMN_FAILED
107 proto_block_restart "$interface"
108 return 1
109 }
110 }
111
112 # Cleanup current state if any
113 uqmi -s -d "$device" --stop-network 0xffffffff --autoconnect > /dev/null 2>&1
114
115 # Set IP format
116 uqmi -s -d "$device" --set-data-format 802.3 > /dev/null 2>&1
117 uqmi -s -d "$device" --wda-set-data-format 802.3 > /dev/null 2>&1
118 dataformat="$(uqmi -s -d "$device" --wda-get-data-format)"
119
120 if [ "$dataformat" = '"raw-ip"' ]; then
121
122 [ -f /sys/class/net/$ifname/qmi/raw_ip ] || {
123 echo "Device only supports raw-ip mode but is missing this required driver attribute: /sys/class/net/$ifname/qmi/raw_ip"
124 return 1
125 }
126
127 echo "Device does not support 802.3 mode. Informing driver of raw-ip only for $ifname .."
128 echo "Y" > /sys/class/net/$ifname/qmi/raw_ip
129 fi
130
131 uqmi -s -d "$device" --sync > /dev/null 2>&1
132
133 echo "Waiting for network registration"
134 local registration_timeout=0
135 while uqmi -s -d "$device" --get-serving-system | grep '"searching"' > /dev/null; do
136 [ -e "$device" ] || return 1
137 if [ "$registration_timeout" -lt "$timeout" ]; then
138 let registration_timeout++
139 sleep 1;
140 else
141 echo "Network registration failed"
142 proto_notify_error "$interface" NETWORK_REGISTRATION_FAILED
143 proto_block_restart "$interface"
144 return 1
145 fi
146 done
147
148 [ -n "$modes" ] && uqmi -s -d "$device" --set-network-modes "$modes" > /dev/null 2>&1
149
150 echo "Starting network $interface"
151
152 pdptype=$(echo "$pdptype" | awk '{print tolower($0)}')
153 [ "$pdptype" = "ip" -o "$pdptype" = "ipv6" -o "$pdptype" = "ipv4v6" ] || pdptype="ip"
154
155 if [ "$pdptype" = "ip" ]; then
156 [ -z "$autoconnect" ] && autoconnect=1
157 [ "$autoconnect" = 0 ] && autoconnect=""
158 else
159 [ "$autoconnect" = 1 ] || autoconnect=""
160 fi
161
162 [ "$pdptype" = "ip" -o "$pdptype" = "ipv4v6" ] && {
163 cid_4=$(uqmi -s -d "$device" --get-client-id wds)
164 if ! [ "$cid_4" -eq "$cid_4" ] 2> /dev/null; then
165 echo "Unable to obtain client ID"
166 proto_notify_error "$interface" NO_CID
167 return 1
168 fi
169
170 uqmi -s -d "$device" --set-client-id wds,"$cid_4" --set-ip-family ipv4 > /dev/null 2>&1
171
172 pdh_4=$(uqmi -s -d "$device" --set-client-id wds,"$cid_4" \
173 --start-network \
174 ${apn:+--apn $apn} \
175 ${profile:+--profile $profile} \
176 ${auth:+--auth-type $auth} \
177 ${username:+--username $username} \
178 ${password:+--password $password} \
179 ${autoconnect:+--autoconnect})
180
181 # pdh_4 is a numeric value on success
182 if ! [ "$pdh_4" -eq "$pdh_4" ] 2> /dev/null; then
183 echo "Unable to connect IPv4"
184 uqmi -s -d "$device" --set-client-id wds,"$cid_4" --release-client-id wds > /dev/null 2>&1
185 proto_notify_error "$interface" CALL_FAILED
186 return 1
187 fi
188
189 # Check data connection state
190 connstat=$(uqmi -s -d "$device" --get-data-status)
191 [ "$connstat" == '"connected"' ] || {
192 echo "No data link!"
193 uqmi -s -d "$device" --set-client-id wds,"$cid_4" --release-client-id wds > /dev/null 2>&1
194 proto_notify_error "$interface" CALL_FAILED
195 return 1
196 }
197 }
198
199 [ "$pdptype" = "ipv6" -o "$pdptype" = "ipv4v6" ] && {
200 cid_6=$(uqmi -s -d "$device" --get-client-id wds)
201 if ! [ "$cid_6" -eq "$cid_6" ] 2> /dev/null; then
202 echo "Unable to obtain client ID"
203 proto_notify_error "$interface" NO_CID
204 return 1
205 fi
206
207 uqmi -s -d "$device" --set-client-id wds,"$cid_6" --set-ip-family ipv6 > /dev/null 2>&1
208
209 pdh_6=$(uqmi -s -d "$device" --set-client-id wds,"$cid_6" \
210 --start-network \
211 ${apn:+--apn $apn} \
212 ${profile:+--profile $profile} \
213 ${auth:+--auth-type $auth} \
214 ${username:+--username $username} \
215 ${password:+--password $password} \
216 ${autoconnect:+--autoconnect})
217
218 # pdh_6 is a numeric value on success
219 if ! [ "$pdh_6" -eq "$pdh_6" ] 2> /dev/null; then
220 echo "Unable to connect IPv6"
221 uqmi -s -d "$device" --set-client-id wds,"$cid_6" --release-client-id wds > /dev/null 2>&1
222 proto_notify_error "$interface" CALL_FAILED
223 return 1
224 fi
225
226 # Check data connection state
227 connstat=$(uqmi -s -d "$device" --get-data-status)
228 [ "$connstat" == '"connected"' ] || {
229 echo "No data link!"
230 uqmi -s -d "$device" --set-client-id wds,"$cid_6" --release-client-id wds > /dev/null 2>&1
231 proto_notify_error "$interface" CALL_FAILED
232 return 1
233 }
234 }
235
236 echo "Setting up $ifname"
237 proto_init_update "$ifname" 1
238 proto_set_keep 1
239 proto_add_data
240 [ -n "$pdh_4" ] && {
241 json_add_string "cid_4" "$cid_4"
242 json_add_string "pdh_4" "$pdh_4"
243 }
244 [ -n "$pdh_6" ] && {
245 json_add_string "cid_6" "$cid_6"
246 json_add_string "pdh_6" "$pdh_6"
247 }
248 proto_close_data
249 proto_send_update "$interface"
250 [ -n "$pdh_6" ] && {
251 if [ -z "$dhcpv6" -o "$dhcpv6" = 0 ]; then
252 json_load "$(uqmi -s -d $device --set-client-id wds,$cid_6 --get-current-settings)"
253 json_select ipv6
254 json_get_var ip_6 ip
255 json_get_var gateway_6 gateway
256 json_get_var dns1_6 dns1
257 json_get_var dns2_6 dns2
258 json_get_var ip_prefix_length ip-prefix-length
259
260 proto_init_update "$ifname" 1
261 proto_set_keep 1
262 proto_add_ipv6_address "$ip_6" "128"
263 proto_add_ipv6_prefix "${ip_6}/${ip_prefix_length}"
264 proto_add_ipv6_route "$gateway_6" "128"
265 [ "$defaultroute" = 0 ] || proto_add_ipv6_route "::0" 0 "$gateway_6" "" "" "${ip_6}/${ip_prefix_length}"
266 [ "$peerdns" = 0 ] || {
267 proto_add_dns_server "$dns1_6"
268 proto_add_dns_server "$dns2_6"
269 }
270 proto_send_update "$interface"
271 else
272 json_init
273 json_add_string name "${interface}_6"
274 json_add_string ifname "@$interface"
275 json_add_string proto "dhcpv6"
276 [ -n "$ip6table" ] && json_add_string ip6table "$ip6table"
277 proto_add_dynamic_defaults
278 # RFC 7278: Extend an IPv6 /64 Prefix to LAN
279 json_add_string extendprefix 1
280 json_close_object
281 ubus call network add_dynamic "$(json_dump)"
282 fi
283 }
284
285 [ -n "$pdh_4" ] && {
286 json_init
287 json_add_string name "${interface}_4"
288 json_add_string ifname "@$interface"
289 json_add_string proto "dhcp"
290 [ -n "$ip4table" ] && json_add_string ip4table "$ip4table"
291 proto_add_dynamic_defaults
292 json_close_object
293 ubus call network add_dynamic "$(json_dump)"
294 }
295 }
296
297 qmi_wds_stop() {
298 local cid="$1"
299 local pdh="$2"
300
301 [ -n "$cid" ] || return
302
303 uqmi -s -d "$device" --set-client-id wds,"$cid" \
304 --stop-network 0xffffffff \
305 --autoconnect > /dev/null 2>&1
306
307 [ -n "$pdh" ] && {
308 uqmi -s -d "$device" --set-client-id wds,"$cid" \
309 --stop-network "$pdh" > /dev/null 2>&1
310 }
311
312 uqmi -s -d "$device" --set-client-id wds,"$cid" \
313 --release-client-id wds > /dev/null 2>&1
314 }
315
316 proto_qmi_teardown() {
317 local interface="$1"
318
319 local device cid_4 pdh_4 cid_6 pdh_6
320 json_get_vars device
321
322 [ -n "$ctl_device" ] && device=$ctl_device
323
324 echo "Stopping network $interface"
325
326 json_load "$(ubus call network.interface.$interface status)"
327 json_select data
328 json_get_vars cid_4 pdh_4 cid_6 pdh_6
329
330 qmi_wds_stop "$cid_4" "$pdh_4"
331 qmi_wds_stop "$cid_6" "$pdh_6"
332
333 proto_init_update "*" 0
334 proto_send_update "$interface"
335 }
336
337 [ -n "$INCLUDE_ONLY" ] || {
338 add_protocol qmi
339 }