]> git.ipfire.org Git - people/stevee/network.git/blob - src/hooks/zones/pppoe
Convert HOOK_SETTINGS into an array
[people/stevee/network.git] / src / hooks / zones / pppoe
1 #!/bin/bash
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2012 IPFire Network Development Team #
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 . /usr/lib/network/header-zone
23
24 HOOK_SETTINGS=(
25 "ACCESS_CONCENTRATOR"
26 "AUTH"
27 "USERNAME"
28 "PASSWORD"
29 "SERVICE_NAME"
30 "MTU"
31 "IPV6"
32 "PREFIX_DELEGATION"
33 )
34
35 # This hook can work with all authentication methods supported by pppd.
36 PPPOE_SUPPORTED_AUTH_METHODS="${PPP_SUPPORTED_AUTH_METHODS}"
37 PPPOE_PLUGIN="rp-pppoe.so"
38
39 # Request an IPv6 address.
40 DEFAULT_IPV6="true"
41
42 # Use IPv6 prefix delegation.
43 DEFAULT_PREFIX_DELEGATION="true"
44
45 hook_check_settings() {
46 assert isset USERNAME
47 assert isset PASSWORD
48
49 isset AUTH && assert isoneof AUTH ${PPPOE_SUPPORTED_AUTH_METHODS}
50
51 assert isset IPV6
52 assert isset PREFIX_DELEGATION
53 }
54
55 hook_parse_cmdline() {
56 while [ $# -gt 0 ]; do
57 case "${1}" in
58 --access-concentrator=*)
59 ACCESS_CONCENTRATOR=$(cli_get_val "${1}")
60 ;;
61 --auth=*)
62 AUTH=$(cli_get_val "${1}")
63 ;;
64 --ipv6=*)
65 local value="$(cli_get_val "${1}")"
66 if enabled value; then
67 IPV6="true"
68 else
69 IPV6="false"
70 fi
71 ;;
72 --mtu=*)
73 MTU=$(cli_get_val "${1}")
74 ;;
75 --password=*)
76 PASSWORD=$(cli_get_val "${1}")
77 ;;
78 --prefix-delegation=*)
79 PREFIX_DELEGATION="$(cli_get_bool "${1}")"
80 ;;
81 --service-name=*)
82 SERVICE_NAME=$(cli_get_val "${1}")
83 ;;
84 --username=*)
85 USERNAME=$(cli_get_val "${1}")
86 ;;
87 *)
88 warning "Unknown argument: ${1}" >&2
89 ;;
90 esac
91 shift
92 done
93 }
94
95 hook_up() {
96 local zone=${1}
97 assert isset zone
98
99 # If this zone's port is not set, we will return
100 # with EXIT_OK so that this zone will remain active,
101 # but we cannot start pppd.
102 local port=$(__hook_get_port "${zone}")
103 if ! isset port || ! port_exists "${port}"; then
104 log WARNING "Could not bring up zone '${zone}' because no port is attached"
105 exit ${EXIT_OK}
106 fi
107
108 zone_settings_read "${zone}"
109
110 # Load the pppoe kernel module
111 module_load "pppoe"
112
113 # Bring up the port.
114 port_up "${port}"
115
116 # Start the ppp daemon.
117 pppd_start ${zone}
118
119 exit ${EXIT_OK}
120 }
121
122 hook_down() {
123 local zone=${1}
124 assert isset zone
125
126 zone_settings_read "${zone}"
127
128 # Stop the ppp daemon.
129 pppd_stop ${zone}
130
131 # Bring down the port.
132 local port=$(__hook_get_port "${zone}")
133 if isset port; then
134 log DEBUG "Bringing down port '${port}'"
135 port_down "${port}"
136 fi
137
138 exit ${EXIT_OK}
139 }
140
141 hook_hotplug() {
142 local zone="${1}"
143
144 case "$(hotplug_action)" in
145 add)
146 if hotplug_event_interface_is_port_of_zone "${zone}"; then
147 # Bring up the zone if it is enabled but not active, yet.
148 zone_start_auto "${zone}"
149
150 exit ${EXIT_OK}
151 fi
152 ;;
153 remove)
154 # PPPoE cannot work if the ethernet device has been removed
155 if hotplug_event_interface_is_port_of_zone "${zone}"; then
156 if zone_is_active "${zone}"; then
157 zone_stop "${zone}"
158 fi
159
160 exit ${EXIT_OK}
161 fi
162 ;;
163 esac
164
165 exit ${EXIT_NOT_HANDLED}
166 }
167
168 hook_discover() {
169 local device=${1}
170
171 # This obviously only works on ethernet (or compatible) devices
172 if ! device_is_ethernet_compatible "${device}"; then
173 exit ${EXIT_ERROR}
174 fi
175
176 local output
177 output=$(pppoe-discovery -I ${device} -U $(uuid) 2>&1)
178
179 # Exit if there was not output
180 [ -z "${output}" ] && exit ${DISCOVER_ERROR}
181
182 # Exit if PADI timed out
183 grep -q "Timeout" <<<${output} && exit ${DISCOVER_ERROR}
184
185 local ac
186 while read line; do
187 case "${line}" in
188 Access-Concentrator:*)
189 ac="${line#Access-Concentrator: }"
190 ;;
191 esac
192 done <<<"${output}"
193
194 echo "ACCESS_CONCENTRATOR=\"$ac\""
195
196 exit ${DISCOVER_OK}
197 }
198
199 hook_status() {
200 local zone=${1}
201 assert isset zone
202
203 cli_device_headline ${zone}
204
205 zone_settings_read "${zone}"
206
207 cli_headline 2 "Configuration"
208 cli_print_fmt1 2 "Username" "${USERNAME}"
209 cli_print_fmt1 2 "Password" "<hidden>"
210
211 local port=$(__hook_get_port "${zone}")
212 if isset port; then
213 cli_print_fmt1 2 "Port" "${port}"
214 fi
215 cli_space
216
217 # Exit if zone is down
218 if ! zone_is_up ${zone}; then
219 echo # Empty line
220 exit ${EXIT_ERROR}
221 fi
222
223 # XXX display time since connection started
224
225 cli_headline 2 "Point-to-Point-over-Ethernet protocol"
226 cli_print_fmt1 2 "MAC-Remote" "$(db_get "${zone}/remote-address")"
227 cli_space
228
229 local proto
230 for proto in ${IP_SUPPORTED_PROTOCOLS}; do
231 db_exists "${zone}/${proto}" || continue
232
233 local headline
234 case "${proto}" in
235 ipv6)
236 headline="Internet Protocol Version 6"
237 ;;
238 ipv4)
239 headline="Internet Protocol Version 4"
240 ;;
241 *)
242 headline="Unkown protocol"
243 ;;
244 esac
245 cli_headline 3 "${headline}"
246
247 cli_print_fmt1 3 "IP address" "$(db_get "${zone}/${proto}/local-ip-address")"
248 cli_print_fmt1 3 "Gateway" "$(db_get "${zone}/${proto}/remote-ip-address")"
249 cli_print_fmt1 3 "DNS servers" "$(db_get "${zone}/${proto}/domain-name-servers")"
250 cli_space
251 done
252
253 exit ${EXIT_OK}
254 }
255
256 hook_ppp_write_config() {
257 local zone=${1}
258 assert isset zone
259
260 local file=${2}
261 assert isset file
262
263 # Read in the configuration files.
264 zone_settings_read "${zone}"
265
266 # A port has to be assigned for this action
267 local port=$(__hook_get_port "${zone}")
268 if ! isset port; then
269 error "No port assigned to pppoe hook of zone '${zone}'"
270 exit ${EXIT_ERROR}
271 fi
272
273 # Prepare the command line options for the pppoe plugin.
274 local plugin_options
275
276 # Add the access concentrator (if any).
277 if isset ACCESS_CONCENTRATOR; then
278 plugin_options="${plugin_options} rp_pppoe_ac '${ACCESS_CONCENTRATOR}'"
279 fi
280
281 # Add the service name (if any).
282 if isset SERVICE_NAME; then
283 plugin_options="${plugin_options} rp_pppoe_service '${SERVICE_NAME}'"
284 fi
285
286 # The last argument must be the interface.
287 plugin_options="${plugin_options} ${port}"
288
289 pppd_write_config ${file} \
290 --interface="${zone}" \
291 --username="${USERNAME}" \
292 --password="${PASSWORD}" \
293 --mtu="${MTU}" \
294 --auth="${AUTH}" \
295 --ipv6="${IPV6}" \
296 \
297 --plugin="${PPPOE_PLUGIN}" \
298 --plugin-options="${plugin_options}"
299
300 exit ${EXIT_OK}
301 }
302
303 __hook_get_port() {
304 local zone="${1}"
305
306 local port
307 for port in $(zone_get_ports "${zone}"); do
308 echo "${port}"
309 return ${EXIT_OK}
310 done
311
312 return ${EXIT_ERROR}
313 }
314
315 hook_port_attach() {
316 # Excepting at least two arguments here
317 assert [ $# -ge 2 ]
318
319 local zone="${1}"
320 local port="${2}"
321 shift 2
322
323 # PPPoE can only use one port
324 local ports_num="$(zone_get_ports_num "${zone}")"
325 if [ ${ports_num} -ge 1 ]; then
326 local ports="$(zone_get_ports "${zone}")"
327 error "The pppoe zone hook only supports assigning one port"
328 error " port '${ports}' has already been assigned to zone '${zone}'"
329 return ${EXIT_ERROR}
330 fi
331
332 if ! zone_port_settings_write "${zone}" "${port}"; then
333 exit ${EXIT_ERROR}
334 fi
335
336 exit ${EXIT_OK}
337 }
338
339 hook_port_detach() {
340 assert [ $# -eq 2 ]
341
342 local zone="${1}"
343 local port="${2}"
344
345 # Shut down the entire zone here, because it cannot
346 # run without a port any way and removing the port would
347 # create a hotplug event which will be processed after the
348 # port has already been detached...
349 zone_stop "${zone}"
350
351 if ! zone_port_settings_remove "${zone}" "${port}"; then
352 exit ${EXIT_ERROR}
353 fi
354
355 exit ${EXIT_OK}
356 }
357
358 hook_port_up() {
359 assert [ $# -eq 2 ]
360
361 local zone="${1}"
362 local port="${2}"
363
364 # Try bringing up the port if it has not been brought up before
365 if ! device_exists "${port}"; then
366 port_create "${port}"
367 fi
368
369 # Make sure that the port is up
370 port_up "${port}"
371
372 exit ${EXIT_OK}
373 }
374
375 hook_port_down() {
376 assert [ $# -eq 2 ]
377
378 local zone="${1}"
379 local port="${2}"
380
381 if device_exists "${port}"; then
382 port_down "${port}"
383 fi
384
385 exit ${EXIT_OK}
386 }
387
388 hook_ppp_ipv6_up() {
389 local zone="${1}"
390
391 ppp_common_ipv6_up "${zone}"
392
393 # Read configuration
394 zone_settings_read "${zone}"
395
396 if enabled PREFIX_DELEGATION; then
397 dhclient_start "${zone}" ipv6
398 fi
399
400 exit ${EXIT_OK}
401 }
402
403 hook_ppp_ipv6_down() {
404 local zone="${1}"
405
406 ppp_common_ipv6_down "${zone}"
407
408 # Read configuration
409 zone_settings_read "${zone}"
410
411 if enabled PREFIX_DELEGATION; then
412 dhclient_stop "${zone}" ipv6
413 fi
414
415 exit ${EXIT_OK}
416 }