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