]> git.ipfire.org Git - people/stevee/network.git/blame - hooks/zones/modem
Initial support for modems (PPP).
[people/stevee/network.git] / 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
24HOOK_SETTINGS="HOOK AUTH DEVICE BAUDRATE LINKNAME USER SECRET PEERDNS DEFAULTROUTE MTU"
25
26AUTH=
27BAUDRATE=921600
28DEFAULTROUTE=1
29DEVICE=
30LINKNAME="$(uuid)"
31MTU=1492
32PEERDNS=1
33SECRET=
34USER=
35
36MODEM_ALLOWED_AUTHS="chap pap"
37
38function pppd_pid() {
39 local zone=${1}
40 shift
41
42 cat /var/run/${zone}.pid 2>/dev/null
43}
44
45function _check() {
46 assert isset USER
47 assert isset SECRET
48 assert isset LINKNAME
49 assert isset DEFAULTROUTE
50 assert isset PEERDNS
51 assert isset DEVICE
52
53 assert isbool DEFAULTROUTE
54 assert isbool PEERDNS
55 assert isinteger BAUDRATE
56
57 isset AUTH && assert isoneof AUTH ${MODEM_ALLOWED_AUTHS}
58}
59
60function _parse_cmdline() {
61 local value
62
63 while [ $# -gt 0 ]; do
64 case "$1" in
65 --user=*)
66 USER=${1#--user=}
67 ;;
68 --secret=*)
69 SECRET=${1#--secret=}
70 ;;
71 --linkname=*)
72 LINKNAME=${1#--name=}
73 ;;
74 --mtu=*)
75 MTU=${1#--mtu=}
76 ;;
77 --defaultroute=*)
78 value=${1#--defaultroute=}
79 if enabled value; then
80 DEFAULTROUTE=1
81 else
82 DEFAULTROUTE=0
83 fi
84 ;;
85 --dns=*)
86 value=${1#--dns=}
87 if enabled value; then
88 PEERDNS=1
89 else
90 PEERDNS=0
91 fi
92 ;;
93 --auth=*)
94 AUTH=${1#--auth=}
95 ;;
96 --device=*)
97 DEVICE=${1#--device=}
98 ;;
99 --baudrate=*)
100 BAUDRATE=${1#--baudrate=}
101 ;;
102 *)
103 echo "Unknown option: $1" >&2
104 exit ${EXIT_ERROR}
105 ;;
106 esac
107 shift
108 done
109}
110
111function _up() {
112 local zone=${1}
113 shift
114
115 assert isset zone
116
117 zone_config_read ${zone}
118
119 assert [ -e "/dev/${DEVICE}" ]
120
121 # Creating necessary files
122 # XXX must be PPP_RUN
123 [ -d "${RED_RUN}/${LINKNAME}" ] || mkdir -p ${RED_RUN}/${LINKNAME}
124
125 ppp_secret "${USER}" "${SECRET}"
126
127 cat <<EOF >${RED_RUN}/${LINKNAME}/options
128# Naming options
129ifname ${zone}
130name ${LINKNAME}
131linkname ${LINKNAME}
132
133# Device configuration
134/dev/${DEVICE} ${BAUDRATE}
135connect "/usr/sbin/chat -v -f /etc/ppp/dialer"
136lock
137modem
138crtscts
139
140# User configuration
141user ${USER}
142
143$(enabled PEERDNS && echo "usepeerdns")
144$(enabled DEFAULTROUTE && echo "defaultroute")
145
146noauth
147$(isset AUTH && echo "require-${AUTH}")
148
149noipdefault
150
151# Maximum transmission/receive unit
152mtu ${MTU}
153mru ${MTU}
154
155# Disable the compression
156noccp noaccomp nodeflate nopcomp novj novjccomp nobsdcomp nomppe
157
158updetach debug
159EOF
160
161 pppd_exec file ${RED_RUN}/${LINKNAME}/options
162
163 local ret=$?
164
165 # Get exit code from ppp daemon and handle it:
166 case "${ret}" in
167 0)
168 log DEBUG "pppd detached successfully"
169 exit ${EXIT_OK}
170 ;;
171 esac
172
173 error_log "pppd exited with unknown exit code '${ret}'"
174
175 exit ${EXIT_ERROR}
176}
177
178function _down() {
179 local zone=${1}
180 shift
181
182 # Kill pppd
183 # XXX very ugly
184 kill $(pppd_pid ${zone}) &>/dev/null
185
186 exit ${EXIT_OK}
187}
188
189function _status() {
190 local zone=${1}
191
192 assert isset zone
193
194 cli_status_headline ${zone}
195
196 zone_config_read ${zone}
197
198 cli_headline " Configuration:"
199 printf "${DEVICE_PRINT_LINE1}" "User:" "${USER}"
200 printf "${DEVICE_PRINT_LINE1}" "Secret:" "<hidden>"
201 echo
202 printf "${DEVICE_PRINT_LINE1}" "MTU:" "${MTU}"
203 printf "${DEVICE_PRINT_LINE1}" "Use default route?" "$(enabled DEFAULTROUTE && echo "enabled" || echo "disabled")"
204 printf "${DEVICE_PRINT_LINE1}" "Use peer DNS?" "$(enabled PEERDNS && echo "enabled" || echo "disabled")"
205 echo
206 cli_headline " Ports:"
207 zone_ports_status ${zone}
208 if [ -z "$(zone_get_ports ${zone})" ]; then
209 echo -e " ${COLOUR_WARN}No ports attached. Won't be able to start.${COLOUR_NORMAL}"
210 fi
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 " Point-to-Point-over-Ethernet protocol:"
221 echo " IP-Address : $(red_db_get ${zone} local-ip-address)"
222 echo " Gateway : $(red_db_get ${zone} remote-ip-address)"
223 echo " DNS-Server : $(red_db_get ${zone} dns)"
224 echo
225 echo " MAC-Remote : $(red_db_get ${zone} remote-address)"
226 echo
227 echo " MTU : $(device_get_mtu ${zone})"
228 echo # Empty line
229 exit ${EXIT_OK}
230}
231
232run $@