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