]> git.ipfire.org Git - people/arne_f/network.git/blame - hooks/pppoe
network: Update codebase.
[people/arne_f/network.git] / hooks / pppoe
CommitLineData
1848564d
MT
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
26HOOK_SETTINGS="HOOK AUTH LINKNAME USER SECRET PEERDNS DEFAULTROUTE MTU"
27HOOK_SETTINGS="${HOOK_SETTINGS} DEVICE DEVICE_VID DEVICE_TYPE"
28
29AUTH=
30DEFAULTROUTE=1
31LINKNAME="$(uuid)"
32MTU=1492
33PEERDNS=1
34SECRET=
35USER=
36
37PPPOE_ALLOWED_AUTHS="chap pap"
38PPPOE_PLUGIN="rp-pppoe.so"
39
40function _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
51function _check() {
52 assert isset USER
53 assert isset SECRET
54 assert isset LINKNAME
55 assert isset DEFAULTROUTE
56 assert isset PEERDNS
57 assert isset DEVICE
58 assert isset DEVICE_TYPE
59
60 assert isbool DEFAULTROUTE
61 assert isbool PEERDNS
62 assert ismac DEVICE
63 assert isoneof DEVICE_TYPE real virtual
64
65 isset AUTH && assert isoneof AUTH ${PPPOE_ALLOWED_AUTHS}
66 isset DEVICE_ID && assert isinteger DEVICE_VID
67}
68
69function _parse_cmdline() {
70 while [ $# -gt 0 ]; do
71 case "$1" in
72 --user=*)
73 USER=${1#--user=}
74 ;;
75 --secret=*)
76 SECRET=${1#--secret=}
77 ;;
78 --linkname=*)
79 LINKNAME=${1#--name=}
80 ;;
81 --mtu=*)
82 MTU=${1#--mtu=}
83 ;;
84 --no-defaultroute)
85 DEFAULTROUTE=0
86 ;;
87 --no-dns)
88 PEERDNS=0
89 ;;
90 --auth=*)
91 AUTH=${1#--auth=}
92 ;;
93 --device=*)
94 DEVICE=${1#--device=}
95 ;;
96 --device-vid=*)
97 DEVICE_VID=${1#--device-vid=}
98 ;;
99 *)
100 echo "Unknown option: $1" >&2
101 exit ${EXIT_ERROR}
102 ;;
103 esac
104 shift
105 done
106
107 if ! device_exists $(devicify ${DEVICE}); then
108 error "Device '${DEVICE}' does not exist."
109 exit ${EXIT_ERROR}
110 fi
111
112 DEVICE=$(macify ${DEVICE})
113
114 if isset DEVICE_VID; then
115 DEVICE_TYPE="virtual"
116 else
117 DEVICE_TYPE="real"
118 fi
119}
120
121function _up() {
122 local zone=${1}
123 shift
124
125 config_read ${ZONE_DIR}/${zone}/settings
126
127 ppp_pre_up
128
129 # Creating necessary files
130 [ -d "${RED_RUN}/${LINKNAME}" ] || mkdir -p ${RED_RUN}/${LINKNAME}
131
132 # Setting up the device
133 if [ -n "${DEVICE_VID}" ]; then
134 device_create_virtual ${DEVICE} ${DEVICE_VID} ${DEVICE_MAC}
135 else
136 device_set_up ${DEVICE}
137 fi
138
139 ppp_secret "${USER}" "${SECRET}"
140
141 cat <<EOF >${RED_RUN}/${LINKNAME}/options
5b20e43a 142# Naming options
1848564d
MT
143ifname ${zone}
144name ${LINKNAME}
145linkname ${LINKNAME}
5b20e43a 146
1848564d 147plugin ${PPPOE_PLUGIN} $(_pppoe_real_device)
5b20e43a
MT
148
149# User configuration
150user ${USER}
151
1848564d
MT
152$(enabled PEERDNS && echo "usepeerdns")
153$(enabled DEFAULTROUTE && echo "defaultroute")
5b20e43a
MT
154
155noauth
1848564d 156$(isset AUTH && echo "require-${AUTH}")
5b20e43a
MT
157
158noipdefault
159
160# Maximum transmission/receive unit
161mtu ${MTU}
162mru ${MTU}
163
164# Disable the compression
1848564d 165noccp noaccomp nodeflate nopcomp novj novjccomp nobsdcomp nomppe
5b20e43a
MT
166
167debug
168EOF
5b20e43a 169
1848564d
MT
170 pppd file ${RED_RUN}/${LINKNAME}/options >/dev/null
171
172 exit ${EXIT_OK}
173}
174
175function _down() {
176 local zone=${1}
177 shift
178
179 config_read ${ZONE_DIR}/${zone}/settings
180
181 ppp_pre_down
182
183 # Kill pppd
184 pid=$(cat /var/run/${zone}.pid 2>/dev/null)
185 if [ -n "${pid}" ]; then
186 kill ${pid} &>/dev/null
187 fi
188
189 # Pull down device or remove virtual one
190 if [ -n "${DEVICE_VID}" ]; then
191 device_remove_virtual ${DEVICE_MAC}
192 else
193 device_set_down ${DEVICE}
194 fi
195
196 exit ${EXIT_OK}
197}
198
199function _discover() {
200 local device=${1}
201
202 if [ "$(device_get_type ${device})" != "real" ]; then
5b20e43a 203 exit ${EXIT_ERROR}
1848564d
MT
204 fi
205
206 local output
207 output=$(pppoe-discovery -I ${device} -U $(uuid) 2>&1)
208
209 # Exit if there was not output
210 [ -z "${output}" ] && exit ${DISCOVER_ERROR}
211
212 # Exit if PADI timed out
213 grep -q "Timeout" <<<${output} && exit ${DISCOVER_ERROR}
214
215 local ac
216 while read line; do
217 case "${line}" in
218 Access-Concentrator:*)
219 ac="${line#Access-Concentrator: }"
220 ;;
221 esac
222 done <<<"${output}"
223
224 echo "ACCESS_CONCENTRATOR=\"$ac\""
225
226 exit ${DISCOVER_OK}
227}
5b20e43a 228
1848564d 229run $@