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
253 [ -n "$modes" ] && uqmi
-s -d "$device" --set-network-modes "$modes" > /dev
/null
2>&1
255 echo "Starting network $interface"
257 pdptype
="$(echo "$pdptype" | awk '{print tolower($0)}')"
259 [ "$pdptype" = "ip" -o "$pdptype" = "ipv6" -o "$pdptype" = "ipv4v6" ] || pdptype
="ip"
261 if [ "$pdptype" = "ip" ]; then
262 [ -z "$autoconnect" ] && autoconnect
=1
263 [ "$autoconnect" = 0 ] && autoconnect
=""
265 [ "$autoconnect" = 1 ] || autoconnect
=""
268 [ "$pdptype" = "ip" -o "$pdptype" = "ipv4v6" ] && {
269 cid_4
=$
(uqmi
-s -d "$device" --get-client-id wds
)
270 if ! [ "$cid_4" -eq "$cid_4" ] 2> /dev
/null
; then
271 echo "Unable to obtain client ID"
272 proto_notify_error
"$interface" NO_CID
276 uqmi
-s -d "$device" --set-client-id wds
,"$cid_4" --set-ip-family ipv4
> /dev
/null
2>&1
278 pdh_4
=$
(uqmi
-s -d "$device" --set-client-id wds
,"$cid_4" \
281 ${profile:+--profile $profile} \
282 ${auth:+--auth-type $auth} \
283 ${username:+--username $username} \
284 ${password:+--password $password} \
285 ${autoconnect:+--autoconnect})
287 # pdh_4 is a numeric value on success
288 if ! [ "$pdh_4" -eq "$pdh_4" ] 2> /dev
/null
; then
289 echo "Unable to connect IPv4"
290 uqmi
-s -d "$device" --set-client-id wds
,"$cid_4" --release-client-id wds
> /dev
/null
2>&1
291 proto_notify_error
"$interface" CALL_FAILED
295 # Check data connection state
296 connstat
=$
(uqmi
-s -d "$device" --set-client-id wds
,"$cid_4" --get-data-status)
297 [ "$connstat" == '"connected"' ] ||
{
299 uqmi
-s -d "$device" --set-client-id wds
,"$cid_4" --release-client-id wds
> /dev
/null
2>&1
300 proto_notify_error
"$interface" CALL_FAILED
305 [ "$pdptype" = "ipv6" -o "$pdptype" = "ipv4v6" ] && {
306 cid_6
=$
(uqmi
-s -d "$device" --get-client-id wds
)
307 if ! [ "$cid_6" -eq "$cid_6" ] 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_6" --set-ip-family ipv6
> /dev
/null
2>&1
316 : "${v6profile:=${profile}}"
318 pdh_6
=$
(uqmi
-s -d "$device" --set-client-id wds
,"$cid_6" \
320 ${v6apn:+--apn $v6apn} \
321 ${v6profile:+--profile $v6profile} \
322 ${auth:+--auth-type $auth} \
323 ${username:+--username $username} \
324 ${password:+--password $password} \
325 ${autoconnect:+--autoconnect})
327 # pdh_6 is a numeric value on success
328 if ! [ "$pdh_6" -eq "$pdh_6" ] 2> /dev
/null
; then
329 echo "Unable to connect IPv6"
330 uqmi
-s -d "$device" --set-client-id wds
,"$cid_6" --release-client-id wds
> /dev
/null
2>&1
331 proto_notify_error
"$interface" CALL_FAILED
335 # Check data connection state
336 connstat
=$
(uqmi
-s -d "$device" --set-client-id wds
,"$cid_6" --set-ip-family ipv6
--get-data-status)
337 [ "$connstat" == '"connected"' ] ||
{
339 uqmi
-s -d "$device" --set-client-id wds
,"$cid_6" --release-client-id wds
> /dev
/null
2>&1
340 proto_notify_error
"$interface" CALL_FAILED
345 echo "Setting up $ifname"
346 proto_init_update
"$ifname" 1
350 json_add_string
"cid_4" "$cid_4"
351 json_add_string
"pdh_4" "$pdh_4"
354 json_add_string
"cid_6" "$cid_6"
355 json_add_string
"pdh_6" "$pdh_6"
358 proto_send_update
"$interface"
360 local zone
="$(fw3 -q network "$interface" 2>/dev/null)"
363 if [ -z "$dhcpv6" -o "$dhcpv6" = 0 ]; then
364 json_load
"$(uqmi -s -d $device --set-client-id wds,$cid_6 --get-current-settings)"
367 json_get_var gateway_6 gateway
368 json_get_var dns1_6 dns1
369 json_get_var dns2_6 dns2
370 json_get_var ip_prefix_length ip-prefix-length
372 proto_init_update
"$ifname" 1
374 proto_add_ipv6_address
"$ip_6" "128"
375 proto_add_ipv6_prefix
"${ip_6}/${ip_prefix_length}"
376 proto_add_ipv6_route
"$gateway_6" "128"
377 [ "$defaultroute" = 0 ] || proto_add_ipv6_route
"::0" 0 "$gateway_6" "" "" "${ip_6}/${ip_prefix_length}"
378 [ "$peerdns" = 0 ] ||
{
379 proto_add_dns_server
"$dns1_6"
380 proto_add_dns_server
"$dns2_6"
384 json_add_string zone
"$zone"
387 proto_send_update
"$interface"
390 json_add_string name
"${interface}_6"
391 json_add_string ifname
"@$interface"
392 [ "$pdptype" = "ipv4v6" ] && json_add_string iface_464xlat
"0"
393 json_add_string proto
"dhcpv6"
394 [ -n "$ip6table" ] && json_add_string ip6table
"$ip6table"
395 proto_add_dynamic_defaults
396 # RFC 7278: Extend an IPv6 /64 Prefix to LAN
397 json_add_string extendprefix
1
398 [ -n "$zone" ] && json_add_string zone
"$zone"
400 ubus call network add_dynamic
"$(json_dump)"
405 if [ "$dhcp" = 0 ]; then
406 json_load
"$(uqmi -s -d $device --set-client-id wds,$cid_4 --get-current-settings)"
409 json_get_var gateway_4 gateway
410 json_get_var dns1_4 dns1
411 json_get_var dns2_4 dns2
412 json_get_var subnet_4 subnet
414 proto_init_update
"$ifname" 1
416 proto_add_ipv4_address
"$ip_4" "$subnet_4"
417 proto_add_ipv4_route
"$gateway_4" "128"
418 [ "$defaultroute" = 0 ] || proto_add_ipv4_route
"0.0.0.0" 0 "$gateway_4"
419 [ "$peerdns" = 0 ] ||
{
420 proto_add_dns_server
"$dns1_4"
421 proto_add_dns_server
"$dns2_4"
425 json_add_string zone
"$zone"
428 proto_send_update
"$interface"
431 json_add_string name
"${interface}_4"
432 json_add_string ifname
"@$interface"
433 json_add_string proto
"dhcp"
434 [ -n "$ip4table" ] && json_add_string ip4table
"$ip4table"
435 proto_add_dynamic_defaults
436 [ -n "$zone" ] && json_add_string zone
"$zone"
438 ubus call network add_dynamic
"$(json_dump)"
447 [ -n "$cid" ] ||
return
449 uqmi
-s -d "$device" --set-client-id wds
,"$cid" \
450 --stop-network 0xffffffff \
451 --autoconnect > /dev
/null
2>&1
454 uqmi
-s -d "$device" --set-client-id wds
,"$cid" \
455 --stop-network "$pdh" > /dev
/null
2>&1
458 uqmi
-s -d "$device" --set-client-id wds
,"$cid" \
459 --release-client-id wds
> /dev
/null
2>&1
462 proto_qmi_teardown
() {
465 local device cid_4 pdh_4 cid_6 pdh_6
468 [ -n "$ctl_device" ] && device
=$ctl_device
470 echo "Stopping network $interface"
472 json_load
"$(ubus call network.interface.$interface status)"
474 json_get_vars cid_4 pdh_4 cid_6 pdh_6
476 qmi_wds_stop
"$cid_4" "$pdh_4"
477 qmi_wds_stop
"$cid_6" "$pdh_6"
479 proto_init_update
"*" 0
480 proto_send_update
"$interface"
483 [ -n "$INCLUDE_ONLY" ] ||
{