]> git.ipfire.org Git - people/stevee/network.git/blame - hooks/zones/pppoe
config: Better formatting for configuration dump.
[people/stevee/network.git] / hooks / zones / pppoe
CommitLineData
1848564d
MT
1#!/bin/bash
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
97cb552e 5# Copyright (C) 2012 IPFire Network Development Team #
1848564d
MT
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
f41fa3d7 22. /usr/lib/network/header-zone
1848564d 23
97cb552e
MT
24HOOK_SETTINGS="HOOK ACCESS_CONCENTRATOR AUTH USERNAME PASSWORD"
25HOOK_SETTINGS="${HOOK_SETTINGS} SERVICE_NAME MTU PORT"
1848564d 26
97cb552e
MT
27# User credentials for the dialin.
28USERNAME=""
29PASSWORD=""
1848564d 30
97cb552e 31# Set the authentication mechanism.
1848564d 32AUTH=
97cb552e
MT
33
34# The physical ethernet port the modem is connected to.
35PORT=""
36
37# Access Concentrator.
38ACCESS_CONCENTRATOR=""
39
40# Service name.
41SERVICE_NAME=""
42
43# Maximum Transmission Unit.
44# 1492 is a very common value for that.
1848564d 45MTU=1492
1848564d 46
97cb552e 47# This hook can work with all authentication methods supported by pppd.
3a829636 48PPPOE_SUPPORTED_AUTH_METHODS="${PPP_SUPPORTED_AUTH_METHODS}"
1848564d
MT
49PPPOE_PLUGIN="rp-pppoe.so"
50
97cb552e
MT
51function _check() {
52 assert isset USERNAME
53 assert isset PASSWORD
261132f9 54
3a829636 55 isset AUTH && assert isoneof AUTH ${PPPOE_SUPPORTED_AUTH_METHODS}
261132f9 56
97cb552e
MT
57 # Check for a valid port setting.
58 assert isset PORT
59 assert port_exists ${PORT}
1848564d
MT
60}
61
62function _parse_cmdline() {
63 while [ $# -gt 0 ]; do
97cb552e
MT
64 case "${1}" in
65 --access-concentrator=*)
66 ACCESS_CONCENTRATOR=$(cli_get_val ${1})
1848564d 67 ;;
97cb552e
MT
68 --auth=*)
69 AUTH=$(cli_get_val ${1})
1848564d
MT
70 ;;
71 --mtu=*)
97cb552e 72 MTU=$(cli_get_val ${1})
1848564d 73 ;;
97cb552e
MT
74 --password=*)
75 PASSWORD=$(cli_get_val ${1})
1848564d 76 ;;
97cb552e
MT
77 --port=*)
78 PORT=$(cli_get_val ${1})
1848564d 79 ;;
97cb552e
MT
80 --service-name=*)
81 SERVICE_NAME=$(cli_get_val ${1})
1848564d 82 ;;
97cb552e
MT
83 --username=*)
84 USERNAME=$(cli_get_val ${1})
201b7dff 85 ;;
1848564d 86 *)
97cb552e 87 warning "Unknown argument: ${1}" >&2
1848564d
MT
88 ;;
89 esac
90 shift
91 done
1848564d
MT
92}
93
94function _up() {
95 local zone=${1}
711ffac1
MT
96 assert isset zone
97
2044f591
MT
98 zone_config_read ${zone}
99
100 # Bring up the port.
101 log DEBUG "Bringing up port '${PORT}'."
102 port_up ${PORT}
103
97cb552e
MT
104 # Start the ppp daemon.
105 pppd_start ${zone}
da453c33 106
97cb552e 107 exit ${EXIT_OK}
1848564d
MT
108}
109
110function _down() {
111 local zone=${1}
97cb552e 112 assert isset zone
1848564d 113
2044f591
MT
114 zone_config_read ${zone}
115
97cb552e
MT
116 # Stop the ppp daemon.
117 pppd_stop ${zone}
1848564d 118
2044f591
MT
119 # Bring down the port.
120 log DEBUG "Bringing down port '${PORT}'."
121 port_down ${PORT}
122
1848564d
MT
123 exit ${EXIT_OK}
124}
125
126function _discover() {
127 local device=${1}
128
129 if [ "$(device_get_type ${device})" != "real" ]; then
5b20e43a 130 exit ${EXIT_ERROR}
1848564d
MT
131 fi
132
133 local output
134 output=$(pppoe-discovery -I ${device} -U $(uuid) 2>&1)
135
136 # Exit if there was not output
137 [ -z "${output}" ] && exit ${DISCOVER_ERROR}
138
139 # Exit if PADI timed out
140 grep -q "Timeout" <<<${output} && exit ${DISCOVER_ERROR}
141
142 local ac
143 while read line; do
144 case "${line}" in
145 Access-Concentrator:*)
146 ac="${line#Access-Concentrator: }"
147 ;;
148 esac
149 done <<<"${output}"
150
151 echo "ACCESS_CONCENTRATOR=\"$ac\""
152
153 exit ${DISCOVER_OK}
154}
5b20e43a 155
8eadf1da
MT
156function _status() {
157 local zone=${1}
711ffac1
MT
158 assert isset zone
159
3cb2fc42 160 cli_device_headline ${zone}
8eadf1da 161
711ffac1
MT
162 zone_config_read ${zone}
163
3cb2fc42 164 cli_headline 2 "Configuration"
97cb552e
MT
165 cli_print_fmt1 2 "Username" "${USERNAME}"
166 cli_print_fmt1 2 "Password" "<hidden>"
167 cli_print_fmt1 2 "Port" "${PORT}"
3cb2fc42
MT
168 cli_space
169
8eadf1da
MT
170 # Exit if zone is down
171 if ! zone_is_up ${zone}; then
172 echo # Empty line
173 exit ${EXIT_ERROR}
174 fi
175
711ffac1
MT
176 # XXX display time since connection started
177
3cb2fc42 178 cli_headline 2 "Point-to-Point-over-Ethernet protocol"
201b7dff
MT
179 local proto
180 for proto in ${IP_SUPPORTED_PROTOCOLS}; do
181 routing_db_exists ${zone} ${proto} || continue
3cb2fc42
MT
182
183 local headline
184 case "${proto}" in
185 ipv6)
186 headline="Internet Protocol Version 6"
187 ;;
188 ipv4)
189 headline="Internet Protocol Version 4"
190 ;;
191 *)
192 headline="Unkown protocol"
193 ;;
194 esac
195 cli_headline 3 "${headline}"
196
197 cli_print_fmt1 3 "IP address" "$(routing_db_get ${zone} ${proto} local-ip-address)"
198 cli_print_fmt1 3 "Gateway" "$(routing_db_get ${zone} ${proto} remote-ip-address)"
199 cli_print_fmt1 3 "DNS servers" "$(routing_db_get ${zone} ${proto} dns)"
200 cli_space
201 cli_print_fmt1 3 "MAC-Remote" "$(routing_db_get ${zone} ${proto} remote-address)"
202 cli_space
201b7dff 203 done
3cb2fc42 204
8eadf1da
MT
205 exit ${EXIT_OK}
206}
207
97cb552e
MT
208function _ppp_write_config() {
209 local zone=${1}
210 assert isset zone
211
212 local file=${2}
213 assert isset file
214
215 # Read in the configuration files.
216 zone_config_read ${zone}
217
97cb552e
MT
218 # Prepare the command line options for the pppoe plugin.
219 local plugin_options
220
221 # Add the access concentrator (if any).
222 if isset ACCESS_CONCENTRATOR; then
223 plugin_options="${plugin_options} rp_pppoe_ac '${ACCESS_CONCENTRATOR}'"
711ffac1
MT
224 fi
225
97cb552e
MT
226 # Add the service name (if any).
227 if isset SERVICE_NAME; then
228 plugin_options="${plugin_options} rp_pppoe_service '${SERVICE_NAME}'"
229 fi
711ffac1 230
97cb552e
MT
231 # The last argument must be the interface.
232 plugin_options="${plugin_options} ${PORT}"
233
234 pppd_write_config ${file} \
235 --interface="${zone}" \
6c74a64c
MT
236 --username="${USERNAME}" \
237 --password="${PASSWORD}" \
97cb552e
MT
238 --mtu="${MTU}" \
239 --auth="${AUTH}" \
240 \
241 --plugin="${PPPOE_PLUGIN}" \
242 --plugin-options="${plugin_options}"
243
6c74a64c 244 exit ${EXIT_OK}
711ffac1 245}