eab7f1ea13d45d33b7cd284c6f82865faf66be77
[openwrt/staging/jow.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 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
31 }
32
33 proto_qmi_setup() {
34 local interface="$1"
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
41
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
45
46 [ "$timeout" = "" ] && timeout="10"
47
48 [ "$metric" = "" ] && metric="0"
49
50 [ -n "$ctl_device" ] && device=$ctl_device
51
52 [ -n "$device" ] || {
53 echo "No control device specified"
54 proto_notify_error "$interface" NO_DEVICE
55 proto_set_available "$interface" 0
56 return 1
57 }
58
59 [ -n "$delay" ] && sleep "$delay"
60
61 device="$(readlink -f $device)"
62 [ -c "$device" ] || {
63 echo "The specified control device does not exist"
64 proto_notify_error "$interface" NO_DEVICE
65 proto_set_available "$interface" 0
66 return 1
67 }
68
69 devname="$(basename "$device")"
70 devpath="$(readlink -f /sys/class/usbmisc/$devname/device/)"
71 ifname="$( ls "$devpath"/net )"
72 [ -n "$ifname" ] || {
73 echo "The interface could not be found."
74 proto_notify_error "$interface" NO_IFACE
75 proto_set_available "$interface" 0
76 return 1
77 }
78
79 [ -n "$mtu" ] && {
80 echo "Setting MTU to $mtu"
81 /sbin/ip link set dev $ifname mtu $mtu
82 }
83
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++
92 sleep 1;
93 else
94 echo "SIM not initialized"
95 proto_notify_error "$interface" SIM_NOT_INITIALIZED
96 proto_block_restart "$interface"
97 return 1
98 fi
99 done
100
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"
108 return 1
109 }
110 }
111 else
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
117 fi
118 json_get_var pin1_verify_tries pin1_verify_tries
119
120 case "$pin1_status" in
121 disabled)
122 echo "PIN verification is disabled"
123 ;;
124 blocked)
125 echo "SIM locked PUK required"
126 proto_notify_error "$interface" PUK_NEEDED
127 proto_block_restart "$interface"
128 return 1
129 ;;
130 not_verified)
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"
135 return 1
136 }
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"
142 return 1
143 }
144 else
145 echo "PIN not specified but required"
146 proto_notify_error "$interface" PIN_NOT_SPECIFIED
147 proto_block_restart "$interface"
148 return 1
149 fi
150 ;;
151 verified)
152 echo "PIN already verified"
153 ;;
154 *)
155 echo "PIN status failed (${pin1_status:-sim_not_present})"
156 proto_notify_error "$interface" PIN_STATUS_FAILED
157 proto_block_restart "$interface"
158 return 1
159 ;;
160 esac
161 json_cleanup
162 fi
163
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 || {
168 mcc=0
169 mnc=0
170 }
171
172 if [ "$plmn" = "0" ]; then
173 if [ "$plmn_mode" != "automatic" ]; then
174 mcc=0
175 mnc=0
176 echo "Setting PLMN to auto"
177 fi
178 elif [ "$mcc" -ne "${plmn:0:3}" -o "$mnc" -ne "${plmn:3}" ]; then
179 mcc=${plmn:0:3}
180 mnc=${plmn:3}
181 echo "Setting PLMN to $plmn"
182 else
183 mcc=""
184 mnc=""
185 fi
186 fi
187
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"
193 return 1
194 }
195 fi
196
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
200
201 # Go online
202 uqmi -s -d "$device" --set-device-operating-mode online > /dev/null 2>&1
203
204 # Set IP format
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)"
208
209 if [ "$dataformat" = '"raw-ip"' ]; then
210
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"
213 return 1
214 }
215
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
218 fi
219
220 uqmi -s -d "$device" --sync > /dev/null 2>&1
221
222 uqmi -s -d "$device" --network-register > /dev/null 2>&1
223
224 echo "Waiting for network registration"
225 sleep 1
226 local registration_timeout=0
227 local registration_state=""
228 while true; do
229 registration_state=$(uqmi -s -d "$device" --get-serving-system 2>/dev/null | jsonfilter -e "@.registration" 2>/dev/null)
230
231 [ "$registration_state" = "registered" ] && break
232
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
238 }
239 let registration_timeout++
240 sleep 1
241 continue
242 fi
243 echo "Network registration failed, registration timeout reached"
244 else
245 # registration_state is 'registration_denied' or 'unknown' or ''
246 echo "Network registration failed (reason: '$registration_state')"
247 fi
248
249 proto_notify_error "$interface" NETWORK_REGISTRATION_FAILED
250 proto_block_restart "$interface"
251 return 1
252 done
253
254 [ -n "$modes" ] && uqmi -s -d "$device" --set-network-modes "$modes" > /dev/null 2>&1
255
256 echo "Starting network $interface"
257
258 pdptype="$(echo "$pdptype" | awk '{print tolower($0)}')"
259
260 [ "$pdptype" = "ip" -o "$pdptype" = "ipv6" -o "$pdptype" = "ipv4v6" ] || pdptype="ip"
261
262 if [ "$pdptype" = "ip" ]; then
263 [ -z "$autoconnect" ] && autoconnect=1
264 [ "$autoconnect" = 0 ] && autoconnect=""
265 else
266 [ "$autoconnect" = 1 ] || autoconnect=""
267 fi
268
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
274 return 1
275 fi
276
277 uqmi -s -d "$device" --set-client-id wds,"$cid_4" --set-ip-family ipv4 > /dev/null 2>&1
278
279 pdh_4=$(uqmi -s -d "$device" --set-client-id wds,"$cid_4" \
280 --start-network \
281 ${apn:+--apn $apn} \
282 ${profile:+--profile $profile} \
283 ${auth:+--auth-type $auth} \
284 ${username:+--username $username} \
285 ${password:+--password $password} \
286 ${autoconnect:+--autoconnect})
287
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
293 return 1
294 fi
295
296 # Check data connection state
297 connstat=$(uqmi -s -d "$device" --set-client-id wds,"$cid_4" --get-data-status)
298 [ "$connstat" == '"connected"' ] || {
299 echo "No data link!"
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
302 return 1
303 }
304 }
305
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
311 return 1
312 fi
313
314 uqmi -s -d "$device" --set-client-id wds,"$cid_6" --set-ip-family ipv6 > /dev/null 2>&1
315
316 : "${v6apn:=${apn}}"
317 : "${v6profile:=${profile}}"
318
319 pdh_6=$(uqmi -s -d "$device" --set-client-id wds,"$cid_6" \
320 --start-network \
321 ${v6apn:+--apn $v6apn} \
322 ${v6profile:+--profile $v6profile} \
323 ${auth:+--auth-type $auth} \
324 ${username:+--username $username} \
325 ${password:+--password $password} \
326 ${autoconnect:+--autoconnect})
327
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
333 return 1
334 fi
335
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"' ] || {
339 echo "No data link!"
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
342 return 1
343 }
344 }
345
346 echo "Setting up $ifname"
347 proto_init_update "$ifname" 1
348 proto_set_keep 1
349 proto_add_data
350 [ -n "$pdh_4" ] && {
351 json_add_string "cid_4" "$cid_4"
352 json_add_string "pdh_4" "$pdh_4"
353 }
354 [ -n "$pdh_6" ] && {
355 json_add_string "cid_6" "$cid_6"
356 json_add_string "pdh_6" "$pdh_6"
357 }
358 proto_close_data
359 proto_send_update "$interface"
360
361 local zone="$(fw3 -q network "$interface" 2>/dev/null)"
362
363 [ -n "$pdh_6" ] && {
364 if [ -z "$dhcpv6" -o "$dhcpv6" = 0 ]; then
365 json_load "$(uqmi -s -d $device --set-client-id wds,$cid_6 --get-current-settings)"
366 json_select ipv6
367 json_get_var ip_6 ip
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
372
373 proto_init_update "$ifname" 1
374 proto_set_keep 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"
382 }
383 [ -n "$zone" ] && {
384 proto_add_data
385 json_add_string zone "$zone"
386 proto_close_data
387 }
388 proto_send_update "$interface"
389 else
390 json_init
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"
400 json_close_object
401 ubus call network add_dynamic "$(json_dump)"
402 fi
403 }
404
405 [ -n "$pdh_4" ] && {
406 if [ "$dhcp" = 0 ]; then
407 json_load "$(uqmi -s -d $device --set-client-id wds,$cid_4 --get-current-settings)"
408 json_select ipv4
409 json_get_var ip_4 ip
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
414
415 proto_init_update "$ifname" 1
416 proto_set_keep 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"
423 }
424 [ -n "$zone" ] && {
425 proto_add_data
426 json_add_string zone "$zone"
427 proto_close_data
428 }
429 proto_send_update "$interface"
430 else
431 json_init
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"
438 json_close_object
439 ubus call network add_dynamic "$(json_dump)"
440 fi
441 }
442 }
443
444 qmi_wds_stop() {
445 local cid="$1"
446 local pdh="$2"
447
448 [ -n "$cid" ] || return
449
450 uqmi -s -d "$device" --set-client-id wds,"$cid" \
451 --stop-network 0xffffffff \
452 --autoconnect > /dev/null 2>&1
453
454 [ -n "$pdh" ] && {
455 uqmi -s -d "$device" --set-client-id wds,"$cid" \
456 --stop-network "$pdh" > /dev/null 2>&1
457 }
458
459 uqmi -s -d "$device" --set-client-id wds,"$cid" \
460 --release-client-id wds > /dev/null 2>&1
461 }
462
463 proto_qmi_teardown() {
464 local interface="$1"
465
466 local device cid_4 pdh_4 cid_6 pdh_6
467 json_get_vars device
468
469 [ -n "$ctl_device" ] && device=$ctl_device
470
471 echo "Stopping network $interface"
472
473 json_load "$(ubus call network.interface.$interface status)"
474 json_select data
475 json_get_vars cid_4 pdh_4 cid_6 pdh_6
476
477 qmi_wds_stop "$cid_4" "$pdh_4"
478 qmi_wds_stop "$cid_6" "$pdh_6"
479
480 proto_init_update "*" 0
481 proto_send_update "$interface"
482 }
483
484 [ -n "$INCLUDE_ONLY" ] || {
485 add_protocol qmi
486 }