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