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