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 while uqmi
-s -d "$device" --get-pin-status |
grep '"UIM uninitialized"' > /dev
/null
; do
87 [ -e "$device" ] ||
return 1
88 if [ "$uninitialized_timeout" -lt "$timeout" -o "$timeout" = "0" ]; then
89 let uninitialized_timeout
++
92 echo "SIM not initialized"
93 proto_notify_error
"$interface" SIM_NOT_INITIALIZED
94 proto_block_restart
"$interface"
99 if uqmi
-s -d "$device" --uim-get-sim-state |
grep -q '"Not supported"\|"Invalid QMI command"' &&
100 uqmi
-s -d "$device" --get-pin-status |
grep -q '"Not supported"\|"Invalid QMI command"' ; then
101 [ -n "$pincode" ] && {
102 uqmi
-s -d "$device" --verify-pin1 "$pincode" > /dev
/null || uqmi
-s -d "$device" --uim-verify-pin1 "$pincode" > /dev
/null ||
{
103 echo "Unable to verify PIN"
104 proto_notify_error
"$interface" PIN_FAILED
105 proto_block_restart
"$interface"
110 json_load
"$(uqmi -s -d "$device" --get-pin-status)"
111 json_get_var pin1_status pin1_status
112 if [ -z "$pin1_status" ]; then
113 json_load
"$(uqmi -s -d "$device" --uim-get-sim-state)"
114 json_get_var pin1_status pin1_status
116 json_get_var pin1_verify_tries pin1_verify_tries
118 case "$pin1_status" in
120 echo "PIN verification is disabled"
123 echo "SIM locked PUK required"
124 proto_notify_error
"$interface" PUK_NEEDED
125 proto_block_restart
"$interface"
129 [ "$pin1_verify_tries" -lt "3" ] && {
130 echo "PIN verify count value is $pin1_verify_tries this is below the limit of 3"
131 proto_notify_error
"$interface" PIN_TRIES_BELOW_LIMIT
132 proto_block_restart
"$interface"
135 if [ -n "$pincode" ]; then
136 uqmi
-s -d "$device" --verify-pin1 "$pincode" > /dev
/null
2>&1 || uqmi
-s -d "$device" --uim-verify-pin1 "$pincode" > /dev
/null
2>&1 ||
{
137 echo "Unable to verify PIN"
138 proto_notify_error
"$interface" PIN_FAILED
139 proto_block_restart
"$interface"
143 echo "PIN not specified but required"
144 proto_notify_error
"$interface" PIN_NOT_SPECIFIED
145 proto_block_restart
"$interface"
150 echo "PIN already verified"
153 echo "PIN status failed (${pin1_status:-sim_not_present})"
154 proto_notify_error
"$interface" PIN_STATUS_FAILED
155 proto_block_restart
"$interface"
162 if [ -n "$plmn" ]; then
163 json_load
"$(uqmi -s -d "$device" --get-plmn)"
164 json_get_var plmn_mode mode
165 json_get_vars mcc mnc ||
{
170 if [ "$plmn" = "0" ]; then
171 if [ "$plmn_mode" != "automatic" ]; then
174 echo "Setting PLMN to auto"
176 elif [ "$mcc" -ne "${plmn:0:3}" -o "$mnc" -ne "${plmn:3}" ]; then
179 echo "Setting PLMN to $plmn"
186 if [ -n "$mcc" -a -n "$mnc" ]; then
187 uqmi
-s -d "$device" --set-plmn --mcc "$mcc" --mnc "$mnc" > /dev
/null
2>&1 ||
{
188 echo "Unable to set PLMN"
189 proto_notify_error
"$interface" PLMN_FAILED
190 proto_block_restart
"$interface"
195 # Cleanup current state if any
196 uqmi
-s -d "$device" --stop-network 0xffffffff --autoconnect > /dev
/null
2>&1
197 uqmi
-s -d "$device" --set-ip-family ipv6
--stop-network 0xffffffff --autoconnect > /dev
/null
2>&1
200 uqmi
-s -d "$device" --set-device-operating-mode online
> /dev
/null
2>&1
203 uqmi
-s -d "$device" --set-data-format 802.3 > /dev
/null
2>&1
204 uqmi
-s -d "$device" --wda-set-data-format 802.3 > /dev
/null
2>&1
205 dataformat
="$(uqmi -s -d "$device" --wda-get-data-format)"
207 if [ "$dataformat" = '"raw-ip"' ]; then
209 [ -f /sys
/class
/net
/$ifname/qmi
/raw_ip
] ||
{
210 echo "Device only supports raw-ip mode but is missing this required driver attribute: /sys/class/net/$ifname/qmi/raw_ip"
214 echo "Device does not support 802.3 mode. Informing driver of raw-ip only for $ifname .."
215 echo "Y" > /sys
/class
/net
/$ifname/qmi
/raw_ip
218 uqmi
-s -d "$device" --sync > /dev
/null
2>&1
220 uqmi
-s -d "$device" --network-register > /dev
/null
2>&1
222 echo "Waiting for network registration"
224 local registration_timeout
=0
225 local registration_state
=""
227 registration_state
=$
(uqmi
-s -d "$device" --get-serving-system 2>/dev
/null | jsonfilter
-e "@.registration" 2>/dev
/null
)
229 [ "$registration_state" = "registered" ] && break
231 if [ "$registration_state" = "searching" ] ||
[ "$registration_state" = "not_registered" ]; then
232 if [ "$registration_timeout" -lt "$timeout" ] ||
[ "$timeout" = "0" ]; then
233 [ "$registration_state" = "searching" ] ||
{
234 echo "Device stopped network registration. Restart network registration"
235 uqmi
-s -d "$device" --network-register > /dev
/null
2>&1
237 let registration_timeout
++
241 echo "Network registration failed, registration timeout reached"
243 # registration_state is 'registration_denied' or 'unknown' or ''
244 echo "Network registration failed (reason: '$registration_state')"
247 proto_notify_error
"$interface" NETWORK_REGISTRATION_FAILED
248 proto_block_restart
"$interface"
252 [ -n "$modes" ] && uqmi
-s -d "$device" --set-network-modes "$modes" > /dev
/null
2>&1
254 echo "Starting network $interface"
256 pdptype
="$(echo "$pdptype" | awk '{print tolower($0)}')"
258 [ "$pdptype" = "ip" -o "$pdptype" = "ipv6" -o "$pdptype" = "ipv4v6" ] || pdptype
="ip"
260 if [ "$pdptype" = "ip" ]; then
261 [ -z "$autoconnect" ] && autoconnect
=1
262 [ "$autoconnect" = 0 ] && autoconnect
=""
264 [ "$autoconnect" = 1 ] || autoconnect
=""
267 [ "$pdptype" = "ip" -o "$pdptype" = "ipv4v6" ] && {
268 cid_4
=$
(uqmi
-s -d "$device" --get-client-id wds
)
269 if ! [ "$cid_4" -eq "$cid_4" ] 2> /dev
/null
; then
270 echo "Unable to obtain client ID"
271 proto_notify_error
"$interface" NO_CID
275 uqmi
-s -d "$device" --set-client-id wds
,"$cid_4" --set-ip-family ipv4
> /dev
/null
2>&1
277 pdh_4
=$
(uqmi
-s -d "$device" --set-client-id wds
,"$cid_4" \
280 ${profile:+--profile $profile} \
281 ${auth:+--auth-type $auth} \
282 ${username:+--username $username} \
283 ${password:+--password $password} \
284 ${autoconnect:+--autoconnect})
286 # pdh_4 is a numeric value on success
287 if ! [ "$pdh_4" -eq "$pdh_4" ] 2> /dev
/null
; then
288 echo "Unable to connect IPv4"
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
294 # Check data connection state
295 connstat
=$
(uqmi
-s -d "$device" --set-client-id wds
,"$cid_4" --get-data-status)
296 [ "$connstat" == '"connected"' ] ||
{
298 uqmi
-s -d "$device" --set-client-id wds
,"$cid_4" --release-client-id wds
> /dev
/null
2>&1
299 proto_notify_error
"$interface" CALL_FAILED
304 [ "$pdptype" = "ipv6" -o "$pdptype" = "ipv4v6" ] && {
305 cid_6
=$
(uqmi
-s -d "$device" --get-client-id wds
)
306 if ! [ "$cid_6" -eq "$cid_6" ] 2> /dev
/null
; then
307 echo "Unable to obtain client ID"
308 proto_notify_error
"$interface" NO_CID
312 uqmi
-s -d "$device" --set-client-id wds
,"$cid_6" --set-ip-family ipv6
> /dev
/null
2>&1
315 : "${v6profile:=${profile}}"
317 pdh_6
=$
(uqmi
-s -d "$device" --set-client-id wds
,"$cid_6" \
319 ${v6apn:+--apn $v6apn} \
320 ${v6profile:+--profile $v6profile} \
321 ${auth:+--auth-type $auth} \
322 ${username:+--username $username} \
323 ${password:+--password $password} \
324 ${autoconnect:+--autoconnect})
326 # pdh_6 is a numeric value on success
327 if ! [ "$pdh_6" -eq "$pdh_6" ] 2> /dev
/null
; then
328 echo "Unable to connect IPv6"
329 uqmi
-s -d "$device" --set-client-id wds
,"$cid_6" --release-client-id wds
> /dev
/null
2>&1
330 proto_notify_error
"$interface" CALL_FAILED
334 # Check data connection state
335 connstat
=$
(uqmi
-s -d "$device" --set-client-id wds
,"$cid_6" --set-ip-family ipv6
--get-data-status)
336 [ "$connstat" == '"connected"' ] ||
{
338 uqmi
-s -d "$device" --set-client-id wds
,"$cid_6" --release-client-id wds
> /dev
/null
2>&1
339 proto_notify_error
"$interface" CALL_FAILED
344 echo "Setting up $ifname"
345 proto_init_update
"$ifname" 1
349 json_add_string
"cid_4" "$cid_4"
350 json_add_string
"pdh_4" "$pdh_4"
353 json_add_string
"cid_6" "$cid_6"
354 json_add_string
"pdh_6" "$pdh_6"
357 proto_send_update
"$interface"
359 local zone
="$(fw3 -q network "$interface" 2>/dev/null)"
362 if [ -z "$dhcpv6" -o "$dhcpv6" = 0 ]; then
363 json_load
"$(uqmi -s -d $device --set-client-id wds,$cid_6 --get-current-settings)"
366 json_get_var gateway_6 gateway
367 json_get_var dns1_6 dns1
368 json_get_var dns2_6 dns2
369 json_get_var ip_prefix_length ip-prefix-length
371 proto_init_update
"$ifname" 1
373 proto_add_ipv6_address
"$ip_6" "128"
374 proto_add_ipv6_prefix
"${ip_6}/${ip_prefix_length}"
375 proto_add_ipv6_route
"$gateway_6" "128"
376 [ "$defaultroute" = 0 ] || proto_add_ipv6_route
"::0" 0 "$gateway_6" "" "" "${ip_6}/${ip_prefix_length}"
377 [ "$peerdns" = 0 ] ||
{
378 proto_add_dns_server
"$dns1_6"
379 proto_add_dns_server
"$dns2_6"
383 json_add_string zone
"$zone"
386 proto_send_update
"$interface"
389 json_add_string name
"${interface}_6"
390 json_add_string ifname
"@$interface"
391 [ "$pdptype" = "ipv4v6" ] && json_add_string iface_464xlat
"0"
392 json_add_string proto
"dhcpv6"
393 [ -n "$ip6table" ] && json_add_string ip6table
"$ip6table"
394 proto_add_dynamic_defaults
395 # RFC 7278: Extend an IPv6 /64 Prefix to LAN
396 json_add_string extendprefix
1
397 [ -n "$zone" ] && json_add_string zone
"$zone"
399 ubus call network add_dynamic
"$(json_dump)"
404 if [ "$dhcp" = 0 ]; then
405 json_load
"$(uqmi -s -d $device --set-client-id wds,$cid_4 --get-current-settings)"
408 json_get_var gateway_4 gateway
409 json_get_var dns1_4 dns1
410 json_get_var dns2_4 dns2
411 json_get_var subnet_4 subnet
413 proto_init_update
"$ifname" 1
415 proto_add_ipv4_address
"$ip_4" "$subnet_4"
416 proto_add_ipv4_route
"$gateway_4" "128"
417 [ "$defaultroute" = 0 ] || proto_add_ipv4_route
"0.0.0.0" 0 "$gateway_4"
418 [ "$peerdns" = 0 ] ||
{
419 proto_add_dns_server
"$dns1_4"
420 proto_add_dns_server
"$dns2_4"
424 json_add_string zone
"$zone"
427 proto_send_update
"$interface"
430 json_add_string name
"${interface}_4"
431 json_add_string ifname
"@$interface"
432 json_add_string proto
"dhcp"
433 [ -n "$ip4table" ] && json_add_string ip4table
"$ip4table"
434 proto_add_dynamic_defaults
435 [ -n "$zone" ] && json_add_string zone
"$zone"
437 ubus call network add_dynamic
"$(json_dump)"
446 [ -n "$cid" ] ||
return
448 uqmi
-s -d "$device" --set-client-id wds
,"$cid" \
449 --stop-network 0xffffffff \
450 --autoconnect > /dev
/null
2>&1
453 uqmi
-s -d "$device" --set-client-id wds
,"$cid" \
454 --stop-network "$pdh" > /dev
/null
2>&1
457 uqmi
-s -d "$device" --set-client-id wds
,"$cid" \
458 --release-client-id wds
> /dev
/null
2>&1
461 proto_qmi_teardown
() {
464 local device cid_4 pdh_4 cid_6 pdh_6
467 [ -n "$ctl_device" ] && device
=$ctl_device
469 echo "Stopping network $interface"
471 json_load
"$(ubus call network.interface.$interface status)"
473 json_get_vars cid_4 pdh_4 cid_6 pdh_6
475 qmi_wds_stop
"$cid_4" "$pdh_4"
476 qmi_wds_stop
"$cid_6" "$pdh_6"
478 proto_init_update
"*" 0
479 proto_send_update
"$interface"
482 [ -n "$INCLUDE_ONLY" ] ||
{