]> git.ipfire.org Git - people/stevee/network.git/blob - hooks/zones/pppoe
network: Make two groups of hooks, again.
[people/stevee/network.git] / hooks / zones / pppoe
1 #!/bin/bash
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2010 Michael Tremer & Christian Schmidt #
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 . /lib/network/header-zone
23
24 # TODO AC name, service name, sync?
25
26 HOOK_SETTINGS="HOOK AUTH LINKNAME USER SECRET PEERDNS DEFAULTROUTE MTU"
27 HOOK_SETTINGS="${HOOK_SETTINGS} DEVICE DEVICE_VID DEVICE_TYPE"
28
29 AUTH=
30 DEFAULTROUTE=1
31 LINKNAME="$(uuid)"
32 MTU=1492
33 PEERDNS=1
34 SECRET=
35 USER=
36
37 PPPOE_ALLOWED_AUTHS="chap pap"
38 PPPOE_PLUGIN="rp-pppoe.so"
39
40 function _pppoe_real_device() {
41 local device
42 if [ -n "${DEVICE_VID}" ]; then
43 device="${DEVICE_MAC}"
44 else
45 device="${DEVICE}"
46 fi
47
48 devicify ${device}
49 }
50
51 function pppd_pid() {
52 local zone=${1}
53 shift
54
55 cat /var/run/${zone}.pid 2>/dev/null
56 }
57
58 function _check() {
59 assert isset USER
60 assert isset SECRET
61 assert isset LINKNAME
62 assert isset DEFAULTROUTE
63 assert isset PEERDNS
64 assert isset DEVICE
65 assert isset DEVICE_TYPE
66
67 assert isbool DEFAULTROUTE
68 assert isbool PEERDNS
69 assert ismac DEVICE
70 assert isoneof DEVICE_TYPE real virtual
71
72 isset AUTH && assert isoneof AUTH ${PPPOE_ALLOWED_AUTHS}
73 isset DEVICE_ID && assert isinteger DEVICE_VID
74 }
75
76 function _parse_cmdline() {
77 while [ $# -gt 0 ]; do
78 case "$1" in
79 --user=*)
80 USER=${1#--user=}
81 ;;
82 --secret=*)
83 SECRET=${1#--secret=}
84 ;;
85 --linkname=*)
86 LINKNAME=${1#--name=}
87 ;;
88 --mtu=*)
89 MTU=${1#--mtu=}
90 ;;
91 --no-defaultroute)
92 DEFAULTROUTE=0
93 ;;
94 --no-dns)
95 PEERDNS=0
96 ;;
97 --auth=*)
98 AUTH=${1#--auth=}
99 ;;
100 --device=*)
101 DEVICE=${1#--device=}
102 ;;
103 --device-vid=*)
104 DEVICE_VID=${1#--device-vid=}
105 ;;
106 *)
107 echo "Unknown option: $1" >&2
108 exit ${EXIT_ERROR}
109 ;;
110 esac
111 shift
112 done
113
114 if ! device_exists $(devicify ${DEVICE}); then
115 error "Device '${DEVICE}' does not exist."
116 exit ${EXIT_ERROR}
117 fi
118
119 DEVICE=$(macify ${DEVICE})
120
121 if isset DEVICE_VID; then
122 DEVICE_TYPE="virtual"
123 else
124 DEVICE_TYPE="real"
125 fi
126 }
127
128 function _up() {
129 local zone=${1}
130 shift
131
132 config_read ${ZONE_DIR}/${zone}/settings
133
134 # Creating necessary files
135 [ -d "${RED_RUN}/${LINKNAME}" ] || mkdir -p ${RED_RUN}/${LINKNAME}
136
137 # Setting up the device
138 if [ -n "${DEVICE_VID}" ]; then
139 device_create_virtual ${DEVICE} ${DEVICE_VID} ${DEVICE_MAC}
140 else
141 device_set_up ${DEVICE}
142 fi
143
144 ppp_secret "${USER}" "${SECRET}"
145
146 cat <<EOF >${RED_RUN}/${LINKNAME}/options
147 # Naming options
148 ifname ${zone}
149 name ${LINKNAME}
150 linkname ${LINKNAME}
151
152 plugin ${PPPOE_PLUGIN} $(_pppoe_real_device)
153
154 # User configuration
155 user ${USER}
156
157 $(enabled PEERDNS && echo "usepeerdns")
158 $(enabled DEFAULTROUTE && echo "defaultroute")
159
160 noauth
161 $(isset AUTH && echo "require-${AUTH}")
162
163 noipdefault
164
165 # Maximum transmission/receive unit
166 mtu ${MTU}
167 mru ${MTU}
168
169 # Disable the compression
170 noccp noaccomp nodeflate nopcomp novj novjccomp nobsdcomp nomppe
171
172 updetach debug
173 EOF
174
175 pppd file ${RED_RUN}/${LINKNAME}/options >/dev/null
176
177 local ret=$?
178
179 # Get exit code from ppp daemon and handle it:
180 case "${ret}" in
181 0)
182 log DEBUG "pppd detached successfully"
183 exit ${EXIT_OK}
184 ;;
185 esac
186
187 error_log "pppd exited with unknown exit code '${ret}'"
188
189 exit ${EXIT_ERROR}
190 }
191
192 function _down() {
193 local zone=${1}
194 shift
195
196 config_read ${ZONE_DIR}/${zone}/settings
197
198 # Kill pppd
199 kill $(pppd_pid ${zone}) &>/dev/null
200
201 # Pull down device or remove virtual one
202 if [ -n "${DEVICE_VID}" ]; then
203 device_remove_virtual ${DEVICE_MAC}
204 else
205 device_set_down ${DEVICE}
206 fi
207
208 exit ${EXIT_OK}
209 }
210
211 function _discover() {
212 local device=${1}
213
214 if [ "$(device_get_type ${device})" != "real" ]; then
215 exit ${EXIT_ERROR}
216 fi
217
218 local output
219 output=$(pppoe-discovery -I ${device} -U $(uuid) 2>&1)
220
221 # Exit if there was not output
222 [ -z "${output}" ] && exit ${DISCOVER_ERROR}
223
224 # Exit if PADI timed out
225 grep -q "Timeout" <<<${output} && exit ${DISCOVER_ERROR}
226
227 local ac
228 while read line; do
229 case "${line}" in
230 Access-Concentrator:*)
231 ac="${line#Access-Concentrator: }"
232 ;;
233 esac
234 done <<<"${output}"
235
236 echo "ACCESS_CONCENTRATOR=\"$ac\""
237
238 exit ${DISCOVER_OK}
239 }
240
241 function _status() {
242 local zone=${1}
243
244 cli_status_headline ${zone}
245
246 # Exit if zone is down
247 if ! zone_is_up ${zone}; then
248 echo # Empty line
249 exit ${EXIT_ERROR}
250 fi
251
252 cli_headline " Point-to-Point-over-Ethernet protocol:"
253 echo " IP-Address : $(red_db_get ${zone} local-ip-address)"
254 echo " Gateway : $(red_db_get ${zone} remote-ip-address)"
255 echo " DNS-Server : $(red_db_get ${zone} dns)"
256 echo
257 echo " MAC-Remote : $(red_db_get ${zone} remote-address)"
258 echo
259 echo " MTU : $(device_get_mtu ${zone})"
260 echo # Empty line
261 exit ${EXIT_OK}
262 }
263
264 run $@