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