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