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