]>
Commit | Line | Data |
---|---|---|
711ffac1 | 1 | #!/bin/bash |
1578dae9 MT |
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 | ############################################################################### | |
711ffac1 MT |
21 | |
22 | function port_dir() { | |
84f3bd05 | 23 | echo "${NETWORK_CONFIG_DIR}/ports" |
711ffac1 MT |
24 | } |
25 | ||
49ec20d8 MT |
26 | function port_get_hook() { |
27 | local port=${1} | |
28 | assert isset port | |
29 | ||
30 | config_get_hook $(port_file ${port}) | |
31 | } | |
32 | ||
33 | function port_config_dir() { | |
34 | local port=${1} | |
35 | ||
36 | print "${RUN_DIR}/ports/${port}" | |
37 | return ${EXIT_OK} | |
38 | } | |
39 | ||
e9df08ad MT |
40 | function port_settings_read() { |
41 | local port="${1}" | |
49ec20d8 | 42 | assert isset port |
e9df08ad | 43 | shift |
49ec20d8 MT |
44 | |
45 | # Save the HOOK variable. | |
46 | local hook="${HOOK}" | |
47 | ||
e9df08ad | 48 | settings_read "$(port_file "${port}")" "$@" |
49ec20d8 MT |
49 | |
50 | # Restore hook. | |
51 | HOOK="${hook}" | |
52 | } | |
53 | ||
e9df08ad MT |
54 | function port_settings_write() { |
55 | local port="${1}" | |
49ec20d8 | 56 | assert isset port |
e9df08ad | 57 | shift |
49ec20d8 | 58 | |
e9df08ad | 59 | settings_write "$(port_file "${port}")" "$@" |
49ec20d8 MT |
60 | } |
61 | ||
8895cf8f MT |
62 | function ports_get_all() { |
63 | local port | |
64 | ||
65 | for port in $(port_dir)/*; do | |
66 | [ -f "${port}" ] || continue | |
67 | ||
68 | basename ${port} | |
69 | done | |
70 | } | |
71 | ||
711ffac1 | 72 | function port_file() { |
e9df08ad | 73 | local port="${1}" |
711ffac1 MT |
74 | assert isset port |
75 | ||
76 | echo "$(port_dir)/${port}" | |
77 | } | |
78 | ||
79 | function port_exists() { | |
80 | local port=${1} | |
81 | ||
84f3bd05 | 82 | [ -f "${NETWORK_CONFIG_DIR}/ports/${port}" ] |
711ffac1 MT |
83 | } |
84 | ||
85 | function port_get_hook() { | |
86 | local port=${1} | |
87 | ||
88 | assert isset port | |
89 | ||
90 | config_get_hook $(port_file ${port}) | |
91 | } | |
92 | ||
93 | function port_is_attached() { | |
94 | local port=${1} | |
95 | shift | |
96 | ||
97 | assert isset port | |
98 | ||
99 | local zone | |
100 | for zone in $(zones_get_all); do | |
101 | ||
102 | assert isset zone | |
103 | assert zone_exists ${zone} | |
104 | ||
105 | if listmatch ${port} $(zone_get_ports ${zone}); then | |
106 | echo "${zone}" | |
107 | return ${EXIT_OK} | |
108 | fi | |
109 | done | |
110 | ||
111 | return ${EXIT_ERROR} | |
112 | } | |
113 | ||
1ba6a2bb | 114 | function port_new() { |
711ffac1 MT |
115 | #local port=${1} |
116 | #shift | |
117 | # | |
118 | #if port_exists ${port}; then | |
119 | # error "Port '${port}' does already exist." | |
120 | # return ${EXIT_ERROR} | |
121 | #fi | |
122 | ||
123 | local hook=${1} | |
124 | shift | |
125 | ||
126 | if ! hook_exists port ${hook}; then | |
127 | error "Port hook '${hook}' does not exist." | |
128 | return ${EXIT_ERROR} | |
129 | fi | |
130 | ||
131 | #port_edit ${port} ${hook} $@ | |
132 | # | |
133 | #if [ $? -ne ${EXIT_OK} ]; then | |
134 | # port_destroy ${port} | |
135 | #fi | |
136 | ||
1ba6a2bb | 137 | hook_exec port ${hook} new $@ |
711ffac1 MT |
138 | } |
139 | ||
140 | function port_destroy() { | |
141 | local port=${1} | |
142 | ||
143 | assert isset port | |
144 | ||
145 | port_exists ${port} || return ${EXIT_OK} | |
146 | ||
98f4dae6 MT |
147 | # Check if the port is attached to any zone and don't delete it. |
148 | local ok=${EXIT_OK} | |
711ffac1 | 149 | |
98f4dae6 | 150 | local attached_zone=$(port_is_attached ${port}) |
711ffac1 | 151 | if [ -n "${attached_zone}" ]; then |
98f4dae6 MT |
152 | error_log "Cannot destroy port '${port}' which is attached to zone '${attached_zone}'." |
153 | ok=${EXIT_ERROR} | |
154 | fi | |
155 | ||
156 | # Check if the port is linked to any other port and don't allow the user | |
157 | # to delete it. | |
158 | local other_port | |
159 | for other_port in $(ports_get); do | |
160 | [ "${other_port}" = "${port}" ] && continue | |
161 | ||
162 | if listmatch ${port} $(port_get_parents ${other_port}); then | |
163 | error_log "Cannot destroy port '${port}' which is a parent port to '${other_port}'." | |
164 | ok=${EXIT_ERROR} | |
165 | fi | |
166 | ||
167 | if listmatch ${port} $(port_get_children ${other_port}); then | |
168 | error_log "Cannot destroy port '${port}' which is child of port '${other_port}'." | |
169 | ok=${EXIT_ERROR} | |
170 | fi | |
171 | done | |
172 | ||
173 | # If ok says we are not okay --> exit | |
174 | if [ ${ok} -ne ${EXIT_OK} ]; then | |
711ffac1 MT |
175 | return ${EXIT_ERROR} |
176 | fi | |
177 | ||
178 | port_down ${port} | |
179 | ||
180 | rm -f $(port_file ${port}) | |
181 | } | |
182 | ||
1ba6a2bb MT |
183 | function port_create() { |
184 | port_cmd "create" $@ | |
185 | } | |
186 | ||
f90e550b | 187 | function port_remove() { |
1ba6a2bb MT |
188 | local port="${1}" |
189 | assert isset port | |
190 | ||
191 | # If the device is still up, we need to bring it down first. | |
192 | if device_is_up "${port}"; then | |
193 | port_down "${port}" | |
194 | fi | |
195 | ||
196 | port_cmd "remove" "${port}" | |
f90e550b MT |
197 | } |
198 | ||
711ffac1 MT |
199 | function port_edit() { |
200 | port_cmd edit $@ | |
201 | } | |
202 | ||
203 | # XXX? Compatibility function | |
204 | function port_show() { | |
205 | port_status $@ | |
206 | } | |
207 | ||
208 | function port_up() { | |
209 | port_cmd up $@ | |
210 | } | |
211 | ||
212 | function port_down() { | |
213 | port_cmd down $@ | |
214 | } | |
215 | ||
216 | function port_status() { | |
217 | port_cmd status $@ | |
218 | } | |
219 | ||
98f4dae6 MT |
220 | function port_info() { |
221 | port_cmd info $@ | |
222 | } | |
223 | ||
711ffac1 MT |
224 | function port_cmd() { |
225 | local cmd=${1} | |
226 | local port=${2} | |
227 | shift 2 | |
228 | ||
229 | assert isset cmd | |
230 | assert isset port | |
231 | ||
232 | local hook=$(port_get_hook ${port}) | |
233 | ||
234 | assert isset hook | |
235 | ||
236 | hook_exec port ${hook} ${cmd} ${port} $@ | |
237 | } | |
f90e550b MT |
238 | |
239 | function ports_get() { | |
240 | local port | |
241 | for port in $(port_dir)/*; do | |
242 | port=$(basename ${port}) | |
243 | if port_exists ${port}; then | |
244 | echo "${port}" | |
245 | fi | |
246 | done | |
247 | } | |
2ae0fb8d | 248 | |
d76f5107 MT |
249 | function port_find_free() { |
250 | local pattern=${1} | |
251 | ||
252 | assert isset pattern | |
253 | ||
254 | local port | |
255 | local i=0 | |
256 | ||
257 | while [ ${i} -lt 99 ]; do | |
258 | port=${pattern//N/${i}} | |
259 | if ! port_exists ${port} && ! device_exists ${port}; then | |
260 | echo "${port}" | |
a1a8f0f4 | 261 | return ${EXIT_OK} |
d76f5107 MT |
262 | fi |
263 | i=$(( ${i} + 1 )) | |
264 | done | |
a1a8f0f4 MT |
265 | |
266 | return ${EXIT_ERROR} | |
d76f5107 | 267 | } |
98f4dae6 MT |
268 | |
269 | function port_get_info() { | |
270 | local port=${1} | |
271 | local key=${2} | |
272 | ||
273 | assert isset port | |
274 | assert port_exists ${port} | |
275 | assert isset key | |
276 | ||
277 | ( | |
278 | eval $(port_info ${port}) | |
279 | echo "${!key}" | |
280 | ) | |
281 | } | |
282 | ||
283 | function port_get_parents() { | |
284 | local port=${1} | |
285 | ||
286 | port_get_info ${port} PORT_PARENTS | |
287 | } | |
288 | ||
289 | function port_get_children() { | |
290 | local port=${1} | |
291 | ||
292 | port_get_info ${port} PORT_CHILDREN | |
293 | } | |
3a7fef62 MT |
294 | |
295 | function port_zone() { | |
296 | # Get name of the zones, this port is configured in. | |
297 | local port=${1} | |
298 | shift | |
299 | ||
300 | assert isset port | |
301 | ||
302 | local zone | |
303 | for zone in $(zones_get_all); do | |
304 | if zone_has_port ${zone} ${port}; then | |
305 | echo "${zone}" | |
306 | return ${EXIT_OK} | |
307 | fi | |
308 | done | |
309 | ||
310 | return ${EXIT_OK} | |
311 | } | |
b8026986 MT |
312 | |
313 | function port_hotplug_event() { | |
314 | local port="${1}" | |
315 | assert isset port | |
316 | ||
317 | hotplug_assert_in_hotplug_event | |
318 | ||
319 | port_cmd "hotplug" "${port}" | |
320 | } | |
321 | ||
322 | function port_get_slaves() { | |
323 | local port="${1}" | |
324 | ||
325 | port_settings_read "${port}" \ | |
326 | --ignore-superfluous-settings SLAVES | |
327 | print "${SLAVES}" | |
328 | } | |
329 | ||
330 | function port_device_is_slave() { | |
331 | assert [ $# -eq 2 ] | |
332 | ||
333 | local port="${1}" | |
334 | local device="${2}" | |
335 | ||
336 | # Get slaves of port | |
337 | local slaves="$(port_get_slaves "${port}")" | |
338 | ||
339 | # Returns true if device is in slaves | |
340 | list_match "${device}" ${slaves} | |
341 | } | |
342 | ||
343 | function port_get_phy() { | |
344 | local port="${1}" | |
345 | ||
346 | port_settings_read "${port}" \ | |
347 | --ignore-superfluous-settings PHY | |
348 | print "${PHY}" | |
349 | } | |
350 | ||
351 | function port_uses_phy() { | |
352 | assert [ $# -eq 2 ] | |
353 | ||
354 | local port="${1}" | |
355 | local phy="${2}" | |
356 | ||
357 | # Nothing to do if an empty argument is given | |
358 | if ! isset phy; then | |
359 | return ${EXIT_FALSE} | |
360 | fi | |
361 | ||
362 | phy="$(phy_get_address "${phy}")" | |
363 | ||
364 | local port_phy="$(port_get_phy "${port}")" | |
365 | [ "${port_phy}" = "${phy}" ] | |
366 | } |