]>
Commit | Line | Data |
---|---|---|
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 | ||
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 | ||
1848564d MT |
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 | |
5b20e43a | 140 | # Naming options |
1848564d MT |
141 | ifname ${zone} |
142 | name ${LINKNAME} | |
143 | linkname ${LINKNAME} | |
5b20e43a | 144 | |
1848564d | 145 | plugin ${PPPOE_PLUGIN} $(_pppoe_real_device) |
5b20e43a MT |
146 | |
147 | # User configuration | |
148 | user ${USER} | |
149 | ||
1848564d MT |
150 | $(enabled PEERDNS && echo "usepeerdns") |
151 | $(enabled DEFAULTROUTE && echo "defaultroute") | |
5b20e43a MT |
152 | |
153 | noauth | |
1848564d | 154 | $(isset AUTH && echo "require-${AUTH}") |
5b20e43a MT |
155 | |
156 | noipdefault | |
157 | ||
158 | # Maximum transmission/receive unit | |
159 | mtu ${MTU} | |
160 | mru ${MTU} | |
161 | ||
162 | # Disable the compression | |
1848564d | 163 | noccp noaccomp nodeflate nopcomp novj novjccomp nobsdcomp nomppe |
5b20e43a | 164 | |
71c6b131 | 165 | updetach debug |
5b20e43a | 166 | EOF |
5b20e43a | 167 | |
1848564d MT |
168 | pppd file ${RED_RUN}/${LINKNAME}/options >/dev/null |
169 | ||
da453c33 MT |
170 | local ret=$? |
171 | ||
172 | # Get exit code from ppp daemon and handle it: | |
173 | case "${ret}" in | |
174 | 0) | |
175 | log DEBUG "pppd detached successfully" | |
176 | exit ${EXIT_OK} | |
177 | ;; | |
178 | esac | |
179 | ||
180 | error_log "pppd exited with unknown exit code '${ret}'" | |
181 | ||
182 | exit ${EXIT_ERROR} | |
1848564d MT |
183 | } |
184 | ||
185 | function _down() { | |
186 | local zone=${1} | |
187 | shift | |
188 | ||
189 | config_read ${ZONE_DIR}/${zone}/settings | |
190 | ||
1848564d MT |
191 | # Kill pppd |
192 | pid=$(cat /var/run/${zone}.pid 2>/dev/null) | |
193 | if [ -n "${pid}" ]; then | |
194 | kill ${pid} &>/dev/null | |
195 | fi | |
196 | ||
197 | # Pull down device or remove virtual one | |
198 | if [ -n "${DEVICE_VID}" ]; then | |
199 | device_remove_virtual ${DEVICE_MAC} | |
200 | else | |
201 | device_set_down ${DEVICE} | |
202 | fi | |
203 | ||
204 | exit ${EXIT_OK} | |
205 | } | |
206 | ||
207 | function _discover() { | |
208 | local device=${1} | |
209 | ||
210 | if [ "$(device_get_type ${device})" != "real" ]; then | |
5b20e43a | 211 | exit ${EXIT_ERROR} |
1848564d MT |
212 | fi |
213 | ||
214 | local output | |
215 | output=$(pppoe-discovery -I ${device} -U $(uuid) 2>&1) | |
216 | ||
217 | # Exit if there was not output | |
218 | [ -z "${output}" ] && exit ${DISCOVER_ERROR} | |
219 | ||
220 | # Exit if PADI timed out | |
221 | grep -q "Timeout" <<<${output} && exit ${DISCOVER_ERROR} | |
222 | ||
223 | local ac | |
224 | while read line; do | |
225 | case "${line}" in | |
226 | Access-Concentrator:*) | |
227 | ac="${line#Access-Concentrator: }" | |
228 | ;; | |
229 | esac | |
230 | done <<<"${output}" | |
231 | ||
232 | echo "ACCESS_CONCENTRATOR=\"$ac\"" | |
233 | ||
234 | exit ${DISCOVER_OK} | |
235 | } | |
5b20e43a | 236 | |
1848564d | 237 | run $@ |