f4432b4fa33676e0bacc7e7e6e7a0244cc11c37d
[openwrt/openwrt.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 return 1
251 done
252
253 [ -n "$modes" ] && uqmi -s -d "$device" --set-network-modes "$modes" > /dev/null 2>&1
254
255 echo "Starting network $interface"
256
257 pdptype="$(echo "$pdptype" | awk '{print tolower($0)}')"
258
259 [ "$pdptype" = "ip" -o "$pdptype" = "ipv6" -o "$pdptype" = "ipv4v6" ] || pdptype="ip"
260
261 if [ "$pdptype" = "ip" ]; then
262 [ -z "$autoconnect" ] && autoconnect=1
263 [ "$autoconnect" = 0 ] && autoconnect=""
264 else
265 [ "$autoconnect" = 1 ] || autoconnect=""
266 fi
267
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
273 return 1
274 fi
275
276 uqmi -s -d "$device" --set-client-id wds,"$cid_4" --set-ip-family ipv4 > /dev/null 2>&1
277
278 pdh_4=$(uqmi -s -d "$device" --set-client-id wds,"$cid_4" \
279 --start-network \
280 ${apn:+--apn $apn} \
281 ${profile:+--profile $profile} \
282 ${auth:+--auth-type $auth} \
283 ${username:+--username $username} \
284 ${password:+--password $password} \
285 ${autoconnect:+--autoconnect})
286
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
292 return 1
293 fi
294
295 # Check data connection state
296 connstat=$(uqmi -s -d "$device" --set-client-id wds,"$cid_4" --get-data-status)
297 [ "$connstat" == '"connected"' ] || {
298 echo "No data link!"
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
301 return 1
302 }
303 }
304
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
310 return 1
311 fi
312
313 uqmi -s -d "$device" --set-client-id wds,"$cid_6" --set-ip-family ipv6 > /dev/null 2>&1
314
315 : "${v6apn:=${apn}}"
316 : "${v6profile:=${profile}}"
317
318 pdh_6=$(uqmi -s -d "$device" --set-client-id wds,"$cid_6" \
319 --start-network \
320 ${v6apn:+--apn $v6apn} \
321 ${v6profile:+--profile $v6profile} \
322 ${auth:+--auth-type $auth} \
323 ${username:+--username $username} \
324 ${password:+--password $password} \
325 ${autoconnect:+--autoconnect})
326
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
332 return 1
333 fi
334
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"' ] || {
338 echo "No data link!"
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
341 return 1
342 }
343 }
344
345 echo "Setting up $ifname"
346 proto_init_update "$ifname" 1
347 proto_set_keep 1
348 proto_add_data
349 [ -n "$pdh_4" ] && {
350 json_add_string "cid_4" "$cid_4"
351 json_add_string "pdh_4" "$pdh_4"
352 }
353 [ -n "$pdh_6" ] && {
354 json_add_string "cid_6" "$cid_6"
355 json_add_string "pdh_6" "$pdh_6"
356 }
357 proto_close_data
358 proto_send_update "$interface"
359
360 local zone="$(fw3 -q network "$interface" 2>/dev/null)"
361
362 [ -n "$pdh_6" ] && {
363 if [ -z "$dhcpv6" -o "$dhcpv6" = 0 ]; then
364 json_load "$(uqmi -s -d $device --set-client-id wds,$cid_6 --get-current-settings)"
365 json_select ipv6
366 json_get_var ip_6 ip
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
371
372 proto_init_update "$ifname" 1
373 proto_set_keep 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"
381 }
382 [ -n "$zone" ] && {
383 proto_add_data
384 json_add_string zone "$zone"
385 proto_close_data
386 }
387 proto_send_update "$interface"
388 else
389 json_init
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"
399 json_close_object
400 ubus call network add_dynamic "$(json_dump)"
401 fi
402 }
403
404 [ -n "$pdh_4" ] && {
405 if [ "$dhcp" = 0 ]; then
406 json_load "$(uqmi -s -d $device --set-client-id wds,$cid_4 --get-current-settings)"
407 json_select ipv4
408 json_get_var ip_4 ip
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
413
414 proto_init_update "$ifname" 1
415 proto_set_keep 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"
422 }
423 [ -n "$zone" ] && {
424 proto_add_data
425 json_add_string zone "$zone"
426 proto_close_data
427 }
428 proto_send_update "$interface"
429 else
430 json_init
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"
437 json_close_object
438 ubus call network add_dynamic "$(json_dump)"
439 fi
440 }
441 }
442
443 qmi_wds_stop() {
444 local cid="$1"
445 local pdh="$2"
446
447 [ -n "$cid" ] || return
448
449 uqmi -s -d "$device" --set-client-id wds,"$cid" \
450 --stop-network 0xffffffff \
451 --autoconnect > /dev/null 2>&1
452
453 [ -n "$pdh" ] && {
454 uqmi -s -d "$device" --set-client-id wds,"$cid" \
455 --stop-network "$pdh" > /dev/null 2>&1
456 }
457
458 uqmi -s -d "$device" --set-client-id wds,"$cid" \
459 --release-client-id wds > /dev/null 2>&1
460 }
461
462 proto_qmi_teardown() {
463 local interface="$1"
464
465 local device cid_4 pdh_4 cid_6 pdh_6
466 json_get_vars device
467
468 [ -n "$ctl_device" ] && device=$ctl_device
469
470 echo "Stopping network $interface"
471
472 json_load "$(ubus call network.interface.$interface status)"
473 json_select data
474 json_get_vars cid_4 pdh_4 cid_6 pdh_6
475
476 qmi_wds_stop "$cid_4" "$pdh_4"
477 qmi_wds_stop "$cid_6" "$pdh_6"
478
479 proto_init_update "*" 0
480 proto_send_update "$interface"
481 }
482
483 [ -n "$INCLUDE_ONLY" ] || {
484 add_protocol qmi
485 }