]> git.ipfire.org Git - people/stevee/network.git/blob - src/hooks/zones/pppoe
fa3f4ed6cea12eae693c766ac39721b5221e45b5
[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 function 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 function 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 function 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 # Bring up the port.
120 port_up "${port}"
121
122 # Start the ppp daemon.
123 pppd_start ${zone}
124
125 exit ${EXIT_OK}
126 }
127
128 function hook_down() {
129 local zone=${1}
130 assert isset zone
131
132 zone_settings_read "${zone}"
133
134 # Stop the ppp daemon.
135 pppd_stop ${zone}
136
137 # Bring down the port.
138 log DEBUG "Bringing down port '${PORT}'."
139 port_down ${PORT}
140
141 exit ${EXIT_OK}
142 }
143
144 function hook_hotplug() {
145 local zone="${1}"
146
147 case "$(hotplug_action)" in
148 add)
149 if hotplug_event_interface_is_port_of_zone "${zone}"; then
150 # Bring up the zone if it is enabled but not active, yet.
151 zone_start_auto "${zone}"
152
153 exit ${EXIT_OK}
154 fi
155 ;;
156 remove)
157 # PPPoE cannot work if the ethernet device has been removed
158 if hotplug_event_interface_is_port_of_zone "${zone}"; then
159 if zone_is_active "${zone}"; then
160 zone_stop "${zone}"
161 fi
162
163 exit ${EXIT_OK}
164 fi
165 ;;
166 esac
167
168 exit ${EXIT_NOT_HANDLED}
169 }
170
171 function hook_discover() {
172 local device=${1}
173
174 # This obviously only works on ethernet (or compatible) devices
175 if ! device_is_ethernet_compatible "${device}"; then
176 exit ${EXIT_ERROR}
177 fi
178
179 local output
180 output=$(pppoe-discovery -I ${device} -U $(uuid) 2>&1)
181
182 # Exit if there was not output
183 [ -z "${output}" ] && exit ${DISCOVER_ERROR}
184
185 # Exit if PADI timed out
186 grep -q "Timeout" <<<${output} && exit ${DISCOVER_ERROR}
187
188 local ac
189 while read line; do
190 case "${line}" in
191 Access-Concentrator:*)
192 ac="${line#Access-Concentrator: }"
193 ;;
194 esac
195 done <<<"${output}"
196
197 echo "ACCESS_CONCENTRATOR=\"$ac\""
198
199 exit ${DISCOVER_OK}
200 }
201
202 function hook_status() {
203 local zone=${1}
204 assert isset zone
205
206 cli_device_headline ${zone}
207
208 zone_settings_read "${zone}"
209
210 cli_headline 2 "Configuration"
211 cli_print_fmt1 2 "Username" "${USERNAME}"
212 cli_print_fmt1 2 "Password" "<hidden>"
213
214 local port=$(__hook_get_port "${zone}")
215 if isset port; then
216 cli_print_fmt1 2 "Port" "${port}"
217 fi
218 cli_space
219
220 # Exit if zone is down
221 if ! zone_is_up ${zone}; then
222 echo # Empty line
223 exit ${EXIT_ERROR}
224 fi
225
226 # XXX display time since connection started
227
228 cli_headline 2 "Point-to-Point-over-Ethernet protocol"
229 local proto
230 for proto in ${IP_SUPPORTED_PROTOCOLS}; do
231 routing_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" "$(routing_db_get ${zone} ${proto} local-ip-address)"
248 cli_print_fmt1 3 "Gateway" "$(routing_db_get ${zone} ${proto} remote-ip-address)"
249 cli_print_fmt1 3 "DNS servers" "$(routing_db_get ${zone} ${proto} dns)"
250 cli_space
251 cli_print_fmt1 3 "MAC-Remote" "$(routing_db_get ${zone} ${proto} remote-address)"
252 cli_space
253 done
254
255 exit ${EXIT_OK}
256 }
257
258 function hook_ppp_write_config() {
259 local zone=${1}
260 assert isset zone
261
262 local file=${2}
263 assert isset file
264
265 # Read in the configuration files.
266 zone_settings_read "${zone}"
267
268 # A port has to be assigned for this action
269 local port=$(__hook_get_port "${zone}")
270 if ! isset port; then
271 error "No port assigned to pppoe hook of zone '${zone}'"
272 exit ${EXIT_ERROR}
273 fi
274
275 # Prepare the command line options for the pppoe plugin.
276 local plugin_options
277
278 # Add the access concentrator (if any).
279 if isset ACCESS_CONCENTRATOR; then
280 plugin_options="${plugin_options} rp_pppoe_ac '${ACCESS_CONCENTRATOR}'"
281 fi
282
283 # Add the service name (if any).
284 if isset SERVICE_NAME; then
285 plugin_options="${plugin_options} rp_pppoe_service '${SERVICE_NAME}'"
286 fi
287
288 # The last argument must be the interface.
289 plugin_options="${plugin_options} ${port}"
290
291 pppd_write_config ${file} \
292 --interface="${zone}" \
293 --username="${USERNAME}" \
294 --password="${PASSWORD}" \
295 --mtu="${MTU}" \
296 --auth="${AUTH}" \
297 --ipv6="${IPV6}" \
298 \
299 --plugin="${PPPOE_PLUGIN}" \
300 --plugin-options="${plugin_options}"
301
302 exit ${EXIT_OK}
303 }
304
305 function __hook_get_port() {
306 local zone="${1}"
307
308 local port
309 for port in $(zone_get_ports "${zone}"); do
310 echo "${port}"
311 return ${EXIT_OK}
312 done
313
314 return ${EXIT_ERROR}
315 }
316
317 function hook_port_attach() {
318 # Excepting at least two arguments here
319 assert [ $# -ge 2 ]
320
321 local zone="${1}"
322 local port="${2}"
323 shift 2
324
325 # PPPoE can only use one port
326 local ports_num="$(zone_get_ports_num "${zone}")"
327 if [ ${ports_num} -ge 1 ]; then
328 local ports="$(zone_get_ports "${zone}")"
329 error "The pppoe zone hook only supports assigning one port"
330 error " port '${ports}' has already been assigned to zone '${zone}'"
331 return ${EXIT_ERROR}
332 fi
333
334 if ! zone_port_settings_write "${zone}" "${port}"; then
335 exit ${EXIT_ERROR}
336 fi
337
338 exit ${EXIT_OK}
339 }
340
341 function hook_port_detach() {
342 assert [ $# -eq 2 ]
343
344 local zone="${1}"
345 local port="${2}"
346
347 # Shut down the entire zone here, because it cannot
348 # run without a port any way and removing the port would
349 # create a hotplug event which will be processed after the
350 # port has already been detached...
351 zone_stop "${zone}"
352
353 if ! zone_port_settings_remove "${zone}" "${port}"; then
354 exit ${EXIT_ERROR}
355 fi
356
357 exit ${EXIT_OK}
358 }
359
360 function hook_ppp_ipv6_up() {
361 local zone="${1}"
362
363 ppp_common_ipv6_up "${zone}"
364
365 # Read configuration
366 zone_settings_read "${zone}"
367
368 if enabled PREFIX_DELEGATION; then
369 dhclient_start "${zone}" ipv6
370 fi
371
372 exit ${EXIT_OK}
373 }
374
375 function hook_ppp_ipv6_down() {
376 local zone="${1}"
377
378 ppp_common_ipv6_down "${zone}"
379
380 # Read configuration
381 zone_settings_read "${zone}"
382
383 if enabled PREFIX_DELEGATION; then
384 dhclient_stop "${zone}" ipv6
385 fi
386
387 exit ${EXIT_OK}
388 }