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