uqmi: support split-APN IPv4 and IPv6 dual-stack
[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 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 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++
90 sleep 1;
91 else
92 echo "SIM not initialized"
93 proto_notify_error "$interface" SIM_NOT_INITIALIZED
94 proto_block_restart "$interface"
95 return 1
96 fi
97 done
98
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"
106 return 1
107 }
108 }
109 else
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
115 fi
116 json_get_var pin1_verify_tries pin1_verify_tries
117
118 case "$pin1_status" in
119 disabled)
120 echo "PIN verification is disabled"
121 ;;
122 blocked)
123 echo "SIM locked PUK required"
124 proto_notify_error "$interface" PUK_NEEDED
125 proto_block_restart "$interface"
126 return 1
127 ;;
128 not_verified)
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"
133 return 1
134 }
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"
140 return 1
141 }
142 else
143 echo "PIN not specified but required"
144 proto_notify_error "$interface" PIN_NOT_SPECIFIED
145 proto_block_restart "$interface"
146 return 1
147 fi
148 ;;
149 verified)
150 echo "PIN already verified"
151 ;;
152 *)
153 echo "PIN status failed (${pin1_status:-sim_not_present})"
154 proto_notify_error "$interface" PIN_STATUS_FAILED
155 proto_block_restart "$interface"
156 return 1
157 ;;
158 esac
159 json_cleanup
160 fi
161
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 || {
166 mcc=0
167 mnc=0
168 }
169
170 if [ "$plmn" = "0" ]; then
171 if [ "$plmn_mode" != "automatic" ]; then
172 mcc=0
173 mnc=0
174 echo "Setting PLMN to auto"
175 fi
176 elif [ "$mcc" -ne "${plmn:0:3}" -o "$mnc" -ne "${plmn:3}" ]; then
177 mcc=${plmn:0:3}
178 mnc=${plmn:3}
179 echo "Setting PLMN to $plmn"
180 else
181 mcc=""
182 mnc=""
183 fi
184 fi
185
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"
191 return 1
192 }
193 fi
194
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
198
199 # Go online
200 uqmi -s -d "$device" --set-device-operating-mode online > /dev/null 2>&1
201
202 # Set IP format
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)"
206
207 if [ "$dataformat" = '"raw-ip"' ]; then
208
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"
211 return 1
212 }
213
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
216 fi
217
218 uqmi -s -d "$device" --sync > /dev/null 2>&1
219
220 uqmi -s -d "$device" --network-register > /dev/null 2>&1
221
222 echo "Waiting for network registration"
223 sleep 1
224 local registration_timeout=0
225 local registration_state=""
226 while true; do
227 registration_state=$(uqmi -s -d "$device" --get-serving-system 2>/dev/null | jsonfilter -e "@.registration" 2>/dev/null)
228
229 [ "$registration_state" = "registered" ] && break
230
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
236 }
237 let registration_timeout++
238 sleep 1
239 continue
240 fi
241 echo "Network registration failed, registration timeout reached"
242 else
243 # registration_state is 'registration_denied' or 'unknown' or ''
244 echo "Network registration failed (reason: '$registration_state')"
245 fi
246
247 proto_notify_error "$interface" NETWORK_REGISTRATION_FAILED
248 proto_block_restart "$interface"
249 return 1
250 done
251
252 [ -n "$modes" ] && uqmi -s -d "$device" --set-network-modes "$modes" > /dev/null 2>&1
253
254 echo "Starting network $interface"
255
256 pdptype="$(echo "$pdptype" | awk '{print tolower($0)}')"
257
258 [ "$pdptype" = "ip" -o "$pdptype" = "ipv6" -o "$pdptype" = "ipv4v6" ] || pdptype="ip"
259
260 if [ "$pdptype" = "ip" ]; then
261 [ -z "$autoconnect" ] && autoconnect=1
262 [ "$autoconnect" = 0 ] && autoconnect=""
263 else
264 [ "$autoconnect" = 1 ] || autoconnect=""
265 fi
266
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
272 return 1
273 fi
274
275 uqmi -s -d "$device" --set-client-id wds,"$cid_4" --set-ip-family ipv4 > /dev/null 2>&1
276
277 pdh_4=$(uqmi -s -d "$device" --set-client-id wds,"$cid_4" \
278 --start-network \
279 ${apn:+--apn $apn} \
280 ${profile:+--profile $profile} \
281 ${auth:+--auth-type $auth} \
282 ${username:+--username $username} \
283 ${password:+--password $password} \
284 ${autoconnect:+--autoconnect})
285
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
291 return 1
292 fi
293
294 # Check data connection state
295 connstat=$(uqmi -s -d "$device" --set-client-id wds,"$cid_4" --get-data-status)
296 [ "$connstat" == '"connected"' ] || {
297 echo "No data link!"
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
300 return 1
301 }
302 }
303
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
309 return 1
310 fi
311
312 uqmi -s -d "$device" --set-client-id wds,"$cid_6" --set-ip-family ipv6 > /dev/null 2>&1
313
314 : "${v6apn:=${apn}}"
315 : "${v6profile:=${profile}}"
316
317 pdh_6=$(uqmi -s -d "$device" --set-client-id wds,"$cid_6" \
318 --start-network \
319 ${v6apn:+--apn $v6apn} \
320 ${v6profile:+--profile $v6profile} \
321 ${auth:+--auth-type $auth} \
322 ${username:+--username $username} \
323 ${password:+--password $password} \
324 ${autoconnect:+--autoconnect})
325
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
331 return 1
332 fi
333
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"' ] || {
337 echo "No data link!"
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
340 return 1
341 }
342 }
343
344 echo "Setting up $ifname"
345 proto_init_update "$ifname" 1
346 proto_set_keep 1
347 proto_add_data
348 [ -n "$pdh_4" ] && {
349 json_add_string "cid_4" "$cid_4"
350 json_add_string "pdh_4" "$pdh_4"
351 }
352 [ -n "$pdh_6" ] && {
353 json_add_string "cid_6" "$cid_6"
354 json_add_string "pdh_6" "$pdh_6"
355 }
356 proto_close_data
357 proto_send_update "$interface"
358
359 local zone="$(fw3 -q network "$interface" 2>/dev/null)"
360
361 [ -n "$pdh_6" ] && {
362 if [ -z "$dhcpv6" -o "$dhcpv6" = 0 ]; then
363 json_load "$(uqmi -s -d $device --set-client-id wds,$cid_6 --get-current-settings)"
364 json_select ipv6
365 json_get_var ip_6 ip
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
370
371 proto_init_update "$ifname" 1
372 proto_set_keep 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"
380 }
381 [ -n "$zone" ] && {
382 proto_add_data
383 json_add_string zone "$zone"
384 proto_close_data
385 }
386 proto_send_update "$interface"
387 else
388 json_init
389 json_add_string name "${interface}_6"
390 json_add_string ifname "@$interface"
391 json_add_string proto "dhcpv6"
392 [ -n "$ip6table" ] && json_add_string ip6table "$ip6table"
393 proto_add_dynamic_defaults
394 # RFC 7278: Extend an IPv6 /64 Prefix to LAN
395 json_add_string extendprefix 1
396 [ -n "$zone" ] && json_add_string zone "$zone"
397 json_close_object
398 ubus call network add_dynamic "$(json_dump)"
399 fi
400 }
401
402 [ -n "$pdh_4" ] && {
403 if [ "$dhcp" = 0 ]; then
404 json_load "$(uqmi -s -d $device --set-client-id wds,$cid_4 --get-current-settings)"
405 json_select ipv4
406 json_get_var ip_4 ip
407 json_get_var gateway_4 gateway
408 json_get_var dns1_4 dns1
409 json_get_var dns2_4 dns2
410 json_get_var subnet_4 subnet
411
412 proto_init_update "$ifname" 1
413 proto_set_keep 1
414 proto_add_ipv4_address "$ip_4" "$subnet_4"
415 proto_add_ipv4_route "$gateway_4" "128"
416 [ "$defaultroute" = 0 ] || proto_add_ipv4_route "0.0.0.0" 0 "$gateway_4"
417 [ "$peerdns" = 0 ] || {
418 proto_add_dns_server "$dns1_4"
419 proto_add_dns_server "$dns2_4"
420 }
421 [ -n "$zone" ] && {
422 proto_add_data
423 json_add_string zone "$zone"
424 proto_close_data
425 }
426 proto_send_update "$interface"
427 else
428 json_init
429 json_add_string name "${interface}_4"
430 json_add_string ifname "@$interface"
431 json_add_string proto "dhcp"
432 [ -n "$ip4table" ] && json_add_string ip4table "$ip4table"
433 proto_add_dynamic_defaults
434 [ -n "$zone" ] && json_add_string zone "$zone"
435 json_close_object
436 ubus call network add_dynamic "$(json_dump)"
437 fi
438 }
439 }
440
441 qmi_wds_stop() {
442 local cid="$1"
443 local pdh="$2"
444
445 [ -n "$cid" ] || return
446
447 uqmi -s -d "$device" --set-client-id wds,"$cid" \
448 --stop-network 0xffffffff \
449 --autoconnect > /dev/null 2>&1
450
451 [ -n "$pdh" ] && {
452 uqmi -s -d "$device" --set-client-id wds,"$cid" \
453 --stop-network "$pdh" > /dev/null 2>&1
454 }
455
456 uqmi -s -d "$device" --set-client-id wds,"$cid" \
457 --release-client-id wds > /dev/null 2>&1
458 }
459
460 proto_qmi_teardown() {
461 local interface="$1"
462
463 local device cid_4 pdh_4 cid_6 pdh_6
464 json_get_vars device
465
466 [ -n "$ctl_device" ] && device=$ctl_device
467
468 echo "Stopping network $interface"
469
470 json_load "$(ubus call network.interface.$interface status)"
471 json_select data
472 json_get_vars cid_4 pdh_4 cid_6 pdh_6
473
474 qmi_wds_stop "$cid_4" "$pdh_4"
475 qmi_wds_stop "$cid_6" "$pdh_6"
476
477 proto_init_update "*" 0
478 proto_send_update "$interface"
479 }
480
481 [ -n "$INCLUDE_ONLY" ] || {
482 add_protocol qmi
483 }