]> git.ipfire.org Git - people/arne_f/network.git/blob - hooks/pppoe
network: Remove some unused functions.
[people/arne_f/network.git] / hooks / 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 _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
69 function _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
121 function _up() {
122 local zone=${1}
123 shift
124
125 config_read ${ZONE_DIR}/${zone}/settings
126
127 # Creating necessary files
128 [ -d "${RED_RUN}/${LINKNAME}" ] || mkdir -p ${RED_RUN}/${LINKNAME}
129
130 # Setting up the device
131 if [ -n "${DEVICE_VID}" ]; then
132 device_create_virtual ${DEVICE} ${DEVICE_VID} ${DEVICE_MAC}
133 else
134 device_set_up ${DEVICE}
135 fi
136
137 ppp_secret "${USER}" "${SECRET}"
138
139 cat <<EOF >${RED_RUN}/${LINKNAME}/options
140 # Naming options
141 ifname ${zone}
142 name ${LINKNAME}
143 linkname ${LINKNAME}
144
145 plugin ${PPPOE_PLUGIN} $(_pppoe_real_device)
146
147 # User configuration
148 user ${USER}
149
150 $(enabled PEERDNS && echo "usepeerdns")
151 $(enabled DEFAULTROUTE && echo "defaultroute")
152
153 noauth
154 $(isset AUTH && echo "require-${AUTH}")
155
156 noipdefault
157
158 # Maximum transmission/receive unit
159 mtu ${MTU}
160 mru ${MTU}
161
162 # Disable the compression
163 noccp noaccomp nodeflate nopcomp novj novjccomp nobsdcomp nomppe
164
165 updetach debug
166 EOF
167
168 pppd file ${RED_RUN}/${LINKNAME}/options >/dev/null
169
170 exit ${EXIT_OK}
171 }
172
173 function _down() {
174 local zone=${1}
175 shift
176
177 config_read ${ZONE_DIR}/${zone}/settings
178
179 # Kill pppd
180 pid=$(cat /var/run/${zone}.pid 2>/dev/null)
181 if [ -n "${pid}" ]; then
182 kill ${pid} &>/dev/null
183 fi
184
185 # Pull down device or remove virtual one
186 if [ -n "${DEVICE_VID}" ]; then
187 device_remove_virtual ${DEVICE_MAC}
188 else
189 device_set_down ${DEVICE}
190 fi
191
192 exit ${EXIT_OK}
193 }
194
195 function _discover() {
196 local device=${1}
197
198 if [ "$(device_get_type ${device})" != "real" ]; then
199 exit ${EXIT_ERROR}
200 fi
201
202 local output
203 output=$(pppoe-discovery -I ${device} -U $(uuid) 2>&1)
204
205 # Exit if there was not output
206 [ -z "${output}" ] && exit ${DISCOVER_ERROR}
207
208 # Exit if PADI timed out
209 grep -q "Timeout" <<<${output} && exit ${DISCOVER_ERROR}
210
211 local ac
212 while read line; do
213 case "${line}" in
214 Access-Concentrator:*)
215 ac="${line#Access-Concentrator: }"
216 ;;
217 esac
218 done <<<"${output}"
219
220 echo "ACCESS_CONCENTRATOR=\"$ac\""
221
222 exit ${DISCOVER_OK}
223 }
224
225 run $@