]> git.ipfire.org Git - people/stevee/network.git/blame - src/hooks/zones/modem
Replace routing_db_* by db_*
[people/stevee/network.git] / src / hooks / zones / modem
CommitLineData
58cbe2e4
AF
1#!/bin/bash
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
5# Copyright (C) 2010 Michael Tremer & Christian Schmidt #
6# #
7# This program is free software: you can redistribute it and/or modify #
8# it under the terms of the GNU General Public License as published by #
9# the Free Software Foundation, either version 3 of the License, or #
10# (at your option) any later version. #
11# #
12# This program is distributed in the hope that it will be useful, #
13# but WITHOUT ANY WARRANTY; without even the implied warranty of #
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15# GNU General Public License for more details. #
16# #
17# You should have received a copy of the GNU General Public License #
18# along with this program. If not, see <http://www.gnu.org/licenses/>. #
19# #
20###############################################################################
21
22. /lib/network/header-zone
23
6c74a64c
MT
24# Modems support all authentication methods, that pppd does support.
25MODEM_ALLOWED_AUTH_METHODS="${PPP_ALLOWED_AUTH_METHODS}"
58cbe2e4 26
6c74a64c
MT
27HOOK_SETTINGS="HOOK"
28
29# Access Point Name.
30APN=
31HOOK_SETTINGS="${HOOK_SETTINGS} APN"
32
33# Sets the authentication algortihm that must be used.
58cbe2e4 34AUTH=
6c74a64c
MT
35HOOK_SETTINGS="${HOOK_SETTINGS} AUTH"
36
37# Baudrate.
58cbe2e4 38BAUDRATE=921600
6c74a64c
MT
39HOOK_SETTINGS="${HOOK_SETTINGS} BAUDRATE"
40
41# The device name of the serial device.
42# XXX how can we make sure that this does not change all the time?
58cbe2e4 43DEVICE=
6c74a64c
MT
44HOOK_SETTINGS="${HOOK_SETTINGS} DEVICE"
45
46# A monitor device.
47# Send AT commands to this device, when the primary device is
48# connected.
49MONITOR_DEVICE=
50HOOK_SETTINGS="${HOOK_SETTINGS} MONITOR_DEVICE"
51
52# Maximum transmission unit.
58cbe2e4 53MTU=1492
6c74a64c 54HOOK_SETTINGS="${HOOK_SETTINGS} MTU"
58cbe2e4 55
6c74a64c
MT
56# User credentials.
57USERNAME=
58PASSWORD=
59HOOK_SETTINGS="${HOOK_SETTINGS} USERNAME PASSWORD"
58cbe2e4 60
6c74a64c
MT
61# PIN code.
62PIN=
63HOOK_SETTINGS="${HOOK_SETTINGS} PIN"
58cbe2e4 64
6c74a64c
MT
65# Phone number.
66PHONE_NUMBER=
67HOOK_SETTINGS="${HOOK_SETTINGS} PHONE_NUMBER"
58cbe2e4 68
87102ab4
MT
69# IMSI
70IMSI=
71HOOK_SETTINGS="${HOOK_SETTINGS} IMSI"
72
1c6a4e30 73hook_check_settings() {
58cbe2e4 74 assert isset DEVICE
6c74a64c
MT
75 assert isset PHONE_NUMBER
76
77 # Make sure the PIN code is an integer, when set.
78 if isset PIN; then
79 assert isinteger PIN
80 assert [ ${#PIN} -ge 4 ]
81 assert [ ${#PIN} -le 8 ]
82 fi
58cbe2e4 83
6c74a64c 84 assert isoneof BAUDRATE ${SERIAL_BAUDRATES}
58cbe2e4 85
6c74a64c 86 isset AUTH && assert isoneof AUTH ${MODEM_ALLOWED_AUTH_METHODS}
58cbe2e4
AF
87}
88
1c6a4e30 89hook_parse_cmdline() {
58cbe2e4
AF
90 local value
91
92 while [ $# -gt 0 ]; do
6c74a64c
MT
93 case "${1}" in
94 --apn=*)
95 APN=$(cli_get_val ${1})
58cbe2e4 96 ;;
6c74a64c
MT
97 --auth=*)
98 AUTH=$(cli_get_val ${1})
58cbe2e4 99 ;;
6c74a64c
MT
100 --baudrate=*)
101 BAUDRATE=$(cli_get_val ${1})
102 assert isoneif "${BAUDRATE}" ${SERIAL_BAUDRATES}
103 ;;
104 --device=*)
105 DEVICE=$(cli_get_val ${1})
106 ;;
87102ab4
MT
107 --imsi=*)
108 IMSI="$(cli_get_val "${1}")"
109 ;;
6c74a64c
MT
110 --monitor-device=*)
111 MONITOR_DEVICE=$(cli_get_val ${1})
58cbe2e4
AF
112 ;;
113 --mtu=*)
6c74a64c
MT
114 MTU=$(cli_get_val ${1})
115 assert isinteger ${MTU}
58cbe2e4 116 ;;
6c74a64c
MT
117 --password=*)
118 PASSWORD=$(cli_get_val ${1})
58cbe2e4 119 ;;
6c74a64c
MT
120 --phone-number=*)
121 PHONE_NUMBER=$(cli_get_val ${1})
58cbe2e4 122 ;;
6c74a64c
MT
123 --pin=*)
124 PIN=$(cli_get_val ${1})
58cbe2e4 125 ;;
6c74a64c
MT
126 --username=*)
127 USERNAME=$(cli_get_val ${1})
58cbe2e4
AF
128 ;;
129 *)
6c74a64c 130 echo "Unknown argument: ${1}" >&2
58cbe2e4
AF
131 exit ${EXIT_ERROR}
132 ;;
133 esac
134 shift
135 done
136}
137
1c6a4e30 138hook_up() {
58cbe2e4 139 local zone=${1}
58cbe2e4
AF
140 assert isset zone
141
6c74a64c 142 # Load configuration file.
1e6f187e 143 zone_settings_read "${zone}"
58cbe2e4 144
6c74a64c
MT
145 # If we have got a PIN, we try to unlock the device first.
146 if isset PIN; then
87102ab4
MT
147 if ! modem_sim_auto_unlock "${DEVICE}" "${PIN}"; then
148 # Reset the PIN setting.
149 PIN=""
1e6f187e 150 zone_settings_write "${zone}"
87102ab4
MT
151 error "Could not unlock the SIM card. Removing PIN from settings."
152 fi
58cbe2e4 153
6c74a64c
MT
154 # For mobile devices, check if a PIN is required although none is set.
155 elif modem_is_mobile ${DEVICE} && modem_sim_locked ${DEVICE}; then
156 error "The SIM card is locked. Please configure the PIN code."
157 exit ${EXIT_ERROR}
158 fi
58cbe2e4 159
6c74a64c
MT
160 # Start the PPP daemon.
161 pppd_start ${zone}
58cbe2e4 162
6c74a64c 163 exit ${EXIT_OK}
58cbe2e4
AF
164}
165
1c6a4e30 166hook_down() {
58cbe2e4 167 local zone=${1}
6c74a64c 168 assert isset zone
58cbe2e4 169
6c74a64c 170 # Stop the PPP daemon.
6d91bb88 171 pppd_stop ${zone}
58cbe2e4
AF
172
173 exit ${EXIT_OK}
174}
175
1c6a4e30 176hook_status() {
58cbe2e4 177 local zone=${1}
58cbe2e4
AF
178 assert isset zone
179
3cb2fc42 180 cli_device_headline ${zone}
58cbe2e4 181
1e6f187e 182 zone_settings_read "${zone}"
58cbe2e4 183
6c74a64c
MT
184 cli_headline 2 "Configuration"
185 cli_print_fmt1 2 "Username" "${USERNAME}"
186 cli_print_fmt1 2 "Password" "<hidden>"
187 cli_space
188
189 cli_headline 2 "Device settings"
190 cli_print_fmt1 2 "Device" "${DEVICE}"
191 if isset MONITOR_DEVICE; then
192 cli_print_fmt1 2 "Monitor device" "${MONITOR_DEVICE}"
58cbe2e4 193 fi
6c74a64c
MT
194 cli_print_fmt1 2 "Baudrate" "${BAUDRATE}"
195 cli_print_fmt1 2 "MTU/MRU" "${MTU}"
196 cli_space
58cbe2e4
AF
197
198 # Exit if zone is down
199 if ! zone_is_up ${zone}; then
200 echo # Empty line
201 exit ${EXIT_ERROR}
202 fi
203
6c74a64c
MT
204 cli_headline 2 "Carrier network"
205
206 # If the device and the monitor device are both locked,
207 # we cannot show any carrier information.
208 local device dev
209 for dev in ${DEVICE} ${MONITOR_DEVICE}; do
210 if ! serial_exists ${dev}; then
211 continue
212 fi
213 if serial_is_locked ${dev}; then
214 continue
215 fi
216
217 device=${dev}
218 done
219
220 if isset device; then
221 cli_print_fmt1 2 "Operator" \
222 "$(modem_get_network_operator ${device})"
223 cli_print_fmt1 2 "SIM IMSI" \
224 "$(modem_get_sim_imsi ${device})"
225 cli_print_fmt1 2 "Mode" \
226 "$(modem_get_network_mode ${device})"
227 cli_print_fmt1 2 "Signal strength" \
228 "$(modem_get_signal_quality ${device}) dBm"
229 local ber=$(modem_get_bit_error_rate ${device})
230 isset ber || ber="unknown"
231 cli_print_fmt1 2 "Bit error rate" "${ber}"
232 else
233 cli_print 2 "Device is locked."
234 fi
235 cli_space
236
58cbe2e4
AF
237 # XXX display time since connection started
238
6c74a64c
MT
239 cli_headline 2 "Point-to-Point-over-Ethernet protocol"
240 local proto
241 for proto in ${IP_SUPPORTED_PROTOCOLS}; do
c041b631 242 db_exists "${zone}/${proto}" || continue
6c74a64c
MT
243
244 local headline
245 case "${proto}" in
246 ipv6)
247 headline="Internet Protocol Version 6"
248 ;;
249 ipv4)
250 headline="Internet Protocol Version 4"
251 ;;
252 *)
253 headline="Unkown protocol"
254 ;;
255 esac
256 cli_headline 3 "${headline}"
257
c041b631
MT
258 cli_print_fmt1 3 "IP address" "$(db_get "${zone}/${proto}/local-ip-address")"
259 cli_print_fmt1 3 "Gateway" "$(db_get "${zone}/${proto}/remote-ip-address")"
260 cli_print_fmt1 3 "DNS servers" "$(db_get "${zone}/${proto}/domain-name-servers")"
6c74a64c
MT
261 cli_space
262 done
263
264 exit ${EXIT_OK}
265}
266
1c6a4e30 267hook_ppp_write_config() {
6c74a64c
MT
268 local zone=${1}
269 assert isset zone
270
271 local file=${2}
272 assert isset file
273
274 # Read in the configuration files.
1e6f187e 275 zone_settings_read "${zone}"
6c74a64c
MT
276
277 pppd_write_config ${file} \
278 --interface="${zone}" \
279 --username="${USERNAME}" \
280 --password="${PASSWORD}" \
281 --mtu="${MTU}" \
282 --auth="${AUTH}" \
283 \
284 --serial="true" \
285 --serial-device="${DEVICE}" \
286 --baudrate="${BAUDRATE}" \
287 --connect-command="/usr/lib/network/dialer ${zone}"
288
58cbe2e4
AF
289 exit ${EXIT_OK}
290}