]> git.ipfire.org Git - people/ms/network.git/blob - hooks/zones/modem
57a90e01171b10d418fb3ecc02dcefd3d0d19002
[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 HOOK_SETTINGS="HOOK AUTH DEVICE BAUDRATE LINKNAME USER SECRET PEERDNS DEFAULTROUTE MTU"
25
26 AUTH=
27 BAUDRATE=921600
28 DEFAULTROUTE=1
29 DEVICE=
30 LINKNAME="$(uuid)"
31 MTU=1492
32 PEERDNS=1
33 SECRET=
34 USER=
35
36 MODEM_ALLOWED_AUTHS="chap pap"
37
38 function pppd_pid() {
39 local zone=${1}
40 shift
41
42 cat /var/run/${zone}.pid 2>/dev/null
43 }
44
45 function _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
60 function _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
111 function _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
129 ifname ${zone}
130 name ${LINKNAME}
131 linkname ${LINKNAME}
132
133 # Device configuration
134 /dev/${DEVICE} ${BAUDRATE}
135 connect "/usr/sbin/chat -v -f /etc/ppp/dialer"
136 lock
137 modem
138 crtscts
139
140 # User configuration
141 user ${USER}
142
143 $(enabled PEERDNS && echo "usepeerdns")
144 $(enabled DEFAULTROUTE && echo "defaultroute")
145
146 noauth
147 $(isset AUTH && echo "require-${AUTH}")
148
149 noipdefault
150
151 # Maximum transmission/receive unit
152 mtu ${MTU}
153 mru ${MTU}
154
155 # Disable the compression
156 noccp noaccomp nodeflate nopcomp novj novjccomp nobsdcomp nomppe
157
158 updetach debug
159 EOF
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
178 function _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
189 function _status() {
190 local zone=${1}
191 assert isset zone
192
193 cli_device_headline ${zone}
194
195 zone_config_read ${zone}
196
197 cli_headline " Configuration:"
198 printf "${DEVICE_PRINT_LINE1}" "User:" "${USER}"
199 printf "${DEVICE_PRINT_LINE1}" "Secret:" "<hidden>"
200 echo
201 printf "${DEVICE_PRINT_LINE1}" "MTU:" "${MTU}"
202 printf "${DEVICE_PRINT_LINE1}" "Use default route?" "$(enabled DEFAULTROUTE && echo "enabled" || echo "disabled")"
203 printf "${DEVICE_PRINT_LINE1}" "Use peer DNS?" "$(enabled PEERDNS && echo "enabled" || echo "disabled")"
204 echo
205 cli_headline " Ports:"
206 zone_ports_status ${zone}
207 if [ -z "$(zone_get_ports ${zone})" ]; then
208 echo -e " ${COLOUR_WARN}No ports attached. Won't be able to start.${COLOUR_NORMAL}"
209 fi
210
211 # Exit if zone is down
212 if ! zone_is_up ${zone}; then
213 echo # Empty line
214 exit ${EXIT_ERROR}
215 fi
216
217 # XXX display time since connection started
218
219 cli_headline " Point-to-Point-over-Ethernet protocol:"
220 echo " IP-Address : $(routing_db_get ${zone} local-ip-address)"
221 echo " Gateway : $(routing_db_get ${zone} remote-ip-address)"
222 echo " DNS-Server : $(routing_db_get ${zone} dns)"
223 echo
224 echo " MAC-Remote : $(routing_db_get ${zone} remote-address)"
225 echo
226 echo " MTU : $(device_get_mtu ${zone})"
227 echo # Empty line
228 exit ${EXIT_OK}
229 }
230
231 run $@