summarylogtreecommitdiffstats
path: root/mobile_mm
blob: 3480c11b72a8b2b0c55fbca063d0ffc9a7015370 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# Contributed by Max Bülte <ff0x@this-is-fine.io>
# Version: 0.1.1
# shellcheck disable=SC2148,SC2016,SC1091,SC2034

# KNOWN BUGS:
# - After suspend/hibernate the mobile broadband connection is not going to reconnect automatically (that's probably ok)
#   and sometimes, the modem is PUK-locked afterwards. The netctl connection is still show as active.
#   To fix this issue just run 'netctl restart <profile>'. This will also unlock the modem if required.

. "$SUBR_DIR/ip"
. "$SUBR_DIR/rfkill"

DEPENDENCIES=('mmcli' 'jq')

function check_deps() {
  local _prog="$1"

  if ! command -v "$_prog" >/dev/null 2>&1; then
    report_error "'$_prog' not found. Check required dependencies."
    return 1
  fi
}

function mobile_mm_up {
  local _mmcli_args=()

  if ! is_interface "$Interface"; then
    report_debug "Interface '$Interface' does not exist. Is this a problem?"
  fi

  if [[ $RFKill ]]; then
    rf_enable "$Interface" "$RFKill" || return 1
  fi

  if [[ -z $AccessPointName ]]; then
    report_error "Option 'AccessPointName' is required"
    return 1
  else
    _mmcli_args+=("apn=${AccessPointName}")
  fi

  [[ $Pin ]] && _mmcli_args+=("pin=${Pin}")

  local _ip_type="ipv4"
  if [[ $IP6 ]] && [[ ! "$IP6" =~ (dhcp|no) ]]; then
    report_notice "Currently only the following options are supported for 'IP6' ('dhcp', 'no'). Continueing with disabled IPv6"
    IP6="no"
  fi
  [[ "$IP6" == 'dhcp' ]] && _ip_type="ipv4v6"
  _mmcli_args+=("ip-type=${_ip_type}")

  if [[ $User ]] && [[ $Password ]]; then
    _mmcli_args+=("user=${User}")
    _mmcli_args+=("password=${Password}")
  fi

  local _roaming=0
  is_yes "${Roaming:-0}" && _roaming=1
  _mmcli_args+=("allow-roaming=${_roaming}")

  _mmcli_args+=("number=${PhoneNumber:-*99#}")

  local _connect_args
  _connect_args=$(printf ",%s" "${_mmcli_args[@]}")
  _connect_args=${_connect_args:1}

  if [[ $Mode ]] && [[ ! "$Mode" =~ ^(ANY|2G|3G|4G)$ ]]; then
    report_notice "Mode must be one of (ANY, 2G, 3G, 4G). Selecting 4G as preferred mode"
    Mode="4G"
  else
    Mode="4G"
  fi

  report_debug "Full connect string: mmcli -m $MODEM --set-allowed-modes=ANY --set-preferred-mode=${Mode} --simple-connect=$_connect_args"

  # Trying to establish the broadband connection until the modem got its bearer (and the bearer_interface) or the timeout is reached
  local _bearer _bearer_interface
  TIMEOUT_CONNECT=${TimeoutConnect:-30}
  _bearer=''; _bearer_interface=''

  until [[ -n "$_bearer_interface" ]] || [ "$TIMEOUT_CONNECT" -eq 0 ]; do
    mmcli -m "$MODEM" --set-allowed-modes=ANY --set-preferred-mode="${Mode}" --simple-connect="$_connect_args"

    if [[ -n "$_bearer" ]]; then
      report_debug "Bearer found on modem"

      until mmcli -J -m "$MODEM" 2>/dev/null | jq -r '.[].generic.bearers[0]' 2>/dev/null || [ "$TIMEOUT_MODEM" -eq 0 ]; do
        sleep 1
        ((TIMEOUT_CONNECT--))
      done

      _bearer_interface="$(mmcli -J -m "$MODEM" -b "$_bearer" 2>/dev/null | jq -r '.[].status.interface' 2>/dev/null)"
    else
      sleep 1
      _bearer="$(mmcli -J -m "$MODEM" 2>/dev/null | jq -r '.[].generic.bearers[0]' 2>/dev/null)"
      ((TIMEOUT_CONNECT--))
    fi
  done

  if [[ -z "$_bearer" ]]; then
    report_error "No bearer found!"
    return 1
  fi

  if [[ -z "$_bearer_interface" ]]; then
    report_error "No bearer interface found. Consider increasing \$TimeoutConnect a bit."
    return 1
  fi

  report_debug "Interface found on modem bearer"

  if [[ "$_bearer_interface" != "$Interface" ]]; then
    report_notice "Network interface used by the bearer is not the same as provided by your connection profile. Please update the profile using the right bearer interface: '$_bearer_interface'"
    Interface=$_bearer_interface
  fi

  # Disable IPv6 before bringing the interface up to prevent SLAAC
  if [[ "$IP6" == "no" ]]; then
    sysctl -q -w "net.ipv6.conf.${Interface/.//}.disable_ipv6=1"
  fi

  if ! bring_interface_up "$Interface"; then
    report_error "Failed to bring interface '$Interface' up"
    return 1
  fi

  # Some cards are plain slow to come up. Don't fail immediately.
  if ! timeout_wait "${TimeoutCarrier:-5}" '(( $(< "/sys/class/net/$Interface/carrier") ))'; then
    report_error "No connection found on interface '$Interface' (timeout)"
    bring_interface_down "$Interface"
    return 1
  fi

  local _interface_address _interface_prefix _interface_gateway _interface_dns

  # Configuring IPv4
  if [[ "$(mmcli -J -m "$MODEM" -b "$_bearer" 2>/dev/null | jq -r '.[]."ipv4-config".method')" == "static" ]]; then
    IP="static"
    _interface_address="$(mmcli -J -m "$MODEM" -b "$_bearer" 2>/dev/null | jq -r '.[]."ipv4-config".address')"
    _interface_prefix="$(mmcli -J -m "$MODEM" -b "$_bearer" 2>/dev/null | jq -r '.[]."ipv4-config".prefix')"
    _interface_gateway="$(mmcli -J -m "$MODEM" -b "$_bearer" 2>/dev/null | jq -r '.[]."ipv4-config".gateway')"
    _interface_dns="$(mmcli -J -m "$MODEM" -b "$_bearer" 2>/dev/null | jq -r '.[]."ipv4-config".dns[0]')"

    Address=("${_interface_address}/${_interface_prefix}")
    Gateway="$_interface_gateway"
    [[ "${UsePeerDNS:-true}" == 'true' ]] && DNS=("$_interface_dns")
  elif [[ "$(mmcli -J -m "$MODEM" -b "$_bearer" 2>/dev/null | jq -r '.[]."ipv4-config".method')" == "dhcp" ]]; then
    IP="dhcp"
  fi

  # FIXME: IPv6 Support (requires testing)
  # Configuring IPv6
  if [[ "$IP6" == 'dhcp' ]]; then
    if [[ "$(mmcli -J -m "$MODEM" -b "$_bearer" 2>/dev/null | jq -r '.[]."ipv6-config".method')" == "static" ]]; then
      IP="static"
      _interface_address="$(mmcli -J -m "$MODEM" -b "$_bearer" 2>/dev/null | jq -r '.[]."ipv6-config".address')"
      _interface_prefix="$(mmcli -J -m "$MODEM" -b "$_bearer" 2>/dev/null | jq -r '.[]."ipv6-config".prefix')"
      _interface_gateway="$(mmcli -J -m "$MODEM" -b "$_bearer" 2>/dev/null | jq -r '.[]."ipv6-config".gateway')"
      _interface_dns="$(mmcli -J -m "$MODEM" -b "$_bearer" 2>/dev/null | jq -r '.[]."ipv6-config".dns[0]')"

      Address=("${_interface_address}/${_interface_prefix}")
      Gateway="$_interface_gateway"
      [[ "${UsePeerDNS:-true}" == 'true' ]] && DNS=("$_interface_dns")
    elif [[ "$(mmcli -J -m "$MODEM" -b "$_bearer" 2>/dev/null | jq -r '.[]."ipv6-config".method')" == "dhcp" ]]; then
      IP6="dhcp"
    fi
  fi

  if ! ip_set; then
    bring_interface_down "$Interface"
    return 1
  fi
}

