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