uqmi: fix network registration loop
[openwrt/staging/zorun.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 dhcp
23 proto_config_add_boolean dhcpv6
24 proto_config_add_boolean autoconnect
25 proto_config_add_int plmn
26 proto_config_add_int timeout
27 proto_config_add_int mtu
28 proto_config_add_defaults
29 }
30
31 proto_qmi_setup() {
32 local interface="$1"
33 local dataformat connstat plmn_mode mcc mnc
34 local device apn auth username password pincode delay modes pdptype
35 local profile dhcp dhcpv6 autoconnect plmn timeout mtu $PROTO_DEFAULT_OPTIONS
36 local ip4table ip6table
37 local cid_4 pdh_4 cid_6 pdh_6
38 local ip_6 ip_prefix_length gateway_6 dns1_6 dns2_6
39
40 json_get_vars device apn auth username password pincode delay modes
41 json_get_vars pdptype profile dhcp dhcpv6 autoconnect plmn ip4table
42 json_get_vars ip6table timeout mtu $PROTO_DEFAULT_OPTIONS
43
44 [ "$timeout" = "" ] && timeout="10"
45
46 [ "$metric" = "" ] && metric="0"
47
48 [ -n "$ctl_device" ] && device=$ctl_device
49
50 [ -n "$device" ] || {
51 echo "No control device specified"
52 proto_notify_error "$interface" NO_DEVICE
53 proto_set_available "$interface" 0
54 return 1
55 }
56
57 [ -n "$delay" ] && sleep "$delay"
58
59 device="$(readlink -f $device)"
60 [ -c "$device" ] || {
61 echo "The specified control device does not exist"
62 proto_notify_error "$interface" NO_DEVICE
63 proto_set_available "$interface" 0
64 return 1
65 }
66
67 devname="$(basename "$device")"
68 devpath="$(readlink -f /sys/class/usbmisc/$devname/device/)"
69 ifname="$( ls "$devpath"/net )"
70 [ -n "$ifname" ] || {
71 echo "The interface could not be found."
72 proto_notify_error "$interface" NO_IFACE
73 proto_set_available "$interface" 0
74 return 1
75 }
76
77 [ -n "$mtu" ] && {
78 echo "Setting MTU to $mtu"
79 /sbin/ip link set dev $ifname mtu $mtu
80 }
81
82 echo "Waiting for SIM initialization"
83 local uninitialized_timeout=0
84 while uqmi -s -d "$device" --get-pin-status | grep '"UIM uninitialized"' > /dev/null; do
85 [ -e "$device" ] || return 1
86 if [ "$uninitialized_timeout" -lt "$timeout" -o "$timeout" = "0" ]; then
87 let uninitialized_timeout++
88 sleep 1;
89 else
90 echo "SIM not initialized"
91 proto_notify_error "$interface" SIM_NOT_INITIALIZED
92 proto_block_restart "$interface"
93 return 1
94 fi
95 done
96
97 if uqmi -s -d "$device" --get-pin-status | grep '"Not supported"\|"Invalid QMI command"' > /dev/null; then
98 [ -n "$pincode" ] && {
99 uqmi -s -d "$device" --verify-pin1 "$pincode" > /dev/null || uqmi -s -d "$device" --uim-verify-pin1 "$pincode" > /dev/null || {
100 echo "Unable to verify PIN"
101 proto_notify_error "$interface" PIN_FAILED
102 proto_block_restart "$interface"
103 return 1
104 }
105 }
106 else
107 . /usr/share/libubox/jshn.sh
108 json_load "$(uqmi -s -d "$device" --get-pin-status)"
109 json_get_var pin1_status pin1_status
110 json_get_var pin1_verify_tries pin1_verify_tries
111
112 case "$pin1_status" in
113 disabled)
114 echo "PIN verification is disabled"
115 ;;
116 blocked)
117 echo "SIM locked PUK required"
118 proto_notify_error "$interface" PUK_NEEDED
119 proto_block_restart "$interface"
120 return 1
121 ;;
122 not_verified)
123 [ "$pin1_verify_tries" -lt "3" ] && {
124 echo "PIN verify count value is $pin1_verify_tries this is below the limit of 3"
125 proto_notify_error "$interface" PIN_TRIES_BELOW_LIMIT
126 proto_block_restart "$interface"
127 return 1
128 }
129 if [ -n "$pincode" ]; then
130 uqmi -s -d "$device" --verify-pin1 "$pincode" > /dev/null 2>&1 || uqmi -s -d "$device" --uim-verify-pin1 "$pincode" > /dev/null 2>&1 || {
131 echo "Unable to verify PIN"
132 proto_notify_error "$interface" PIN_FAILED
133 proto_block_restart "$interface"
134 return 1
135 }
136 else
137 echo "PIN not specified but required"
138 proto_notify_error "$interface" PIN_NOT_SPECIFIED
139 proto_block_restart "$interface"
140 return 1
141 fi
142 ;;
143 verified)
144 echo "PIN already verified"
145 ;;
146 *)
147 echo "PIN status failed ($pin1_status)"
148 proto_notify_error "$interface" PIN_STATUS_FAILED
149 proto_block_restart "$interface"
150 return 1
151 ;;
152 esac
153 fi
154
155 if [ -n "$plmn" ]; then
156 json_load "$(uqmi -s -d "$device" --get-plmn)"
157 json_get_var plmn_mode mode
158 json_get_vars mcc mnc || {
159 mcc=0
160 mnc=0
161 }
162
163 if [ "$plmn" = "0" ]; then
164 if [ "$plmn_mode" != "automatic" ]; then
165 mcc=0
166 mnc=0
167 echo "Setting PLMN to auto"
168 fi
169 elif [ "$mcc" -ne "${plmn:0:3}" -o "$mnc" -ne "${plmn:3}" ]; then
170 mcc=${plmn:0:3}
171 mnc=${plmn:3}
172 echo "Setting PLMN to $plmn"
173 else
174 mcc=""
175 mnc=""
176 fi
177 fi
178
179 if [ -n "$mcc" -a -n "$mnc" ]; then
180 uqmi -s -d "$device" --set-plmn --mcc "$mcc" --mnc "$mnc" > /dev/null 2>&1 || {
181 echo "Unable to set PLMN"
182 proto_notify_error "$interface" PLMN_FAILED
183 proto_block_restart "$interface"
184 return 1
185 }
186 fi
187
188 # Cleanup current state if any
189 uqmi -s -d "$device" --stop-network 0xffffffff --autoconnect > /dev/null 2>&1
190
191 # Go online
192 uqmi -s -d "$device" --set-device-operating-mode online > /dev/null 2>&1
193
194 # Set IP format
195 uqmi -s -d "$device" --set-data-format 802.3 > /dev/null 2>&1
196 uqmi -s -d "$device" --wda-set-data-format 802.3 > /dev/null 2>&1
197 dataformat="$(uqmi -s -d "$device" --wda-get-data-format)"
198
199 if [ "$dataformat" = '"raw-ip"' ]; then
200
201 [ -f /sys/class/net/$ifname/qmi/raw_ip ] || {
202 echo "Device only supports raw-ip mode but is missing this required driver attribute: /sys/class/net/$ifname/qmi/raw_ip"
203 return 1
204 }
205
206 echo "Device does not support 802.3 mode. Informing driver of raw-ip only for $ifname .."
207 echo "Y" > /sys/class/net/$ifname/qmi/raw_ip
208 fi
209
210 uqmi -s -d "$device" --sync > /dev/null 2>&1
211
212 uqmi -s -d "$device" --network-register > /dev/null 2>&1
213
214 echo "Waiting for network registration"
215 sleep 1
216 local registration_timeout=0
217 local registration_state=""
218 while true; do
219 registration_state=$(uqmi -s -d "$device" --get-serving-system 2>/dev/null | jsonfilter -e "@.registration" 2>/dev/null)
220
221 [ "$registration_state" = "registered" ] && break
222
223 if [ "$registration_state" = "searching" ] || [ "$registration_state" = "not_registered" ]; then
224 if [ "$registration_timeout" -lt "$timeout" ] || [ "$timeout" = "0" ]; then
225 [ "$registration_state" = "searching" ] || {
226 echo "Device stopped network registration. Restart network registration"
227 uqmi -s -d "$device" --network-register > /dev/null 2>&1
228 }
229 let registration_timeout++
230 sleep 1
231 continue
232 fi
233 echo "Network registration failed, registration timeout reached"
234 else
235 # registration_state is 'registration_denied' or 'unknown' or ''
236 echo "Network registration failed (reason: '$registration_state')"
237 fi
238
239 proto_notify_error "$interface" NETWORK_REGISTRATION_FAILED
240 proto_block_restart "$interface"
241 return 1
242 done
243
244 [ -n "$modes" ] && uqmi -s -d "$device" --set-network-modes "$modes" > /dev/null 2>&1
245
246 echo "Starting network $interface"
247
248 pdptype=$(echo "$pdptype" | awk '{print tolower($0)}')
249 [ "$pdptype" = "ip" -o "$pdptype" = "ipv6" -o "$pdptype" = "ipv4v6" ] || pdptype="ip"
250
251 if [ "$pdptype" = "ip" ]; then
252 [ -z "$autoconnect" ] && autoconnect=1
253 [ "$autoconnect" = 0 ] && autoconnect=""
254 else
255 [ "$autoconnect" = 1 ] || autoconnect=""
256 fi
257
258 [ "$pdptype" = "ip" -o "$pdptype" = "ipv4v6" ] && {
259 cid_4=$(uqmi -s -d "$device" --get-client-id wds)
260 if ! [ "$cid_4" -eq "$cid_4" ] 2> /dev/null; then
261 echo "Unable to obtain client ID"
262 proto_notify_error "$interface" NO_CID
263 return 1
264 fi
265
266 uqmi -s -d "$device" --set-client-id wds,"$cid_4" --set-ip-family ipv4 > /dev/null 2>&1
267
268 pdh_4=$(uqmi -s -d "$device" --set-client-id wds,"$cid_4" \
269 --start-network \
270 ${apn:+--apn $apn} \
271 ${profile:+--profile $profile} \
272 ${auth:+--auth-type $auth} \
273 ${username:+--username $username} \
274 ${password:+--password $password} \
275 ${autoconnect:+--autoconnect})
276
277 # pdh_4 is a numeric value on success
278 if ! [ "$pdh_4" -eq "$pdh_4" ] 2> /dev/null; then
279 echo "Unable to connect IPv4"
280 uqmi -s -d "$device" --set-client-id wds,"$cid_4" --release-client-id wds > /dev/null 2>&1
281 proto_notify_error "$interface" CALL_FAILED
282 return 1
283 fi
284
285 # Check data connection state
286 connstat=$(uqmi -s -d "$device" --get-data-status)
287 [ "$connstat" == '"connected"' ] || {
288 echo "No data link!"
289 uqmi -s -d "$device" --set-client-id wds,"$cid_4" --release-client-id wds > /dev/null 2>&1
290 proto_notify_error "$interface" CALL_FAILED
291 return 1
292 }
293 }
294
295 [ "$pdptype" = "ipv6" -o "$pdptype" = "ipv4v6" ] && {
296 cid_6=$(uqmi -s -d "$device" --get-client-id wds)
297 if ! [ "$cid_6" -eq "$cid_6" ] 2> /dev/null; then
298 echo "Unable to obtain client ID"
299 proto_notify_error "$interface" NO_CID
300 return 1
301 fi
302
303 uqmi -s -d "$device" --set-client-id wds,"$cid_6" --set-ip-family ipv6 > /dev/null 2>&1
304
305 pdh_6=$(uqmi -s -d "$device" --set-client-id wds,"$cid_6" \
306 --start-network \
307 ${apn:+--apn $apn} \
308 ${profile:+--profile $profile} \
309 ${auth:+--auth-type $auth} \
310 ${username:+--username $username} \
311 ${password:+--password $password} \
312 ${autoconnect:+--autoconnect})
313
314 # pdh_6 is a numeric value on success
315 if ! [ "$pdh_6" -eq "$pdh_6" ] 2> /dev/null; then
316 echo "Unable to connect IPv6"
317 uqmi -s -d "$device" --set-client-id wds,"$cid_6" --release-client-id wds > /dev/null 2>&1
318 proto_notify_error "$interface" CALL_FAILED
319 return 1
320 fi
321
322 # Check data connection state
323 connstat=$(uqmi -s -d "$device" --get-data-status)
324 [ "$connstat" == '"connected"' ] || {
325 echo "No data link!"
326 uqmi -s -d "$device" --set-client-id wds,"$cid_6" --release-client-id wds > /dev/null 2>&1
327 proto_notify_error "$interface" CALL_FAILED
328 return 1
329 }
330 }
331
332 echo "Setting up $ifname"
333 proto_init_update "$ifname" 1
334 proto_set_keep 1
335 proto_add_data
336 [ -n "$pdh_4" ] && {
337 json_add_string "cid_4" "$cid_4"
338 json_add_string "pdh_4" "$pdh_4"
339 }
340 [ -n "$pdh_6" ] && {
341 json_add_string "cid_6" "$cid_6"
342 json_add_string "pdh_6" "$pdh_6"
343 }
344 proto_close_data
345 proto_send_update "$interface"
346
347 local zone="$(fw3 -q network "$interface" 2>/dev/null)"
348
349 [ -n "$pdh_6" ] && {
350 if [ -z "$dhcpv6" -o "$dhcpv6" = 0 ]; then
351 json_load "$(uqmi -s -d $device --set-client-id wds,$cid_6 --get-current-settings)"
352 json_select ipv6
353 json_get_var ip_6 ip
354 json_get_var gateway_6 gateway
355 json_get_var dns1_6 dns1
356 json_get_var dns2_6 dns2
357 json_get_var ip_prefix_length ip-prefix-length
358
359 proto_init_update "$ifname" 1
360 proto_set_keep 1
361 proto_add_ipv6_address "$ip_6" "128"
362 proto_add_ipv6_prefix "${ip_6}/${ip_prefix_length}"
363 proto_add_ipv6_route "$gateway_6" "128"
364 [ "$defaultroute" = 0 ] || proto_add_ipv6_route "::0" 0 "$gateway_6" "" "" "${ip_6}/${ip_prefix_length}"
365 [ "$peerdns" = 0 ] || {
366 proto_add_dns_server "$dns1_6"
367 proto_add_dns_server "$dns2_6"
368 }
369 [ -n "$zone" ] && {
370 proto_add_data
371 json_add_string zone "$zone"
372 proto_close_data
373 }
374 proto_send_update "$interface"
375 else
376 json_init
377 json_add_string name "${interface}_6"
378 json_add_string ifname "@$interface"
379 json_add_string proto "dhcpv6"
380 [ -n "$ip6table" ] && json_add_string ip6table "$ip6table"
381 proto_add_dynamic_defaults
382 # RFC 7278: Extend an IPv6 /64 Prefix to LAN
383 json_add_string extendprefix 1
384 [ -n "$zone" ] && json_add_string zone "$zone"
385 json_close_object
386 ubus call network add_dynamic "$(json_dump)"
387 fi
388 }
389
390 [ -n "$pdh_4" ] && {
391 if [ "$dhcp" = 0 ]; then
392 json_load "$(uqmi -s -d $device --set-client-id wds,$cid_4 --get-current-settings)"
393 json_select ipv4
394 json_get_var ip_4 ip
395 json_get_var gateway_4 gateway
396 json_get_var dns1_4 dns1
397 json_get_var dns2_4 dns2
398 json_get_var subnet_4 subnet
399
400 proto_init_update "$ifname" 1
401 proto_set_keep 1
402 proto_add_ipv4_address "$ip_4" "$subnet_4"
403 proto_add_ipv4_route "$gateway_4" "128"
404 [ "$defaultroute" = 0 ] || proto_add_ipv4_route "0.0.0.0" 0 "$gateway_4"
405 [ "$peerdns" = 0 ] || {
406 proto_add_dns_server "$dns1_4"
407 proto_add_dns_server "$dns2_4"
408 }
409 [ -n "$zone" ] && {
410 proto_add_data
411 json_add_string zone "$zone"
412 proto_close_data
413 }
414 proto_send_update "$interface"
415 else
416 json_init
417 json_add_string name "${interface}_4"
418 json_add_string ifname "@$interface"
419 json_add_string proto "dhcp"
420 [ -n "$ip4table" ] && json_add_string ip4table "$ip4table"
421 proto_add_dynamic_defaults
422 [ -n "$zone" ] && json_add_string zone "$zone"
423 json_close_object
424 ubus call network add_dynamic "$(json_dump)"
425 fi
426 }
427 }
428
429 qmi_wds_stop() {
430 local cid="$1"
431 local pdh="$2"
432
433 [ -n "$cid" ] || return
434
435 uqmi -s -d "$device" --set-client-id wds,"$cid" \
436 --stop-network 0xffffffff \
437 --autoconnect > /dev/null 2>&1
438
439 [ -n "$pdh" ] && {
440 uqmi -s -d "$device" --set-client-id wds,"$cid" \
441 --stop-network "$pdh" > /dev/null 2>&1
442 }
443
444 uqmi -s -d "$device" --set-client-id wds,"$cid" \
445 --release-client-id wds > /dev/null 2>&1
446 }
447
448 proto_qmi_teardown() {
449 local interface="$1"
450
451 local device cid_4 pdh_4 cid_6 pdh_6
452 json_get_vars device
453
454 [ -n "$ctl_device" ] && device=$ctl_device
455
456 echo "Stopping network $interface"
457
458 json_load "$(ubus call network.interface.$interface status)"
459 json_select data
460 json_get_vars cid_4 pdh_4 cid_6 pdh_6
461
462 qmi_wds_stop "$cid_4" "$pdh_4"
463 qmi_wds_stop "$cid_6" "$pdh_6"
464
465 proto_init_update "*" 0
466 proto_send_update "$interface"
467 }
468
469 [ -n "$INCLUDE_ONLY" ] || {
470 add_protocol qmi
471 }