Debian Codegraph @

utils/mbim-network.in

raw
1 #!/bin/sh
2
3 # SPDX-License-Identifier: GPL-2.0-or-later
4 #
5 # Copyright (C) 2013-2016 Aleksander Morgado <[email protected]>
6 #
7 # Based on libqmi's qmi-network script
8 #
9
10 print_usage ()
11 {
12 echo "usage: $0 [OPTIONS] [DEVICE] [COMMAND]"
13 }
14
15 help ()
16 {
17 echo "Usage: mbim-network [OPTIONS] [DEVICE] [COMMAND]"
18 echo
19 echo "Simple network management of MBIM devices"
20 echo
21 echo "Commands:"
22 echo " start Start network connection"
23 echo " stop Stop network connection"
24 echo " status Query network connection status"
25 echo
26 echo "Options:"
27 echo " --profile=[PATH] Use the profile in the specified path"
28 echo " --help, -h Show help options"
29 echo " --version Show version"
30 echo
31 echo "Notes:"
32 echo
33 echo " 1) [DEVICE] is given as the full path to the cdc-wdm character"
34 echo " device, e.g.:"
35 echo " /dev/cdc-wdm0"
36 echo
37 echo " 2) The mbim-network script requires a profile to work. Unless"
38 echo " explicitly specified with \`--profile', the file is assumed to"
39 echo " be available in the following path:"
40 echo " /etc/mbim-network.conf"
41 echo
42 echo " 3) The APN to use should be configured in the profile, in the"
43 echo " following way (e.g. assuming APN is called 'internet'):"
44 echo " APN=internet"
45 echo
46 echo " 4) Optional APN user/password strings may be given in the"
47 echo " following way:"
48 echo " APN_USER=user"
49 echo " APN_PASS=password"
50 echo
51 echo " 5) If APN user/password is specified, the authentication protocol"
52 echo " to use (one of PAP, CHAP or MSCHAPV2) must also be specified in"
53 echo " the following way:"
54 echo " APN_AUTH=protocol"
55 echo
56 echo " 6) If you want to instruct the mbim-network script to use the"
57 echo " mbim-proxy setup, you can do so by configuring the following line"
58 echo " in the profile:"
59 echo " PROXY=yes"
60 echo
61 echo " 7) Once the mbim-network script reports a successful connection"
62 echo " you still need to run a DHCP client on the associated WWAN network"
63 echo " interface."
64 echo
65 }
66
67 version ()
68 {
69 echo "mbim-network @VERSION@"
70 echo "Copyright (C) 2013-2021 Aleksander Morgado"
71 echo "License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl-2.0.html>"
72 echo "This is free software: you are free to change and redistribute it."
73 echo "There is NO WARRANTY, to the extent permitted by law."
74 echo
75 }
76
77 # Basic options
78 if [ $# -lt 2 ]; then
79 if [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
80 help
81 exit 0
82 elif [ "$1" = "--version" ]; then
83 version
84 exit 0
85 fi
86
87 echo "error: missing arguments" 1>&2
88 print_usage
89 exit 255
90 fi
91
92 # Defaults
93 PROFILE_FILE=/etc/mbim-network.conf
94
95 # Device + Command with options; options given first
96 while [ $# -gt 2 ]; do
97 OPT="$1"
98 shift
99
100 case "$OPT" in
101 "--")
102 break 2;;
103 "--profile")
104 if [ $# -gt 2 ]; then
105 PROFILE_FILE="$1"
106 shift
107 else
108 PROFILE_FILE=""
109 fi
110 ;;
111 "--profile="*)
112 PROFILE_FILE="${OPT#*=}";;
113 *)
114 echo >&2 "Invalid option: $OPT"
115 print_usage
116 exit 255;;
117 esac
118 done
119
120 if [ -z "$PROFILE_FILE" ]; then
121 echo "error: empty profile path given" 1>&2
122 print_usage
123 exit 255
124 fi
125
126 if [ $# -ne 2 ] || [ "$1" = "--*" ] || [ "$2" = "--*" ]; then
127 echo "error: missing arguments" 1>&2
128 print_usage
129 exit 255
130 fi
131
132 DEVICE=$1
133 COMMAND=$2
134
135 STATE_FILE=/tmp/mbim-network-state-`basename $DEVICE`
136
137 load_profile ()
138 {
139 if [ -f "$PROFILE_FILE" ]; then
140 echo "Loading profile at ${PROFILE_FILE}..."
141 . $PROFILE_FILE
142
143 if [ -n "$APN" ]; then
144 echo " APN: $APN"
145 else
146 echo " APN: unset"
147 fi
148
149 if [ -n "$APN_AUTH" ]; then
150 echo " APN auth protocol: $APN_AUTH"
151 else
152 echo " APN auth protocol: unset"
153 fi
154
155 if [ -n "$APN_USER" ]; then
156 echo " APN user: $APN_USER"
157 if [ -z "$APN_AUTH" ]; then
158 echo "error: APN_USER in the profile requires APN_AUTH" 1>&2
159 exit 32
160 fi
161 else
162 echo " APN user: unset"
163 fi
164
165 if [ -n "$APN_PASS" ]; then
166 echo " APN password: $APN_PASS"
167 if [ -z "$APN_USER" ]; then
168 echo "error: APN_PASS in the profile requires APN_USER" 1>&2
169 exit 32
170 fi
171 else
172 echo " APN password: unset"
173 fi
174
175 if [ "$PROXY" = "yes" ]; then
176 echo " mbim-proxy: $PROXY"
177 PROXY_OPT='--device-open-proxy'
178 else
179 echo " mbim-proxy: no"
180 fi
181 else
182 echo "Profile at '$PROFILE_FILE' not found..."
183 fi
184 }
185
186 save_state ()
187 {
188 KEY=$1
189 VAL=$2
190
191 if [ -n "${PROXY_OPT}" ]; then
192 return
193 fi
194
195 echo "Saving state at ${STATE_FILE}... ($KEY: $VAL)"
196
197 if [ -f "$STATE_FILE" ]; then
198 PREVIOUS=`cat $STATE_FILE`
199 PREVIOUS=`echo "$PREVIOUS" | grep -v $KEY`
200 if [ -n "$PREVIOUS" ]; then
201 echo $PREVIOUS > $STATE_FILE
202 else
203 rm $STATE_FILE
204 fi
205 fi
206
207 if [ -n "$VAL" ]; then
208 echo "$KEY=\"$VAL\"" >> $STATE_FILE
209 fi
210 }
211
212 load_state ()
213 {
214 if [ -n "${PROXY_OPT}" ]; then
215 return
216 fi
217
218 if [ -f "$STATE_FILE" ]; then
219 echo "Loading previous state from ${STATE_FILE}..."
220 . $STATE_FILE
221 if [ -n "$TRID" ]; then
222 echo " Previous Transaction ID: $TRID"
223 fi
224 fi
225 }
226
227 clear_state ()
228 {
229 if [ -n "${PROXY_OPT}" ]; then
230 return
231 fi
232
233 echo "Clearing state at ${STATE_FILE}..."
234 rm -f $STATE_FILE
235 }
236
237 #
238 # $ sudo mbimcli -d /dev/cdc-wdm0 --connect="Internet" --no-close
239 # [/dev/cdc-wdm0] Successfully connected
240 # [/dev/cdc-wdm0] Connection status:
241 # Session ID: '0'
242 # Activation state: 'activated'
243 # Voice call state: 'none'
244 # IP type: 'ipv4'
245 # Context type: 'internet'
246 # Network error: 'unknown'
247 #
248 connect ()
249 {
250 # Always try to connect using a fresh session
251 if [ -z "${PROXY_OPT}" ]; then
252 if [ -n "$TRID" ]; then
253 # This will implicitly close the previous session
254 mbimcli -d $DEVICE --no-open=$TRID
255 clear_state
256 fi
257 fi
258
259 if [ -z "${PROXY_OPT}" ]; then
260 EXTRA_OPT="--no-close"
261 else
262 EXTRA_OPT="${PROXY_OPT}"
263 fi
264
265 SUBSCRIBER_READY_CMD="mbimcli -d $DEVICE --query-subscriber-ready-status ${EXTRA_OPT}"
266 echo "Querying subscriber ready status '$SUBSCRIBER_READY_CMD'..."
267
268 SUBSCRIBER_READY_OUT=`$SUBSCRIBER_READY_CMD`
269 echo $SUBSCRIBER_READY_OUT
270
271 if [ -z "${PROXY_OPT}" ]; then
272 TRID=`echo "$SUBSCRIBER_READY_OUT" | sed -n "s/.*TRID.*'\(.*\)'.*/\1/p"`
273 EXTRA_OPT="--no-open=$TRID --no-close"
274 else
275 EXTRA_OPT="${PROXY_OPT}"
276 fi
277
278 REGISTRATION_STATE_CMD="mbimcli -d $DEVICE --query-registration-state ${EXTRA_OPT}"
279 echo "Querying registration state '$REGISTRATION_STATE_CMD'..."
280
281 REGISTRATION_STATE_OUT=`$REGISTRATION_STATE_CMD`
282 echo $REGISTRATION_STATE_OUT
283
284 if [ -z "${PROXY_OPT}" ]; then
285 TRID=`echo "$REGISTRATION_STATE_OUT" | sed -n "s/.*TRID.*'\(.*\)'.*/\1/p"`
286 EXTRA_OPT="--no-open=$TRID --no-close"
287 else
288 EXTRA_OPT="${PROXY_OPT}"
289 fi
290
291 ATTACH_CMD="mbimcli -d $DEVICE --attach-packet-service ${EXTRA_OPT}"
292 echo "Attaching to packet service with '$ATTACH_CMD'..."
293
294 ATTACH_OUT=`$ATTACH_CMD`
295 echo $ATTACH_OUT
296
297 if [ -z "${PROXY_OPT}" ]; then
298 TRID=`echo "$ATTACH_OUT" | sed -n "s/.*TRID.*'\(.*\)'.*/\1/p"`
299 EXTRA_OPT="--no-open=$TRID --no-close"
300 else
301 EXTRA_OPT="${PROXY_OPT}"
302 fi
303
304 CONNECT_ARGS="apn='$APN'"
305 if [ -n "$APN_AUTH" ]; then
306 CONNECT_ARGS="${CONNECT_ARGS},auth='$APN_AUTH'"
307 if [ -n "$APN_USER" ]; then
308 CONNECT_ARGS="${CONNECT_ARGS},username='$APN_USER'"
309 if [ -n "$APN_PASS" ]; then
310 CONNECT_ARGS="${CONNECT_ARGS},password='$APN_PASS'"
311 fi
312 fi
313 fi
314
315 CONNECT_CMD="mbimcli -d $DEVICE --connect=$CONNECT_ARGS ${EXTRA_OPT}"
316 echo "Starting network with '$CONNECT_CMD'..."
317
318 CONNECT_OUT=`$CONNECT_CMD`
319 if [ $? -eq 0 ]; then
320 echo "Network started successfully"
321 else
322 echo "Network start failed"
323 echo $CONNECT_OUT
324 fi
325
326 # Save the new TRID
327 if [ -z "${PROXY_OPT}" ]; then
328 TRID=`echo "$CONNECT_OUT" | sed -n "s/.*TRID.*'\(.*\)'.*/\1/p"`
329 if [ -n "$TRID" ]; then
330 save_state "TRID" $TRID
331 fi
332 fi
333 }
334
335 #
336 # $ sudo mbimcli -d /dev/cdc-wdm0 --disconnect="0"
337 # [/dev/cdc-wdm0] Successfully disconnected
338 # [/dev/cdc-wdm0] Connection status:
339 # Session ID: '0'
340 # Activation state: 'deactivated'
341 # Voice call state: 'none'
342 # IP type: 'default'
343 # Context type: 'internet'
344 # Network error: 'unknown'
345 #
346 disconnect ()
347 {
348 # Always close the session when disconnecting
349 if [ -n "${PROXY_OPT}" ]; then
350 EXTRA_OPT="${PROXY_OPT}"
351 elif [ -n "$TRID" ]; then
352 EXTRA_OPT="--no-open=$TRID"
353 else
354 EXTRA_OPT=""
355 fi
356 DISCONNECT_CMD="mbimcli -d $DEVICE --disconnect ${EXTRA_OPT}"
357
358 echo "Stopping network with '$DISCONNECT_CMD'..."
359
360 DISCONNECT_OUT=`$DISCONNECT_CMD`
361 if [ $? -eq 0 ]; then
362 echo "Network stopped successfully"
363 else
364 echo "Network stop failed"
365 echo $DISCONNECT_OUT
366 fi
367
368 clear_state
369 }
370
371 #
372 # $ sudo mbimcli -d /dev/cdc-wdm0 --query-connection-state --no-close
373 # [/dev/cdc-wdm0] Connection status:
374 # Session ID: '0'
375 # Activation state: 'deactivated'
376 # Voice call state: 'none'
377 # IP type: 'default'
378 # Context type: 'none'
379 # Network error: 'unknown'
380 #
381 status ()
382 {
383 if [ -n "${PROXY_OPT}" ]; then
384 EXTRA_OPT="${PROXY_OPT}"
385 elif [ -n "$TRID" ]; then
386 EXTRA_OPT="--no-open=$TRID --no-close"
387 else
388 EXTRA_OPT=""
389 fi
390
391 STATUS_CMD="mbimcli -d $DEVICE --query-connection-state ${EXTRA_OPT}"
392 echo "Getting status with '$STATUS_CMD'..."
393
394 STATUS_OUT=`$STATUS_CMD`
395
396 # Save the new TRID
397 if [ -z "${PROXY_OPT}" ]; then
398 TRID=`echo "$STATUS_OUT" | sed -n "s/.*TRID.*'\(.*\)'.*/\1/p"`
399 if [ -n "$TRID" ]; then
400 save_state "TRID" $TRID
401 fi
402 fi
403
404 CONN=`echo "$STATUS_OUT" | sed -n "s/.*Activation state:.*'\(.*\)'.*/\1/p"`
405 if [ -z "$CONN" ]; then
406 echo "error: couldn't get connection status" 1>&2
407 exit 2
408 else
409 echo "Status: $CONN"
410 if [ "$CONN" != "activated" ]; then
411 exit 64
412 fi
413 fi
414 }
415
416 # Main
417
418 # Load profile, if any
419 load_profile
420
421 # Load previous state, if any
422 load_state
423
424 # Process commands
425 case $COMMAND in
426 "start")
427 connect
428 ;;
429 "stop")
430 disconnect
431 ;;
432 "status")
433 status
434 ;;
435 *)
436 echo "error: unexpected command '$COMMAND'" 1>&2
437 print_usage
438 exit 255
439 ;;
440 esac
441
442 exit 0