function mobile_mm_down {
  local _bearer _bearer_interface
  _bearer="$(mmcli -J -m "$MODEM" 2>/dev/null | jq -r '.[].generic.bearers[0]' 2>/dev/null)"
  _bearer_interface="$(mmcli -J -m "$MODEM" -b "$_bearer" 2>/dev/null | jq -r '.[].status.interface')"

  if [[ "$(mmcli -J -m "$MODEM" -b "$_bearer" 2>/dev/null | jq -r '.[]."ipv4-config".method')" == "static" ]]; then
    _interface_address="$(mmcli -J -m "$MODEM" -b "$_bearer" 2>/dev/null | jq -r '.[]."ipv4-config".address')"
    _interface_prefix="$(mmcli -J -m "$MODEM" -b "$_bearer" 2>/dev/null | jq -r '.[]."ipv4-config".prefix')"
    _interface_gateway="$(mmcli -J -m "$MODEM" -b "$_bearer" 2>/dev/null | jq -r '.[]."ipv4-config".gateway')"
    _interface_dns="$(mmcli -J -m "$MODEM" -b "$_bearer" 2>/dev/null | jq -r '.[]."ipv4-config".dns[0]')"

    Address=("${_interface_address}/${_interface_prefix}")
    Gateway="$_interface_gateway"
    [[ "${UsePeerDNS:-true}" == 'true' ]] && DNS=("$_interface_dns")
  fi

  if [[ "$(mmcli -J -m "$MODEM" -b "$_bearer" 2>/dev/null | jq -r '.[]."ipv6-config".method')" == "static" ]]; then
    _interface_address="$(mmcli -J -m "$MODEM" -b "$_bearer" 2>/dev/null | jq -r '.[]."ipv6-config".address')"
    _interface_prefix="$(mmcli -J -m "$MODEM" -b "$_bearer" 2>/dev/null | jq -r '.[]."ipv6-config".prefix')"
    _interface_gateway="$(mmcli -J -m "$MODEM" -b "$_bearer" 2>/dev/null | jq -r '.[]."ipv6-config".gateway')"
    _interface_dns="$(mmcli -J -m "$MODEM" -b "$_bearer" 2>/dev/null | jq -r '.[]."ipv6-config".dns[0]')"

    Address=("${_interface_address}/${_interface_prefix}")
    Gateway="$_interface_gateway"
    [[ "${UsePeerDNS:-true}" == 'true' ]] && DNS=("$_interface_dns")
  fi

  mmcli -m "$MODEM" --simple-disconnect 2>/dev/null
  ip_unset
  interface_delete "$Interface"

  if [[ $RFKill ]]; then
    rf_disable "$Interface" "$RFKill"
  fi
}

