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