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