]> git.ipfire.org Git - people/stevee/network.git/blob - src/hooks/zones/modem
50d43c75b15807f78b90fd58e30e91d972ac2867
[people/stevee/network.git] / src / hooks / zones / modem
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
24 # Modems support all authentication methods, that pppd does support.
25 MODEM_ALLOWED_AUTH_METHODS="${PPP_ALLOWED_AUTH_METHODS}"
26
27 HOOK_SETTINGS="HOOK"
28
29 # Access Point Name.
30 HOOK_SETTINGS="${HOOK_SETTINGS} APN"
31
32 # Sets the authentication algortihm that must be used.
33 HOOK_SETTINGS="${HOOK_SETTINGS} AUTH"
34
35 # Baudrate.
36 DEFAULT_BAUDRATE=921600
37 HOOK_SETTINGS="${HOOK_SETTINGS} BAUDRATE"
38
39 # The device name of the serial device.
40 # XXX how can we make sure that this does not change all the time?
41 HOOK_SETTINGS="${HOOK_SETTINGS} DEVICE"
42
43 # A monitor device.
44 # Send AT commands to this device, when the primary device is
45 # connected.
46 HOOK_SETTINGS="${HOOK_SETTINGS} MONITOR_DEVICE"
47
48 # Maximum transmission unit.
49 HOOK_SETTINGS="${HOOK_SETTINGS} MTU"
50
51 # User credentials.
52 HOOK_SETTINGS="${HOOK_SETTINGS} USERNAME PASSWORD"
53
54 # PIN code.
55 HOOK_SETTINGS="${HOOK_SETTINGS} PIN"
56
57 # Phone number.
58 HOOK_SETTINGS="${HOOK_SETTINGS} PHONE_NUMBER"
59
60 # IMSI
61 HOOK_SETTINGS="${HOOK_SETTINGS} IMSI"
62
63 hook_check_settings() {
64 assert isset DEVICE
65 assert isset PHONE_NUMBER
66
67 # Make sure the PIN code is an integer, when set.
68 if isset PIN; then
69 assert isinteger PIN
70 assert [ ${#PIN} -ge 4 ]
71 assert [ ${#PIN} -le 8 ]
72 fi
73
74 assert isoneof BAUDRATE ${SERIAL_BAUDRATES}
75
76 if isset AUTH; then
77 assert isoneof AUTH ${MODEM_ALLOWED_AUTH_METHODS}
78 fi
79 }
80
81 hook_parse_cmdline() {
82 local value
83
84 while [ $# -gt 0 ]; do
85 case "${1}" in
86 --apn=*)
87 APN=$(cli_get_val "${1}")
88 ;;
89 --auth=*)
90 AUTH=$(cli_get_val "${1}")
91 ;;
92 --baudrate=*)
93 BAUDRATE=$(cli_get_val "${1}")
94 assert isoneif "${BAUDRATE}" ${SERIAL_BAUDRATES}
95 ;;
96 --device=*)
97 DEVICE=$(cli_get_val "${1}")
98 ;;
99 --imsi=*)
100 IMSI="$(cli_get_val "${1}")"
101 ;;
102 --monitor-device=*)
103 MONITOR_DEVICE=$(cli_get_val "${1}")
104 ;;
105 --mtu=*)
106 MTU=$(cli_get_val "${1}")
107 assert isinteger ${MTU}
108 ;;
109 --password=*)
110 PASSWORD=$(cli_get_val "${1}")
111 ;;
112 --phone-number=*)
113 PHONE_NUMBER=$(cli_get_val "${1}")
114 ;;
115 --pin=*)
116 PIN=$(cli_get_val "${1}")
117 ;;
118 --username=*)
119 USERNAME=$(cli_get_val "${1}")
120 ;;
121 *)
122 echo "Unknown argument: ${1}" >&2
123 exit ${EXIT_ERROR}
124 ;;
125 esac
126 shift
127 done
128 }
129
130 hook_up() {
131 local zone=${1}
132 assert isset zone
133
134 # Load configuration file.
135 zone_settings_read "${zone}"
136
137 # If we have got a PIN, we try to unlock the device first.
138 if isset PIN; then
139 if ! modem_sim_auto_unlock "${DEVICE}" "${PIN}"; then
140 # Reset the PIN setting.
141 PIN=""
142 zone_settings_write "${zone}"
143 error "Could not unlock the SIM card. Removing PIN from settings."
144 fi
145
146 # For mobile devices, check if a PIN is required although none is set.
147 elif modem_is_mobile ${DEVICE} && modem_sim_locked ${DEVICE}; then
148 error "The SIM card is locked. Please configure the PIN code."
149 exit ${EXIT_ERROR}
150 fi
151
152 # Start the PPP daemon.
153 pppd_start ${zone}
154
155 exit ${EXIT_OK}
156 }
157
158 hook_down() {
159 local zone=${1}
160 assert isset zone
161
162 # Stop the PPP daemon.
163 pppd_stop ${zone}
164
165 exit ${EXIT_OK}
166 }
167
168 hook_status() {
169 local zone=${1}
170 assert isset zone
171
172 cli_device_headline ${zone}
173
174 zone_settings_read "${zone}"
175
176 cli_headline 2 "Configuration"
177 cli_print_fmt1 2 "Username" "${USERNAME}"
178 cli_print_fmt1 2 "Password" "<hidden>"
179 cli_space
180
181 cli_headline 2 "Device settings"
182 cli_print_fmt1 2 "Device" "${DEVICE}"
183 if isset MONITOR_DEVICE; then
184 cli_print_fmt1 2 "Monitor device" "${MONITOR_DEVICE}"
185 fi
186 cli_print_fmt1 2 "Baudrate" "${BAUDRATE}"
187 cli_print_fmt1 2 "MTU/MRU" "${MTU}"
188 cli_space
189
190 # If the device and the monitor device are both locked,
191 # we cannot show any carrier information.
192 local device dev
193 for dev in ${DEVICE} ${MONITOR_DEVICE}; do
194 if ! serial_exists ${dev}; then
195 continue
196 fi
197
198 if serial_is_locked ${dev}; then
199 continue
200 fi
201
202 device=${dev}
203 done
204
205 if isset device && modem_is_mobile "${device}"; then
206 modem_mobile_network_status "${device}" 2
207 else
208 cli_print 2 "Device is locked."
209 fi
210 cli_space
211
212 # Exit if zone is down
213 if ! zone_is_up ${zone}; then
214 echo # Empty line
215 exit ${EXIT_ERROR}
216 fi
217
218 # XXX display time since connection started
219
220 cli_headline 2 "Point-to-Point-over-Ethernet protocol"
221 local proto
222 for proto in ${IP_SUPPORTED_PROTOCOLS}; do
223 db_exists "${zone}/${proto}" || continue
224
225 local headline
226 case "${proto}" in
227 ipv6)
228 headline="Internet Protocol Version 6"
229 ;;
230 ipv4)
231 headline="Internet Protocol Version 4"
232 ;;
233 *)
234 headline="Unkown protocol"
235 ;;
236 esac
237 cli_headline 3 "${headline}"
238
239 cli_print_fmt1 3 "IP address" "$(db_get "${zone}/${proto}/local-ip-address")"
240 cli_print_fmt1 3 "Gateway" "$(db_get "${zone}/${proto}/remote-ip-address")"
241 cli_print_fmt1 3 "DNS servers" "$(db_get "${zone}/${proto}/domain-name-servers")"
242 cli_space
243 done
244
245 exit ${EXIT_OK}
246 }
247
248 hook_ppp_write_config() {
249 local zone=${1}
250 assert isset zone
251
252 local file=${2}
253 assert isset file
254
255 # Read in the configuration files.
256 zone_settings_read "${zone}"
257
258 pppd_write_config ${file} \
259 --interface="${zone}" \
260 --username="${USERNAME}" \
261 --password="${PASSWORD}" \
262 --mtu="${MTU}" \
263 --auth="${AUTH}" \
264 \
265 --serial="true" \
266 --serial-device="${DEVICE}" \
267 --baudrate="${BAUDRATE}" \
268 --connect-command="/usr/lib/network/dialer ${zone}"
269
270 exit ${EXIT_OK}
271 }