]> git.ipfire.org Git - people/stevee/network.git/blob - src/functions/functions.ports
5bed4d8bf8739a5c1353a88150daa4a9f535b615
[people/stevee/network.git] / src / functions / functions.ports
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 function port_dir() {
23 echo "${NETWORK_CONFIG_DIR}/ports"
24 }
25
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
40 function port_settings_read() {
41 local port="${1}"
42 assert isset port
43 shift
44
45 # Save the HOOK variable.
46 local hook="${HOOK}"
47
48 settings_read "$(port_file "${port}")" "$@"
49
50 # Restore hook.
51 HOOK="${hook}"
52 }
53
54 function port_settings_write() {
55 local port="${1}"
56 assert isset port
57 shift
58
59 settings_write "$(port_file "${port}")" "$@"
60 }
61
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
72 function port_file() {
73 local port="${1}"
74 assert isset port
75
76 echo "$(port_dir)/${port}"
77 }
78
79 function port_exists() {
80 local port=${1}
81
82 [ -f "${NETWORK_CONFIG_DIR}/ports/${port}" ]
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
114 function port_new() {
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
137 hook_exec port ${hook} new $@
138 }
139
140 function port_destroy() {
141 local port=${1}
142
143 assert isset port
144
145 port_exists ${port} || return ${EXIT_OK}
146
147 # Check if the port is attached to any zone and don't delete it.
148 local ok=${EXIT_OK}
149
150 local attached_zone=$(port_is_attached ${port})
151 if [ -n "${attached_zone}" ]; then
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
175 return ${EXIT_ERROR}
176 fi
177
178 port_down ${port}
179
180 rm -f $(port_file ${port})
181 }
182
183 function port_create() {
184 port_cmd "create" $@
185 }
186
187 function port_remove() {
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}"
197 }
198
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
220 function port_info() {
221 port_cmd info $@
222 }
223
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 }
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 }
248
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}"
261 return ${EXIT_OK}
262 fi
263 i=$(( ${i} + 1 ))
264 done
265
266 return ${EXIT_ERROR}
267 }
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 }
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 }
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 }