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