]> git.ipfire.org Git - people/stevee/network.git/blob - src/hooks/zones/pppoe
pppoe: Allow hotplugging the port and better handle pppd errors
[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="false"
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 zone_settings_read "${zone}"
109
110 # Bring up the port.
111 local port=$(__hook_get_port "${zone}")
112 port_up "${port}"
113
114 # Start the ppp daemon.
115 pppd_start ${zone}
116
117 exit ${EXIT_OK}
118 }
119
120 function hook_down() {
121 local zone=${1}
122 assert isset zone
123
124 zone_settings_read "${zone}"
125
126 # Stop the ppp daemon.
127 pppd_stop ${zone}
128
129 # Bring down the port.
130 log DEBUG "Bringing down port '${PORT}'."
131 port_down ${PORT}
132
133 exit ${EXIT_OK}
134 }
135
136 function hook_hotplug() {
137 local zone="${1}"
138
139 case "$(hotplug_action)" in
140 add)
141 if hotplug_event_interface_is_port_of_zone "${zone}"; then
142 # Bring up the zone if it is enabled but not active, yet.
143 zone_start_auto "${zone}"
144
145 exit ${EXIT_OK}
146 fi
147 ;;
148 remove)
149 # PPPoE cannot work if the ethernet device has been removed
150 if hotplug_event_interface_is_port_of_zone "${zone}"; then
151 if zone_is_active "${zone}"; then
152 zone_stop "${zone}"
153 fi
154
155 exit ${EXIT_OK}
156 fi
157 ;;
158 esac
159
160 exit ${EXIT_NOT_HANDLED}
161 }
162
163 function hook_discover() {
164 local device=${1}
165
166 # This obviously only works on ethernet (or compatible) devices
167 if ! device_is_ethernet_compatible "${device}"; then
168 exit ${EXIT_ERROR}
169 fi
170
171 local output
172 output=$(pppoe-discovery -I ${device} -U $(uuid) 2>&1)
173
174 # Exit if there was not output
175 [ -z "${output}" ] && exit ${DISCOVER_ERROR}
176
177 # Exit if PADI timed out
178 grep -q "Timeout" <<<${output} && exit ${DISCOVER_ERROR}
179
180 local ac
181 while read line; do
182 case "${line}" in
183 Access-Concentrator:*)
184 ac="${line#Access-Concentrator: }"
185 ;;
186 esac
187 done <<<"${output}"
188
189 echo "ACCESS_CONCENTRATOR=\"$ac\""
190
191 exit ${DISCOVER_OK}
192 }
193
194 function hook_status() {
195 local zone=${1}
196 assert isset zone
197
198 cli_device_headline ${zone}
199
200 zone_settings_read "${zone}"
201
202 cli_headline 2 "Configuration"
203 cli_print_fmt1 2 "Username" "${USERNAME}"
204 cli_print_fmt1 2 "Password" "<hidden>"
205
206 local port=$(__hook_get_port "${zone}")
207 if isset port; then
208 cli_print_fmt1 2 "Port" "${port}"
209 fi
210 cli_space
211
212 # Exit if zone is down
213 if ! zone_is_up ${zone}; then
214 echo # Empty line
215 exit ${EXIT_ERROR}
216 fi
217
218 # XXX display time since connection started
219
220 cli_headline 2 "Point-to-Point-over-Ethernet protocol"
221 local proto
222 for proto in ${IP_SUPPORTED_PROTOCOLS}; do
223 routing_db_exists ${zone} ${proto} || continue
224
225 local headline
226 case "${proto}" in
227 ipv6)
228 headline="Internet Protocol Version 6"
229 ;;
230 ipv4)
231 headline="Internet Protocol Version 4"
232 ;;
233 *)
234 headline="Unkown protocol"
235 ;;
236 esac
237 cli_headline 3 "${headline}"
238
239 cli_print_fmt1 3 "IP address" "$(routing_db_get ${zone} ${proto} local-ip-address)"
240 cli_print_fmt1 3 "Gateway" "$(routing_db_get ${zone} ${proto} remote-ip-address)"
241 cli_print_fmt1 3 "DNS servers" "$(routing_db_get ${zone} ${proto} dns)"
242 cli_space
243 cli_print_fmt1 3 "MAC-Remote" "$(routing_db_get ${zone} ${proto} remote-address)"
244 cli_space
245 done
246
247 exit ${EXIT_OK}
248 }
249
250 function hook_ppp_write_config() {
251 local zone=${1}
252 assert isset zone
253
254 local file=${2}
255 assert isset file
256
257 # Read in the configuration files.
258 zone_settings_read "${zone}"
259
260 # A port has to be assigned for this action
261 local port=$(__hook_get_port "${zone}")
262 if ! isset port; then
263 error "No port assigned to pppoe hook of zone '${zone}'"
264 exit ${EXIT_ERROR}
265 fi
266
267 # Prepare the command line options for the pppoe plugin.
268 local plugin_options
269
270 # Add the access concentrator (if any).
271 if isset ACCESS_CONCENTRATOR; then
272 plugin_options="${plugin_options} rp_pppoe_ac '${ACCESS_CONCENTRATOR}'"
273 fi
274
275 # Add the service name (if any).
276 if isset SERVICE_NAME; then
277 plugin_options="${plugin_options} rp_pppoe_service '${SERVICE_NAME}'"
278 fi
279
280 # The last argument must be the interface.
281 plugin_options="${plugin_options} ${port}"
282
283 pppd_write_config ${file} \
284 --interface="${zone}" \
285 --username="${USERNAME}" \
286 --password="${PASSWORD}" \
287 --mtu="${MTU}" \
288 --auth="${AUTH}" \
289 --ipv6="${IPV6}" \
290 \
291 --plugin="${PPPOE_PLUGIN}" \
292 --plugin-options="${plugin_options}"
293
294 exit ${EXIT_OK}
295 }
296
297 function __hook_get_port() {
298 local zone="${1}"
299
300 local port
301 for port in $(zone_get_ports "${zone}"); do
302 echo "${port}"
303 return ${EXIT_OK}
304 done
305
306 return ${EXIT_ERROR}
307 }
308
309 function hook_port_add() {
310 # Excepting at least two arguments here
311 assert [ $# -ge 2 ]
312
313 local zone="${1}"
314 local port="${2}"
315 shift 2
316
317 # PPPoE can only use one port
318 local ports_num="$(zone_get_ports_num "${zone}")"
319 if [ ${ports_num} -ge 1 ]; then
320 local port=$(__hook_get_port "${zone}")
321 error "The pppoe zone hook only supports assigning one port"
322 error " port '${port}' has already been assigned to zone '${zone}'"
323 return ${EXIT_ERROR}
324 fi
325
326 zone_port_settings_write "${zone}" "${port}"
327 log INFO "Port '${port}' has been added to zone '${zone}'"
328
329 exit ${EXIT_OK}
330 }
331
332 function hook_port_remove() {
333 assert [ $# -eq 2 ]
334
335 local zone="${1}"
336 local port="${2}"
337
338 # Shut down the port (if possible)
339 port_down "${port}"
340
341 log INFO "Port '${port}' has been removed from zone '${zone}'"
342 zone_port_settings_remove "${zone}" "${port}"
343
344 exit ${EXIT_OK}
345 }