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