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 if uqmi
-s -d "$device" --uim-get-sim-state |
grep -q '"Not supported"\|"Invalid QMI command"' &&
102 uqmi
-s -d "$device" --get-pin-status |
grep -q '"Not supported"\|"Invalid QMI command"' ; then
103 [ -n "$pincode" ] && {
104 uqmi
-s -d "$device" --verify-pin1 "$pincode" > /dev
/null || uqmi
-s -d "$device" --uim-verify-pin1 "$pincode" > /dev
/null ||
{
105 echo "Unable to verify PIN"
106 proto_notify_error
"$interface" PIN_FAILED
107 proto_block_restart
"$interface"
112 json_load
"$(uqmi -s -d "$device" --get-pin-status)"
113 json_get_var pin1_status pin1_status
114 if [ -z "$pin1_status" ]; then
115 json_load
"$(uqmi -s -d "$device" --uim-get-sim-state)"
116 json_get_var pin1_status pin1_status
118 json_get_var pin1_verify_tries pin1_verify_tries
120 case "$pin1_status" in
122 echo "PIN verification is disabled"
125 echo "SIM locked PUK required"
126 proto_notify_error
"$interface" PUK_NEEDED
127 proto_block_restart
"$interface"
131 [ "$pin1_verify_tries" -lt "3" ] && {
132 echo "PIN verify count value is $pin1_verify_tries this is below the limit of 3"
133 proto_notify_error
"$interface" PIN_TRIES_BELOW_LIMIT
134 proto_block_restart
"$interface"
137 if [ -n "$pincode" ]; then
138 uqmi
-s -d "$device" --verify-pin1 "$pincode" > /dev
/null
2>&1 || uqmi
-s -d "$device" --uim-verify-pin1 "$pincode" > /dev
/null
2>&1 ||
{
139 echo "Unable to verify PIN"
140 proto_notify_error
"$interface" PIN_FAILED
141 proto_block_restart
"$interface"
145 echo "PIN not specified but required"
146 proto_notify_error
"$interface" PIN_NOT_SPECIFIED
147 proto_block_restart
"$interface"
152 echo "PIN already verified"
155 echo "PIN status failed (${pin1_status:-sim_not_present})"
156 proto_notify_error
"$interface" PIN_STATUS_FAILED
157 proto_block_restart
"$interface"
164 if [ -n "$plmn" ]; then
165 json_load
"$(uqmi -s -d "$device" --get-plmn)"
166 json_get_var plmn_mode mode
167 json_get_vars mcc mnc ||
{
172 if [ "$plmn" = "0" ]; then
173 if [ "$plmn_mode" != "automatic" ]; then
176 echo "Setting PLMN to auto"
178 elif [ "$mcc" -ne "${plmn:0:3}" -o "$mnc" -ne "${plmn:3}" ]; then
181 echo "Setting PLMN to $plmn"
188 if [ -n "$mcc" -a -n "$mnc" ]; then
189 uqmi
-s -d "$device" --set-plmn --mcc "$mcc" --mnc "$mnc" > /dev
/null
2>&1 ||
{
190 echo "Unable to set PLMN"
191 proto_notify_error
"$interface" PLMN_FAILED
192 proto_block_restart
"$interface"
197 # Cleanup current state if any
198 uqmi
-s -d "$device" --stop-network 0xffffffff --autoconnect > /dev
/null
2>&1
199 uqmi
-s -d "$device" --set-ip-family ipv6
--stop-network 0xffffffff --autoconnect > /dev
/null
2>&1
202 uqmi
-s -d "$device" --set-device-operating-mode online
> /dev
/null
2>&1
205 uqmi
-s -d "$device" --set-data-format 802.3 > /dev
/null
2>&1
206 uqmi
-s -d "$device" --wda-set-data-format 802.3 > /dev
/null
2>&1
207 dataformat
="$(uqmi -s -d "$device" --wda-get-data-format)"
209 if [ "$dataformat" = '"raw-ip"' ]; then
211 [ -f /sys
/class
/net
/$ifname/qmi
/raw_ip
] ||
{
212 echo "Device only supports raw-ip mode but is missing this required driver attribute: /sys/class/net/$ifname/qmi/raw_ip"
216 echo "Device does not support 802.3 mode. Informing driver of raw-ip only for $ifname .."
217 echo "Y" > /sys
/class
/net
/$ifname/qmi
/raw_ip
220 uqmi
-s -d "$device" --sync > /dev
/null
2>&1
222 uqmi
-s -d "$device" --network-register > /dev
/null
2>&1
224 echo "Waiting for network registration"
226 local registration_timeout
=0
227 local registration_state
=""
229 registration_state
=$
(uqmi
-s -d "$device" --get-serving-system 2>/dev
/null | jsonfilter
-e "@.registration" 2>/dev
/null
)
231 [ "$registration_state" = "registered" ] && break
233 if [ "$registration_state" = "searching" ] ||
[ "$registration_state" = "not_registered" ]; then
234 if [ "$registration_timeout" -lt "$timeout" ] ||
[ "$timeout" = "0" ]; then
235 [ "$registration_state" = "searching" ] ||
{
236 echo "Device stopped network registration. Restart network registration"
237 uqmi
-s -d "$device" --network-register > /dev
/null
2>&1
239 let registration_timeout
++
243 echo "Network registration failed, registration timeout reached"
245 # registration_state is 'registration_denied' or 'unknown' or ''
246 echo "Network registration failed (reason: '$registration_state')"
249 proto_notify_error
"$interface" NETWORK_REGISTRATION_FAILED
250 proto_block_restart
"$interface"
254 [ -n "$modes" ] && uqmi
-s -d "$device" --set-network-modes "$modes" > /dev
/null
2>&1
256 echo "Starting network $interface"
258 pdptype
="$(echo "$pdptype" | awk '{print tolower($0)}')"
260 [ "$pdptype" = "ip" -o "$pdptype" = "ipv6" -o "$pdptype" = "ipv4v6" ] || pdptype
="ip"
262 if [ "$pdptype" = "ip" ]; then
263 [ -z "$autoconnect" ] && autoconnect
=1
264 [ "$autoconnect" = 0 ] && autoconnect
=""
266 [ "$autoconnect" = 1 ] || autoconnect
=""
269 [ "$pdptype" = "ip" -o "$pdptype" = "ipv4v6" ] && {
270 cid_4
=$
(uqmi
-s -d "$device" --get-client-id wds
)
271 if ! [ "$cid_4" -eq "$cid_4" ] 2> /dev
/null
; then
272 echo "Unable to obtain client ID"
273 proto_notify_error
"$interface" NO_CID
277 uqmi
-s -d "$device" --set-client-id wds
,"$cid_4" --set-ip-family ipv4
> /dev
/null
2>&1
279 pdh_4
=$
(uqmi
-s -d "$device" --set-client-id wds
,"$cid_4" \
282 ${profile:+--profile $profile} \
283 ${auth:+--auth-type $auth} \
284 ${username:+--username $username} \
285 ${password:+--password $password} \
286 ${autoconnect:+--autoconnect})
288 # pdh_4 is a numeric value on success
289 if ! [ "$pdh_4" -eq "$pdh_4" ] 2> /dev
/null
; then
290 echo "Unable to connect IPv4"
291 uqmi
-s -d "$device" --set-client-id wds
,"$cid_4" --release-client-id wds
> /dev
/null
2>&1
292 proto_notify_error
"$interface" CALL_FAILED
296 # Check data connection state
297 connstat
=$
(uqmi
-s -d "$device" --set-client-id wds
,"$cid_4" --get-data-status)
298 [ "$connstat" == '"connected"' ] ||
{
300 uqmi
-s -d "$device" --set-client-id wds
,"$cid_4" --release-client-id wds
> /dev
/null
2>&1
301 proto_notify_error
"$interface" CALL_FAILED
306 [ "$pdptype" = "ipv6" -o "$pdptype" = "ipv4v6" ] && {
307 cid_6
=$
(uqmi
-s -d "$device" --get-client-id wds
)
308 if ! [ "$cid_6" -eq "$cid_6" ] 2> /dev
/null
; then
309 echo "Unable to obtain client ID"
310 proto_notify_error
"$interface" NO_CID
314 uqmi
-s -d "$device" --set-client-id wds
,"$cid_6" --set-ip-family ipv6
> /dev
/null
2>&1
317 : "${v6profile:=${profile}}"
319 pdh_6
=$
(uqmi
-s -d "$device" --set-client-id wds
,"$cid_6" \
321 ${v6apn:+--apn $v6apn} \
322 ${v6profile:+--profile $v6profile} \
323 ${auth:+--auth-type $auth} \
324 ${username:+--username $username} \
325 ${password:+--password $password} \
326 ${autoconnect:+--autoconnect})
328 # pdh_6 is a numeric value on success
329 if ! [ "$pdh_6" -eq "$pdh_6" ] 2> /dev
/null
; then
330 echo "Unable to connect IPv6"
331 uqmi
-s -d "$device" --set-client-id wds
,"$cid_6" --release-client-id wds
> /dev
/null
2>&1
332 proto_notify_error
"$interface" CALL_FAILED
336 # Check data connection state
337 connstat
=$
(uqmi
-s -d "$device" --set-client-id wds
,"$cid_6" --set-ip-family ipv6
--get-data-status)
338 [ "$connstat" == '"connected"' ] ||
{
340 uqmi
-s -d "$device" --set-client-id wds
,"$cid_6" --release-client-id wds
> /dev
/null
2>&1
341 proto_notify_error
"$interface" CALL_FAILED
346 echo "Setting up $ifname"
347 proto_init_update
"$ifname" 1
351 json_add_string
"cid_4" "$cid_4"
352 json_add_string
"pdh_4" "$pdh_4"
355 json_add_string
"cid_6" "$cid_6"
356 json_add_string
"pdh_6" "$pdh_6"
359 proto_send_update
"$interface"
361 local zone
="$(fw3 -q network "$interface" 2>/dev/null)"
364 if [ -z "$dhcpv6" -o "$dhcpv6" = 0 ]; then
365 json_load
"$(uqmi -s -d $device --set-client-id wds,$cid_6 --get-current-settings)"
368 json_get_var gateway_6 gateway
369 json_get_var dns1_6 dns1
370 json_get_var dns2_6 dns2
371 json_get_var ip_prefix_length ip-prefix-length
373 proto_init_update
"$ifname" 1
375 proto_add_ipv6_address
"$ip_6" "128"
376 proto_add_ipv6_prefix
"${ip_6}/${ip_prefix_length}"
377 proto_add_ipv6_route
"$gateway_6" "128"
378 [ "$defaultroute" = 0 ] || proto_add_ipv6_route
"::0" 0 "$gateway_6" "" "" "${ip_6}/${ip_prefix_length}"
379 [ "$peerdns" = 0 ] ||
{
380 proto_add_dns_server
"$dns1_6"
381 proto_add_dns_server
"$dns2_6"
385 json_add_string zone
"$zone"
388 proto_send_update
"$interface"
391 json_add_string name
"${interface}_6"
392 json_add_string ifname
"@$interface"
393 [ "$pdptype" = "ipv4v6" ] && json_add_string iface_464xlat
"0"
394 json_add_string proto
"dhcpv6"
395 [ -n "$ip6table" ] && json_add_string ip6table
"$ip6table"
396 proto_add_dynamic_defaults
397 # RFC 7278: Extend an IPv6 /64 Prefix to LAN
398 json_add_string extendprefix
1
399 [ -n "$zone" ] && json_add_string zone
"$zone"
401 ubus call network add_dynamic
"$(json_dump)"
406 if [ "$dhcp" = 0 ]; then
407 json_load
"$(uqmi -s -d $device --set-client-id wds,$cid_4 --get-current-settings)"
410 json_get_var gateway_4 gateway
411 json_get_var dns1_4 dns1
412 json_get_var dns2_4 dns2
413 json_get_var subnet_4 subnet
415 proto_init_update
"$ifname" 1
417 proto_add_ipv4_address
"$ip_4" "$subnet_4"
418 proto_add_ipv4_route
"$gateway_4" "128"
419 [ "$defaultroute" = 0 ] || proto_add_ipv4_route
"0.0.0.0" 0 "$gateway_4"
420 [ "$peerdns" = 0 ] ||
{
421 proto_add_dns_server
"$dns1_4"
422 proto_add_dns_server
"$dns2_4"
426 json_add_string zone
"$zone"
429 proto_send_update
"$interface"
432 json_add_string name
"${interface}_4"
433 json_add_string ifname
"@$interface"
434 json_add_string proto
"dhcp"
435 [ -n "$ip4table" ] && json_add_string ip4table
"$ip4table"
436 proto_add_dynamic_defaults
437 [ -n "$zone" ] && json_add_string zone
"$zone"
439 ubus call network add_dynamic
"$(json_dump)"
448 [ -n "$cid" ] ||
return
450 uqmi
-s -d "$device" --set-client-id wds
,"$cid" \
451 --stop-network 0xffffffff \
452 --autoconnect > /dev
/null
2>&1
455 uqmi
-s -d "$device" --set-client-id wds
,"$cid" \
456 --stop-network "$pdh" > /dev
/null
2>&1
459 uqmi
-s -d "$device" --set-client-id wds
,"$cid" \
460 --release-client-id wds
> /dev
/null
2>&1
463 proto_qmi_teardown
() {
466 local device cid_4 pdh_4 cid_6 pdh_6
469 [ -n "$ctl_device" ] && device
=$ctl_device
471 echo "Stopping network $interface"
473 json_load
"$(ubus call network.interface.$interface status)"
475 json_get_vars cid_4 pdh_4 cid_6 pdh_6
477 qmi_wds_stop
"$cid_4" "$pdh_4"
478 qmi_wds_stop
"$cid_6" "$pdh_6"
480 proto_init_update
"*" 0
481 proto_send_update
"$interface"
484 [ -n "$INCLUDE_ONLY" ] ||
{