# Check that the dependencies are available
for PROGRAM in "${DEPENDENCIES[@]}"; do
  check_deps "$PROGRAM"
done

# Try getting the modem path
MODEM="$(mmcli -J -L 2>/dev/null | jq -r '.[][0]' 2>/dev/null)"

if [[ -z "$MODEM" ]]; then
  report_debug "No modem found yet."

  if ! systemctl is-active ModemManager.service &>/dev/null; then
    report_notice "ModemManager service not running, trying to start it.."
    systemctl start ModemManager.service

    # Wait until ModemManager has initialised the modem
    report_debug "Sleeping until modem is available.."
    TIMEOUT_MODEM=${TimeoutModem:-15}
    until [[ "$(mmcli -J -L 2>/dev/null | jq -r '.[][0]' 2>/dev/null)" =~ ^/org/freedesktop/ModemManager[0-9]+/Modem/[0-9]+$ ]] || [ "$TIMEOUT_MODEM" -eq 0 ]; do
      sleep 1
      ((TIMEOUT_MODEM--))
    done

    # Finally get the modem path
    MODEM="$(mmcli -J -L 2>/dev/null | jq -r '.[][0]' 2>/dev/null)"
  fi
fi

MODEM_ID="$(mmcli -J -L 2>/dev/null | jq -r '.[][0] | [splits("/")] | (.[5] | tonumber)' 2>/dev/null)"
if [[ -z "$MODEM_ID" ]] || [[ ! "$MODEM_ID" =~ ^[0-9]+$ ]]; then
  report_error "No modem found! Please ensure that ModemManager is capable of finding your modem."
  exit 1
fi

# Get the modem status
MODEM_STATUS="$(mmcli -J -m "$MODEM" 2>/dev/null | jq -r '.[].generic.state')"

if [[ "$MODEM_STATUS" == 'locked' ]]; then
  report_notice "Modem is locked. Trying to supply PUK/PIN, if provided in the connection profile.."

  # Getting SIM for modem
  SIM="$(mmcli -J -m "$MODEM" 2>/dev/null | jq -r '.[].generic.sim')"

  if [[ -n "$SIM" ]]; then
    if [[ $Puk ]]; then
      report_notice "Trying to unlock using the PUK.."
      mmcli -m "$MODEM" -i "$SIM" --puk="${Pin}" >/dev/null 2>&1
    fi

    if [[ $Pin ]]; then
      report_notice "Trying to unlock using the PIN.."
      mmcli -m "$MODEM" -i "$SIM" --pin="${Pin}" >/dev/null 2>&1
    fi

    MODEM_STATUS="$(mmcli -J -m "$MODEM" 2>/dev/null | jq -r '.[].generic.state')"
    if [[ "$MODEM_STATUS" == 'locked' ]]; then
      report_error "Modem is still locked. Maybe the supplied PIN/PUK is wrong?"
      exit 1
    fi
  else
    report_error "No SIM card found!"
    exit 1
  fi
fi

# vim: ft=sh ts=2 et sw=2: