Debian Codegraph @

utils/qmi-network.in

raw
1 #!/bin/sh
2
3 # This program is free software; you can redistribute it and/or modify it under
4 # the terms of the GNU General Public License as published by the Free Software
5 # Foundation, either version 2 of the License, or (at your option) any later
6 # version.
7 #
8 # This program is distributed in the hope that it will be useful, but WITHOUT
9 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11 # details.
12 #
13 # You should have received a copy of the GNU General Public License along with
14 # this program; if not, write to the Free Software Foundation, Inc., 51
15 # Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 #
17 # Copyright (C) 2012-2017 Aleksander Morgado <[email protected]>
18
19 print_usage ()
20 {
21 echo "usage: $0 [OPTIONS] [DEVICE] [COMMAND]"
22 }
23
24 help ()
25 {
26 echo "Usage: qmi-network [OPTIONS] [DEVICE] [COMMAND]"
27 echo
28 echo "Simple network management of QMI devices"
29 echo
30 echo "Commands:"
31 echo " start Start network connection"
32 echo " stop Stop network connection"
33 echo " status Query network connection status"
34 echo
35 echo "Options:"
36 echo " --profile=[PATH] Use the profile in the specified path"
37 echo " --help Show help options"
38 echo " --version Show version"
39 echo
40 echo "Notes:"
41 echo
42 echo " 1) [DEVICE] is given as the full path to the cdc-wdm character"
43 echo " device, e.g.:"
44 echo " /dev/cdc-wdm0"
45 echo
46 echo " 2) The qmi-network script requires a profile to work. Unless"
47 echo " explicitly specified with \`--profile', the file is assumed to"
48 echo " be available in the following path:"
49 echo " /etc/qmi-network.conf"
50 echo
51 echo " 3) The APN to use should be configured in the profile, in the"
52 echo " following way (e.g. assuming APN is called 'internet'):"
53 echo " APN=internet"
54 echo
55 echo " 4) Optional APN user/password strings may be given in the following"
56 echo " way:"
57 echo " APN_USER=user"
58 echo " APN_PASS=password"
59 echo
60 echo " 5) Optional IP_TYPE strings may be given in the following"
61 echo " way:"
62 echo " IP_TYPE=[4|6]"
63 echo
64 echo " 6) If you want to instruct the qmi-network script to use the"
65 echo " qmi-proxy setup, you can do so by configuring the following line"
66 echo " in the profile:"
67 echo " PROXY=yes"
68 echo
69 echo " 7) To instruct the qmi-network script to use an already existing"
70 echo " Profile ID, or to create a new one on the fly, use the following"
71 echo " configuration in the profile:"
72 echo " PROFILE=number # to use the profile-id number"
73 echo " PROFILE=auto # to create a temporary profile on the fly"
74 echo
75 echo " 8) Once the qmi-network script reports a successful connection"
76 echo " you still need to run a DHCP client on the associated WWAN network"
77 echo " interface."
78 echo
79 }
80
81 version ()
82 {
83 echo "qmi-network @VERSION@"
84 echo "Copyright (C) 2013-2021 Aleksander Morgado"
85 echo "License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl-2.0.html>"
86 echo "This is free software: you are free to change and redistribute it."
87 echo "There is NO WARRANTY, to the extent permitted by law."
88 echo
89 }
90
91 # Basic options
92 if [ $# -lt 2 ]; then
93 if [ "$1" = "--help" ]; then
94 help
95 exit 0
96 elif [ "$1" = "--version" ]; then
97 version
98 exit 0
99 fi
100
101 echo "error: missing arguments" 1>&2
102 print_usage
103 exit 255
104 fi
105
106 # Defaults
107 PROFILE_FILE=/etc/qmi-network.conf
108
109 # Device + Command with options; options given first
110 while [ $# -gt 2 ]; do
111 OPT="$1"
112 shift
113
114 case "$OPT" in
115 "--")
116 break 2;;
117 "--profile")
118 if [ $# -gt 2 ]; then
119 PROFILE_FILE="$1"
120 shift
121 else
122 PROFILE_FILE=""
123 fi
124 ;;
125 "--profile="*)
126 PROFILE_FILE="${OPT#*=}";;
127 *)
128 echo >&2 "Invalid option: $OPT"
129 print_usage
130 exit 255;;
131 esac
132 done
133
134 if [ -z "$PROFILE_FILE" ]; then
135 echo "error: empty profile path given" 1>&2
136 print_usage
137 exit 255
138 fi
139
140 if [ $# -ne 2 ] || [ "$1" = '--*' ] || [ "$2" = '--*' ]; then
141 echo "error: missing arguments" 1>&2
142 print_usage
143 exit 255
144 fi
145
146 DEVICE=$1
147 COMMAND=$2
148
149 STATE_FILE=/tmp/qmi-network-state-`basename $DEVICE`
150
151 load_profile ()
152 {
153 if [ -f "$PROFILE_FILE" ]; then
154 echo "Loading profile at ${PROFILE_FILE}..."
155 . $PROFILE_FILE
156
157 if [ -n "$APN" ]; then
158 echo " APN: $APN"
159 else
160 echo " APN: unset"
161 fi
162
163 if [ -n "$APN_USER" ]; then
164 echo " APN user: $APN_USER"
165 else
166 echo " APN user: unset"
167 fi
168
169 if [ -n "$APN_PASS" ]; then
170 echo " APN password: $APN_PASS"
171 else
172 echo " APN password: unset"
173 fi
174
175 if [ "$PROXY" = "yes" ]; then
176 echo " qmi-proxy: $PROXY"
177 PROXY_OPT='--device-open-proxy'
178 else
179 echo " qmi-proxy: no"
180 fi
181
182 if [ -n "$IP_TYPE" ]; then
183 echo " IP_TYPE: $IP_TYPE"
184 else
185 echo " IP_TYPE: unset"
186 fi
187
188 if [ -n "$PROFILE" ]; then
189 echo " PROFILE: $PROFILE"
190 else
191 echo " PROFILE: unset"
192 fi
193
194 else
195 echo "Profile at '$PROFILE_FILE' not found..."
196 fi
197 }
198
199 save_state ()
200 {
201 KEY=$1
202 VAL=$2
203
204 echo "Saving state at ${STATE_FILE}... ($KEY: $VAL)"
205
206 if [ -f "$STATE_FILE" ]; then
207 PREVIOUS=`cat $STATE_FILE`
208 PREVIOUS=`echo "$PREVIOUS" | grep -v $KEY`
209 if [ -n "$PREVIOUS" ]; then
210 echo $PREVIOUS > $STATE_FILE
211 else
212 rm $STATE_FILE
213 fi
214 fi
215
216 if [ -n "$VAL" ]; then
217 echo "$KEY=\"$VAL\"" >> $STATE_FILE
218 fi
219 }
220
221 load_state ()
222 {
223 if [ -f "$STATE_FILE" ]; then
224 echo "Loading previous state from ${STATE_FILE}..."
225 . $STATE_FILE
226
227 if [ -n "$CID" ]; then
228 echo " Previous CID: $CID"
229 fi
230 if [ -n "$PDH" ]; then
231 echo " Previous PDH: $PDH"
232 fi
233 fi
234 }
235
236 clear_state ()
237 {
238 echo "Clearing state at ${STATE_FILE}..."
239 rm -f $STATE_FILE
240 }
241
242 setup_data_format ()
243 {
244 RUN_WDA=0
245
246 # Read link layer protocol setup in the device
247
248 DEVICE_DATA_FORMAT_CMD="qmicli -d $DEVICE --wda-get-data-format $PROXY_OPT"
249 echo "Checking data format with '$DEVICE_DATA_FORMAT_CMD'..."
250 if [ -n "$QMIDEBUG" ]; then
251 DEVICE_DATA_FORMAT_OUT="\
252 [/dev/cdc-wdm1] Successfully got data format
253 QoS flow header: no
254 Link layer protocol: '802-3'
255 Uplink data aggregation protocol: 'disabled'
256 Downlink data aggregation protocol: 'disabled'
257 NDP signature: '0'
258 Uplink data aggregation max size: '0'
259 Downlink data aggregation max size: '0'"
260 else
261 DEVICE_DATA_FORMAT_OUT=`$DEVICE_DATA_FORMAT_CMD`
262 fi
263 DEVICE_LLP=`echo "$DEVICE_DATA_FORMAT_OUT" | sed -n "s/.*Link layer protocol:.*'\(.*\)'.*/\1/p"`
264
265 if [ -z "$DEVICE_LLP" ]; then
266 echo "Device link layer protocol not retrieved: WDA unsupported" 1>&2
267 return
268 fi
269
270 if [ "$DEVICE_LLP" != "802-3" -a "$DEVICE_LLP" != "raw-ip" ]; then
271 echo "Device link layer protocol not retrieved: unexpected value reported: '$DEVICE_LLP'" 1>&2
272 return
273 fi
274 echo "Device link layer protocol retrieved: $DEVICE_LLP"
275
276 # Read link layer protocol setup in the kernel
277
278 EXPECTED_DATA_FORMAT_CMD="qmicli -d $DEVICE --get-expected-data-format"
279 echo "Getting expected data format with '$EXPECTED_DATA_FORMAT_CMD'..."
280 if [ -n "$QMIDEBUG" ]; then
281 EXPECTED_LLP="raw-ip"
282 else
283 EXPECTED_DATA_FORMAT_OUT=`$EXPECTED_DATA_FORMAT_CMD`
284 if [ $? -eq 0 ]; then
285 EXPECTED_LLP=$EXPECTED_DATA_FORMAT_OUT
286 echo "Expected link layer protocol retrieved: $EXPECTED_LLP"
287 else
288 echo "Expected link layer protocol not retrieved: kernel unsupported" 1>&2
289 EXPECTED_LLP="802-3"
290 # The kernel doesn't support expected data format, so we change the
291 # device data format instead
292 RUN_WDA=1
293 fi
294 fi
295
296 if [ "$EXPECTED_LLP" != "802-3" -a "$EXPECTED_LLP" != "raw-ip" ]; then
297 echo "Expected link layer protocol not retrieved: unexpected value reported: '$EXPECTED_LLP'" 1>&2
298 return
299 fi
300
301 if [ "$DEVICE_LLP" = "$EXPECTED_LLP" ]; then
302 echo "Device and kernel link layer protocol match: $DEVICE_LLP"
303 return
304 fi
305
306 if [ $RUN_WDA -eq 1 ]; then
307 DEVICE_DATA_FORMAT_SET_CMD="qmicli -d $DEVICE --wda-set-data-format=$EXPECTED_LLP $PROXY_OPT"
308 echo "Updating device link layer protocol with '$DEVICE_DATA_FORMAT_SET_CMD'..."
309 if [ -n "$QMIDEBUG" ]; then
310 DEVICE_DATA_FORMAT_SET_OUT="\
311 [/dev/cdc-wdm1] Successfully set data format
312 QoS flow header: no
313 Link layer protocol: '802-3'
314 Uplink data aggregation protocol: 'disabled'
315 Downlink data aggregation protocol: 'disabled'
316 NDP signature: '0'
317 Downlink data aggregation max datagrams: '0'
318 Downlink data aggregation max size: '0'"
319 else
320 DEVICE_DATA_FORMAT_SET_OUT=`$DEVICE_DATA_FORMAT_SET_CMD`
321 fi
322
323 LLP=`echo "$DEVICE_DATA_FORMAT_SET_OUT" | sed -n "s/.*Link layer protocol:.*'\(.*\)'.*/\1/p"`
324 if [ -z "$LLP" ]; then
325 echo "Error updating Device link layer protocol" 1>&2
326 else
327 echo "New device link layer protocol retrieved: $LLP"
328 fi
329 else
330 EXPECTED_DATA_FORMAT_SET_CMD="qmicli -d $DEVICE --set-expected-data-format=$DEVICE_LLP"
331 echo "Updating kernel link layer protocol with '$EXPECTED_DATA_FORMAT_SET_CMD'..."
332 EXPECTED_DATA_FORMAT_SET_OUT=`$EXPECTED_DATA_FORMAT_SET_CMD`
333 if [ $? -eq 0 ]; then
334 echo "Kernel link layer protocol updated"
335 else
336 echo "Error updating kernel link layer protocol " 1>&2
337 fi
338 fi
339 }
340
341 # qmicli -d /dev/cdc-wdm0 --wds-create-profile --client-no-release-cid
342 create_profile()
343 {
344 if [ -n "$CID" ]; then
345 USE_PREVIOUS_CID="--client-cid=$CID"
346 fi
347
348 CREATE_PROFILE_ARGS="3gpp"
349 if [ -n "$APN" ]; then
350 CREATE_PROFILE_ARGS="${CREATE_PROFILE_ARGS},apn='$APN'"
351 fi
352
353 if [ -n "$IP_TYPE" ]; then
354 if [ "$IP_TYPE" -eq 4 ]; then
355 CREATE_PROFILE_ARGS="${CREATE_PROFILE_ARGS},pdp-type='IP'"
356 fi
357 if [ "$IP_TYPE" -eq 6 ]; then
358 CREATE_PROFILE_ARGS="${CREATE_PROFILE_ARGS},pdp-type='IPV6'"
359 fi
360 fi
361
362 if [ -n "$APN_USER" ]; then
363 CREATE_PROFILE_ARGS="${CREATE_PROFILE_ARGS},username='$APN_USER'"
364 if [ -n "$APN_PASS" ]; then
365 CREATE_PROFILE_ARGS="${CREATE_PROFILE_ARGS},password='$APN_PASS'"
366 fi
367 fi
368
369 CREATE_PROFILE_CMD="qmicli -d $DEVICE --wds-create-profile=$CREATE_PROFILE_ARGS $USE_PREVIOUS_CID --client-no-release-cid $PROXY_OPT"
370
371 echo "Creating profile with '$CREATE_PROFILE_CMD'..."
372 CREATE_PROFILE_OUT=`$CREATE_PROFILE_CMD`
373
374 # Save the profile ID
375 PROFILE_ID=`echo "$CREATE_PROFILE_OUT" | sed -n "s/.*Profile index:\s*'\([0-9]*\)'.*/\1/p"`
376 if [ -z "$PROFILE_ID" ]; then
377 echo "Profile ID creation failed"
378 echo $CREATE_PROFILE_OUT
379 exit 255
380 fi
381 }
382
383 delete_profile()
384 {
385 if [ -n "$PROFILE" -a "$PROFILE" = "auto" ]; then
386 if [ -n "$CID" ]; then
387 USE_PREVIOUS_CID="--client-cid=$CID"
388 fi
389
390 DELETE_PROFILE_CMD="qmicli -d $DEVICE --wds-delete-profile=3gpp,$PROFILE_ID $USE_PREVIOUS_CID --client-no-release-cid $PROXY_OPT"
391
392 echo "Deleting profile with '$DELETE_PROFILE_CMD'..."
393 $DELETE_PROFILE_CMD
394 fi
395 }
396
397 # qmicli -d /dev/cdc-wdm0 --wds-start-network --client-no-release-cid
398 # [/dev/cdc-wdm0] Network started
399 # Packet data handle: 3634026241
400 # [/dev/cdc-wdm0] Client ID not released:
401 # Service: 'wds'
402 # CID: '80'
403 start_network ()
404 {
405 if [ -n "$CID" ]; then
406 USE_PREVIOUS_CID="--client-cid=$CID"
407 fi
408
409 if [ -n "$PDH" ]; then
410 echo "error: cannot re-start network, PDH already exists" 1>&2
411 exit 3
412 fi
413
414 setup_data_format
415
416 if [ -n "$PROFILE" ]; then
417 if [ "$PROFILE" = "auto" ]; then
418 create_profile
419 else
420 PROFILE_ID="$PROFILE"
421 fi
422 START_NETWORK_ARGS="3gpp-profile=$PROFILE_ID"
423 fi
424
425 if [ -z "$PROFILE" -a -n "$APN" ]; then
426 START_NETWORK_ARGS="apn='$APN'"
427 if [ -n "$APN_USER" ]; then
428 START_NETWORK_ARGS="${START_NETWORK_ARGS},username='$APN_USER'"
429 if [ -n "$APN_PASS" ]; then
430 START_NETWORK_ARGS="${START_NETWORK_ARGS},password='$APN_PASS'"
431 fi
432 fi
433 if [ -n "$IP_TYPE" ]; then
434 START_NETWORK_ARGS="${START_NETWORK_ARGS},ip-type='$IP_TYPE'"
435 fi
436 fi
437
438 START_NETWORK_CMD="qmicli -d $DEVICE --wds-start-network=$START_NETWORK_ARGS $USE_PREVIOUS_CID --client-no-release-cid $PROXY_OPT"
439 echo "Starting network with '$START_NETWORK_CMD'..."
440
441 if [ -n "$QMIDEBUG" ]; then
442 START_NETWORK_OUT="\
443 [/dev/cdc-wdm0] Network started
444 Packet data handle: '3634026241'
445 [/dev/cdc-wdm0] Client ID not released:
446 Service: 'wds'
447 CID: '80'"
448 else
449 START_NETWORK_OUT=`$START_NETWORK_CMD`
450 fi
451
452 # Save the new CID if we didn't use any before
453 if [ -z "$CID" ]; then
454 CID=`echo "$START_NETWORK_OUT" | sed -n "s/.*CID.*'\(.*\)'.*/\1/p"`
455 if [ -z "$CID" ]; then
456 echo "error: network start failed, client not allocated" 1>&2
457 exit 1
458 else
459 save_state "CID" $CID
460 fi
461 fi
462
463 PDH=`echo "$START_NETWORK_OUT" | sed -n "s/.*handle.*'\(.*\)'.*/\1/p"`
464 if [ -z "$PDH" ]; then
465 echo "error: network start failed, no packet data handle" 1>&2
466 # Cleanup the client
467 qmicli -d "$DEVICE" --wds-noop --client-cid="$CID" $PROXY_OPT
468 clear_state
469 exit 2
470 else
471 save_state "PDH" $PDH
472 fi
473
474 echo "Network started successfully"
475 if [ "$PROFILE" = "auto" ]; then
476 delete_profile
477 fi
478 }
479
480 # qmicli -d /dev/cdc-wdm0 --wds-stop-network
481 stop_network ()
482 {
483 if [ -z "$CID" ]; then
484 echo "Network already stopped"
485 elif [ -z "$PDH" ]; then
486 echo "Network already stopped; need to cleanup CID $CID"
487 # Cleanup the client
488 qmicli -d "$DEVICE" --wds-noop --client-cid="$CID" $PROXY_OPT
489 else
490 STOP_NETWORK_CMD="qmicli -d $DEVICE --wds-stop-network=$PDH --client-cid=$CID $PROXY_OPT"
491 echo "Stopping network with '$STOP_NETWORK_CMD'..."
492
493 if [ -n "$QMIDEBUG" ]; then
494 STOP_NETWORK_OUT="\
495 [/dev/cdc-wdm0] Network stopped
496 "
497 else
498 STOP_NETWORK_OUT=`$STOP_NETWORK_CMD`
499 fi
500
501 echo "Network stopped successfully"
502 fi
503
504 clear_state
505 }
506
507 # qmicli -d /dev/cdc-wdm0 --wds-get-packet-service-status
508 packet_service_status ()
509 {
510 if [ -n "$CID" ]; then
511 USE_PREVIOUS_CID="--client-cid=$CID --client-no-release-cid"
512 fi
513
514 STATUS_CMD="qmicli -d $DEVICE --wds-get-packet-service-status $USE_PREVIOUS_CID $PROXY_OPT"
515 echo "Getting status with '$STATUS_CMD'..."
516
517 if [ -n "$QMIDEBUG" ]; then
518 STATUS_OUT="\
519 [/dev/cdc-wdm0] Connection status: 'disconnected'
520 "
521 else
522 STATUS_OUT=`$STATUS_CMD`
523 fi
524
525 CONN=`echo "$STATUS_OUT" | sed -n "s/.*Connection status:.*'\(.*\)'.*/\1/p"`
526 if [ -z "$CONN" ]; then
527 echo "error: couldn't get packet service status" 1>&2
528 exit 2
529 else
530 echo "Status: $CONN"
531 if [ "$CONN" != "connected" ]; then
532 exit 64
533 fi
534 fi
535 }
536
537 # Main
538
539 # Load profile, if any
540 load_profile
541
542 # Load previous state, if any
543 load_state
544
545 # Process commands
546 case $COMMAND in
547 "start")
548 start_network
549 ;;
550 "stop")
551 stop_network
552 ;;
553 "status")
554 packet_service_status
555 ;;
556 *)
557 echo "error: unexpected command '$COMMAND'" 1>&2
558 print_usage
559 exit 255
560 ;;
561 esac
562
563 exit 0