3 [ -n "$INCLUDE_ONLY" ] ||
{
9 proto_qmi_init_config
() {
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 autoconnect
27 proto_config_add_int plmn
28 proto_config_add_int timeout
29 proto_config_add_int mtu
30 proto_config_add_defaults
35 local dataformat connstat plmn_mode mcc mnc
36 local device apn v6apn auth username password pincode delay modes pdptype
37 local profile v6profile dhcp dhcpv6 autoconnect plmn timeout mtu
$PROTO_DEFAULT_OPTIONS
38 local ip4table ip6table
39 local cid_4 pdh_4 cid_6 pdh_6
40 local ip_6 ip_prefix_length gateway_6 dns1_6 dns2_6
42 json_get_vars device apn v6apn auth username password pincode delay modes
43 json_get_vars pdptype profile v6profile dhcp dhcpv6 autoconnect plmn ip4table
44 json_get_vars ip6table timeout mtu
$PROTO_DEFAULT_OPTIONS
46 [ "$timeout" = "" ] && timeout
="10"
48 [ "$metric" = "" ] && metric
="0"
50 [ -n "$ctl_device" ] && device
=$ctl_device
53 echo "No control device specified"
54 proto_notify_error
"$interface" NO_DEVICE
55 proto_set_available
"$interface" 0
59 [ -n "$delay" ] && sleep "$delay"
61 device
="$(readlink -f $device)"
63 echo "The specified control device does not exist"
64 proto_notify_error
"$interface" NO_DEVICE
65 proto_set_available
"$interface" 0
69 devname
="$(basename "$device")"
70 devpath
="$(readlink -f /sys/class/usbmisc/$devname/device/)"
71 ifname
="$( ls "$devpath"/net )"
73 echo "The interface could not be found."
74 proto_notify_error
"$interface" NO_IFACE
75 proto_set_available
"$interface" 0
80 echo "Setting MTU to $mtu"
81 /sbin
/ip link
set dev
$ifname mtu
$mtu
84 echo "Waiting for SIM initialization"
85 local uninitialized_timeout
=0
86 # timeout 3s for first call to avoid hanging uqmi
87 uqmi
-d "$device" --get-pin-status -t 3000 > /dev
/null
2>&1
88 while uqmi
-s -d "$device" --get-pin-status |
grep '"UIM uninitialized"' > /dev
/null
; do
89 [ -e "$device" ] ||
return 1
90 if [ "$uninitialized_timeout" -lt "$timeout" -o "$timeout" = "0" ]; then
91 let uninitialized_timeout
++
94 echo "SIM not initialized"
95 proto_notify_error
"$interface" SIM_NOT_INITIALIZED
96 proto_block_restart
"$interface"
101 # Check if UIM application is stuck in illegal state
102 local uim_state_timeout
=0
104 json_load
"$(uqmi -s -d "$device" --uim-get-sim-state)"
105 json_get_var card_application_state card_application_state
107 # SIM card is either completely absent or state is labeled as illegal
108 # Try to power-cycle the SIM card to recover from this state
109 if [ -z "$card_application_state" -o "$card_application_state" = "illegal" ]; then
110 echo "SIM in illegal state - Power-cycling SIM"
112 # Try to reset SIM application
113 uqmi
-d "$device" --uim-power-off --uim-slot 1
115 uqmi
-d "$device" --uim-power-on --uim-slot 1
117 if [ "$uim_state_timeout" -lt "$timeout" ] ||
[ "$timeout" = "0" ]; then
118 let uim_state_timeout
++
124 proto_notify_error
"$interface" SIM_ILLEGAL_STATE
125 proto_block_restart
"$interface"
132 if uqmi
-s -d "$device" --uim-get-sim-state |
grep -q '"Not supported"\|"Invalid QMI command"' &&
133 uqmi
-s -d "$device" --get-pin-status |
grep -q '"Not supported"\|"Invalid QMI command"' ; then
134 [ -n "$pincode" ] && {
135 uqmi
-s -d "$device" --verify-pin1 "$pincode" > /dev
/null || uqmi
-s -d "$device" --uim-verify-pin1 "$pincode" > /dev
/null ||
{
136 echo "Unable to verify PIN"
137 proto_notify_error
"$interface" PIN_FAILED
138 proto_block_restart
"$interface"
143 json_load
"$(uqmi -s -d "$device" --get-pin-status)"
144 json_get_var pin1_status pin1_status
145 if [ -z "$pin1_status" ]; then
146 json_load
"$(uqmi -s -d "$device" --uim-get-sim-state)"
147 json_get_var pin1_status pin1_status
149 json_get_var pin1_verify_tries pin1_verify_tries
151 case "$pin1_status" in
153 echo "PIN verification is disabled"
156 echo "SIM locked PUK required"
157 proto_notify_error
"$interface" PUK_NEEDED
158 proto_block_restart
"$interface"
162 [ "$pin1_verify_tries" -lt "3" ] && {
163 echo "PIN verify count value is $pin1_verify_tries this is below the limit of 3"
164 proto_notify_error
"$interface" PIN_TRIES_BELOW_LIMIT
165 proto_block_restart
"$interface"
168 if [ -n "$pincode" ]; then
169 uqmi
-s -d "$device" --verify-pin1 "$pincode" > /dev
/null
2>&1 || uqmi
-s -d "$device" --uim-verify-pin1 "$pincode" > /dev
/null
2>&1 ||
{
170 echo "Unable to verify PIN"
171 proto_notify_error
"$interface" PIN_FAILED
172 proto_block_restart
"$interface"
176 echo "PIN not specified but required"
177 proto_notify_error
"$interface" PIN_NOT_SPECIFIED
178 proto_block_restart
"$interface"
183 echo "PIN already verified"
186 echo "PIN status failed (${pin1_status:-sim_not_present})"
187 proto_notify_error
"$interface" PIN_STATUS_FAILED
188 proto_block_restart
"$interface"
195 if [ -n "$plmn" ]; then
196 json_load
"$(uqmi -s -d "$device" --get-plmn)"
197 json_get_var plmn_mode mode
198 json_get_vars mcc mnc ||
{
203 if [ "$plmn" = "0" ]; then
204 if [ "$plmn_mode" != "automatic" ]; then
207 echo "Setting PLMN to auto"
209 elif [ "$mcc" -ne "${plmn:0:3}" -o "$mnc" -ne "${plmn:3}" ]; then
212 echo "Setting PLMN to $plmn"
219 if [ -n "$mcc" -a -n "$mnc" ]; then
220 uqmi
-s -d "$device" --set-plmn --mcc "$mcc" --mnc "$mnc" > /dev
/null
2>&1 ||
{
221 echo "Unable to set PLMN"
222 proto_notify_error
"$interface" PLMN_FAILED
223 proto_block_restart
"$interface"
228 # Cleanup current state if any
229 uqmi
-s -d "$device" --stop-network 0xffffffff --autoconnect > /dev
/null
2>&1
230 uqmi
-s -d "$device" --set-ip-family ipv6
--stop-network 0xffffffff --autoconnect > /dev
/null
2>&1
233 uqmi
-s -d "$device" --set-device-operating-mode online
> /dev
/null
2>&1
236 uqmi
-s -d "$device" --set-data-format 802.3 > /dev
/null
2>&1
237 uqmi
-s -d "$device" --wda-set-data-format 802.3 > /dev
/null
2>&1
238 dataformat
="$(uqmi -s -d "$device" --wda-get-data-format)"
240 if [ "$dataformat" = '"raw-ip"' ]; then
242 [ -f /sys
/class
/net
/$ifname/qmi
/raw_ip
] ||
{
243 echo "Device only supports raw-ip mode but is missing this required driver attribute: /sys/class/net/$ifname/qmi/raw_ip"
247 echo "Device does not support 802.3 mode. Informing driver of raw-ip only for $ifname .."
248 echo "Y" > /sys
/class
/net
/$ifname/qmi
/raw_ip
251 uqmi
-s -d "$device" --sync > /dev
/null
2>&1
253 uqmi
-s -d "$device" --network-register > /dev
/null
2>&1
256 uqmi
-s -d "$device" --set-network-modes "$modes" > /dev
/null
2>&1
258 # Scan network to not rely on registration-timeout after RAT change
259 uqmi
-s -d "$device" --network-scan > /dev
/null
2>&1
262 echo "Waiting for network registration"
264 local registration_timeout
=0
265 local registration_state
=""
267 registration_state
=$
(uqmi
-s -d "$device" --get-serving-system 2>/dev
/null | jsonfilter
-e "@.registration" 2>/dev
/null
)
269 [ "$registration_state" = "registered" ] && break
271 if [ "$registration_state" = "searching" ] ||
[ "$registration_state" = "not_registered" ]; then
272 if [ "$registration_timeout" -lt "$timeout" ] ||
[ "$timeout" = "0" ]; then
273 [ "$registration_state" = "searching" ] ||
{
274 echo "Device stopped network registration. Restart network registration"
275 uqmi
-s -d "$device" --network-register > /dev
/null
2>&1
277 let registration_timeout
++
281 echo "Network registration failed, registration timeout reached"
283 # registration_state is 'registration_denied' or 'unknown' or ''
284 echo "Network registration failed (reason: '$registration_state')"
287 proto_notify_error
"$interface" NETWORK_REGISTRATION_FAILED
292 echo "Starting network $interface"
294 pdptype
="$(echo "$pdptype" | awk '{print tolower($0)}')"
296 [ "$pdptype" = "ip" -o "$pdptype" = "ipv6" -o "$pdptype" = "ipv4v6" ] || pdptype
="ip"
298 if [ "$pdptype" = "ip" ]; then
299 [ -z "$autoconnect" ] && autoconnect
=1
300 [ "$autoconnect" = 0 ] && autoconnect
=""
302 [ "$autoconnect" = 1 ] || autoconnect
=""
305 [ "$pdptype" = "ip" -o "$pdptype" = "ipv4v6" ] && {
306 cid_4
=$
(uqmi
-s -d "$device" --get-client-id wds
)
307 if ! [ "$cid_4" -eq "$cid_4" ] 2> /dev
/null
; then
308 echo "Unable to obtain client ID"
309 proto_notify_error
"$interface" NO_CID
313 uqmi
-s -d "$device" --set-client-id wds
,"$cid_4" --set-ip-family ipv4
> /dev
/null
2>&1
315 pdh_4
=$
(uqmi
-s -d "$device" --set-client-id wds
,"$cid_4" \
318 ${profile:+--profile $profile} \
319 ${auth:+--auth-type $auth} \
320 ${username:+--username $username} \
321 ${password:+--password $password} \
322 ${autoconnect:+--autoconnect})
324 # pdh_4 is a numeric value on success
325 if ! [ "$pdh_4" -eq "$pdh_4" ] 2> /dev
/null
; then
326 echo "Unable to connect IPv4"
327 uqmi
-s -d "$device" --set-client-id wds
,"$cid_4" --release-client-id wds
> /dev
/null
2>&1
328 proto_notify_error
"$interface" CALL_FAILED
332 # Check data connection state
333 connstat
=$
(uqmi
-s -d "$device" --set-client-id wds
,"$cid_4" --get-data-status)
334 [ "$connstat" == '"connected"' ] ||
{
336 uqmi
-s -d "$device" --set-client-id wds
,"$cid_4" --release-client-id wds
> /dev
/null
2>&1
337 proto_notify_error
"$interface" CALL_FAILED
342 [ "$pdptype" = "ipv6" -o "$pdptype" = "ipv4v6" ] && {
343 cid_6
=$
(uqmi
-s -d "$device" --get-client-id wds
)
344 if ! [ "$cid_6" -eq "$cid_6" ] 2> /dev
/null
; then
345 echo "Unable to obtain client ID"
346 proto_notify_error
"$interface" NO_CID
350 uqmi
-s -d "$device" --set-client-id wds
,"$cid_6" --set-ip-family ipv6
> /dev
/null
2>&1
353 : "${v6profile:=${profile}}"
355 pdh_6
=$
(uqmi
-s -d "$device" --set-client-id wds
,"$cid_6" \
357 ${v6apn:+--apn $v6apn} \
358 ${v6profile:+--profile $v6profile} \
359 ${auth:+--auth-type $auth} \
360 ${username:+--username $username} \
361 ${password:+--password $password} \
362 ${autoconnect:+--autoconnect})
364 # pdh_6 is a numeric value on success
365 if ! [ "$pdh_6" -eq "$pdh_6" ] 2> /dev
/null
; then
366 echo "Unable to connect IPv6"
367 uqmi
-s -d "$device" --set-client-id wds
,"$cid_6" --release-client-id wds
> /dev
/null
2>&1
368 proto_notify_error
"$interface" CALL_FAILED
372 # Check data connection state
373 connstat
=$
(uqmi
-s -d "$device" --set-client-id wds
,"$cid_6" --set-ip-family ipv6
--get-data-status)
374 [ "$connstat" == '"connected"' ] ||
{
376 uqmi
-s -d "$device" --set-client-id wds
,"$cid_6" --release-client-id wds
> /dev
/null
2>&1
377 proto_notify_error
"$interface" CALL_FAILED
382 echo "Setting up $ifname"
383 proto_init_update
"$ifname" 1
387 json_add_string
"cid_4" "$cid_4"
388 json_add_string
"pdh_4" "$pdh_4"
391 json_add_string
"cid_6" "$cid_6"
392 json_add_string
"pdh_6" "$pdh_6"
395 proto_send_update
"$interface"
397 local zone
="$(fw3 -q network "$interface" 2>/dev/null)"
400 if [ -z "$dhcpv6" -o "$dhcpv6" = 0 ]; then
401 json_load
"$(uqmi -s -d $device --set-client-id wds,$cid_6 --get-current-settings)"
404 json_get_var gateway_6 gateway
405 json_get_var dns1_6 dns1
406 json_get_var dns2_6 dns2
407 json_get_var ip_prefix_length ip-prefix-length
409 proto_init_update
"$ifname" 1
411 proto_add_ipv6_address
"$ip_6" "128"
412 proto_add_ipv6_prefix
"${ip_6}/${ip_prefix_length}"
413 proto_add_ipv6_route
"$gateway_6" "128"
414 [ "$defaultroute" = 0 ] || proto_add_ipv6_route
"::0" 0 "$gateway_6" "" "" "${ip_6}/${ip_prefix_length}"
415 [ "$peerdns" = 0 ] ||
{
416 proto_add_dns_server
"$dns1_6"
417 proto_add_dns_server
"$dns2_6"
421 json_add_string zone
"$zone"
424 proto_send_update
"$interface"
427 json_add_string name
"${interface}_6"
428 json_add_string ifname
"@$interface"
429 [ "$pdptype" = "ipv4v6" ] && json_add_string iface_464xlat
"0"
430 json_add_string proto
"dhcpv6"
431 [ -n "$ip6table" ] && json_add_string ip6table
"$ip6table"
432 proto_add_dynamic_defaults
433 # RFC 7278: Extend an IPv6 /64 Prefix to LAN
434 json_add_string extendprefix
1
435 [ -n "$zone" ] && json_add_string zone
"$zone"
437 ubus call network add_dynamic
"$(json_dump)"
442 if [ "$dhcp" = 0 ]; then
443 json_load
"$(uqmi -s -d $device --set-client-id wds,$cid_4 --get-current-settings)"
446 json_get_var gateway_4 gateway
447 json_get_var dns1_4 dns1
448 json_get_var dns2_4 dns2
449 json_get_var subnet_4 subnet
451 proto_init_update
"$ifname" 1
453 proto_add_ipv4_address
"$ip_4" "$subnet_4"
454 proto_add_ipv4_route
"$gateway_4" "128"
455 [ "$defaultroute" = 0 ] || proto_add_ipv4_route
"0.0.0.0" 0 "$gateway_4"
456 [ "$peerdns" = 0 ] ||
{
457 proto_add_dns_server
"$dns1_4"
458 proto_add_dns_server
"$dns2_4"
462 json_add_string zone
"$zone"
465 proto_send_update
"$interface"
468 json_add_string name
"${interface}_4"
469 json_add_string ifname
"@$interface"
470 json_add_string proto
"dhcp"
471 [ -n "$ip4table" ] && json_add_string ip4table
"$ip4table"
472 proto_add_dynamic_defaults
473 [ -n "$zone" ] && json_add_string zone
"$zone"
475 ubus call network add_dynamic
"$(json_dump)"
484 [ -n "$cid" ] ||
return
486 uqmi
-s -d "$device" --set-client-id wds
,"$cid" \
487 --stop-network 0xffffffff \
488 --autoconnect > /dev
/null
2>&1
491 uqmi
-s -d "$device" --set-client-id wds
,"$cid" \
492 --stop-network "$pdh" > /dev
/null
2>&1
495 uqmi
-s -d "$device" --set-client-id wds
,"$cid" \
496 --release-client-id wds
> /dev
/null
2>&1
499 proto_qmi_teardown
() {
502 local device cid_4 pdh_4 cid_6 pdh_6
505 [ -n "$ctl_device" ] && device
=$ctl_device
507 echo "Stopping network $interface"
509 json_load
"$(ubus call network.interface.$interface status)"
511 json_get_vars cid_4 pdh_4 cid_6 pdh_6
513 qmi_wds_stop
"$cid_4" "$pdh_4"
514 qmi_wds_stop
"$cid_6" "$pdh_6"
516 proto_init_update
"*" 0
517 proto_send_update
"$interface"
520 [ -n "$INCLUDE_ONLY" ] ||
{