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