]>
| 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 | 21 | |
| 8707df4b MT |
22 | ports_get_all() { |
| 23 | local port | |
| 24 | for port in $(list_directory "${NETWORK_PORTS_DIR}"); do | |
| 25 | if port_exists "${port}"; then | |
| 26 | print "${port}" | |
| 27 | fi | |
| 28 | done | |
| 29 | } | |
| 30 | ||
| 31 | # XXX TO BE REMOVED | |
| 1c6a4e30 | 32 | port_list() { |
| 8707df4b | 33 | ports_get_all "$@" |
| bae37360 MT |
34 | } |
| 35 | ||
| 1c6a4e30 | 36 | port_list_in_use() { |
| bae37360 MT |
37 | local ports_in_use |
| 38 | ||
| 39 | # Collect all ports that are attached to a zone | |
| 40 | local zone | |
| 41 | for zone in $(zones_get_all); do | |
| 42 | list_append ports_in_use $(zone_get_ports "${zone}") | |
| 43 | done | |
| 44 | ||
| 45 | # Collect all ports that are enslaved by an other port | |
| 46 | local port | |
| 47 | for port in $(port_list); do | |
| 48 | list_append ports_in_use $(port_get_slaves "${port}") | |
| 49 | done | |
| 50 | ||
| 51 | list_sort ${ports_in_use} | |
| 52 | } | |
| 53 | ||
| 1c6a4e30 | 54 | port_list_free() { |
| bae37360 MT |
55 | local ports_in_use="$(port_list_in_use)" |
| 56 | ||
| 57 | local port | |
| 58 | for port in $(port_list); do | |
| 59 | if ! list_match "${port}" ${ports_in_use}; then | |
| 60 | print "${port}" | |
| 61 | fi | |
| 62 | done | |
| 63 | ||
| 64 | return ${EXIT_OK} | |
| 65 | } | |
| 66 | ||
| 1c6a4e30 | 67 | port_get_hook() { |
| 49ec20d8 MT |
68 | local port=${1} |
| 69 | assert isset port | |
| 70 | ||
| 71 | config_get_hook $(port_file ${port}) | |
| 72 | } | |
| 73 | ||
| 1c6a4e30 | 74 | port_config_dir() { |
| 49ec20d8 MT |
75 | local port=${1} |
| 76 | ||
| 77 | print "${RUN_DIR}/ports/${port}" | |
| 78 | return ${EXIT_OK} | |
| 79 | } | |
| 80 | ||
| 1c6a4e30 | 81 | port_settings_read() { |
| e9df08ad | 82 | local port="${1}" |
| 49ec20d8 MT |
83 | assert isset port |
| 84 | ||
| 85 | # Save the HOOK variable. | |
| 86 | local hook="${HOOK}" | |
| 87 | ||
| d389e96b | 88 | settings_read "$(port_file "${port}")" ${HOOK_SETTINGS[*]} |
| 49ec20d8 MT |
89 | |
| 90 | # Restore hook. | |
| 91 | HOOK="${hook}" | |
| 92 | } | |
| 93 | ||
| 1c6a4e30 | 94 | port_settings_write() { |
| e9df08ad | 95 | local port="${1}" |
| 49ec20d8 | 96 | assert isset port |
| 1e6f187e | 97 | |
| 227d458f MT |
98 | settings_write "$(port_file "${port}")" \ |
| 99 | --check="hook_check_settings" HOOK ${HOOK_SETTINGS[*]} | |
| 49ec20d8 MT |
100 | } |
| 101 | ||
| 1c6a4e30 | 102 | port_file() { |
| e9df08ad | 103 | local port="${1}" |
| 711ffac1 MT |
104 | assert isset port |
| 105 | ||
| 0d994c19 | 106 | echo "${NETWORK_PORTS_DIR}/${port}/settings" |
| 711ffac1 MT |
107 | } |
| 108 | ||
| 1c6a4e30 | 109 | port_exists() { |
| 711ffac1 MT |
110 | local port=${1} |
| 111 | ||
| af51d3a9 | 112 | [ -d "${NETWORK_CONFIG_DIR}/ports/${port}" ] |
| 711ffac1 MT |
113 | } |
| 114 | ||
| 1c6a4e30 | 115 | port_get_hook() { |
| 711ffac1 MT |
116 | local port=${1} |
| 117 | ||
| 118 | assert isset port | |
| 119 | ||
| 120 | config_get_hook $(port_file ${port}) | |
| 121 | } | |
| 122 | ||
| 1c6a4e30 | 123 | port_is_attached() { |
| 711ffac1 MT |
124 | local port=${1} |
| 125 | shift | |
| 126 | ||
| 127 | assert isset port | |
| 128 | ||
| 129 | local zone | |
| 130 | for zone in $(zones_get_all); do | |
| 8c9205b1 | 131 | if list_match ${port} $(zone_get_ports ${zone}); then |
| 711ffac1 MT |
132 | echo "${zone}" |
| 133 | return ${EXIT_OK} | |
| 134 | fi | |
| 135 | done | |
| 136 | ||
| 137 | return ${EXIT_ERROR} | |
| 138 | } | |
| 139 | ||
| 1c6a4e30 | 140 | port_is_up() { |
| 2212045f | 141 | device_is_up "$@" |
| abba34c1 MT |
142 | } |
| 143 | ||
| 1c6a4e30 | 144 | port_new() { |
| ed75c16d | 145 | local hook="${1}" |
| 711ffac1 MT |
146 | shift |
| 147 | ||
| ed75c16d | 148 | if ! hook_exists port "${hook}"; then |
| 711ffac1 MT |
149 | error "Port hook '${hook}' does not exist." |
| 150 | return ${EXIT_ERROR} | |
| 151 | fi | |
| 152 | ||
| 2212045f | 153 | hook_exec port "${hook}" new "$@" |
| 711ffac1 MT |
154 | } |
| 155 | ||
| 1c6a4e30 | 156 | port_destroy() { |
| 711ffac1 | 157 | local port=${1} |
| 711ffac1 MT |
158 | assert isset port |
| 159 | ||
| ab5edf53 MT |
160 | # Cannot delete a port that does not exist |
| 161 | if ! port_exists ${port}; then | |
| 162 | error "No such port: ${port}" | |
| 163 | return ${EXIT_ERROR} | |
| 164 | fi | |
| 711ffac1 | 165 | |
| 98f4dae6 | 166 | local attached_zone=$(port_is_attached ${port}) |
| 711ffac1 | 167 | if [ -n "${attached_zone}" ]; then |
| 27fff860 MT |
168 | if ! zone_port_detach "${attached_zone}" "${port}"; then |
| 169 | error "Could not remove port ${port} from zone ${zone}" | |
| 170 | return ${EXIT_ERROR} | |
| 171 | fi | |
| 98f4dae6 MT |
172 | fi |
| 173 | ||
| 174 | # Check if the port is linked to any other port and don't allow the user | |
| 175 | # to delete it. | |
| 176 | local other_port | |
| 177 | for other_port in $(ports_get); do | |
| 178 | [ "${other_port}" = "${port}" ] && continue | |
| 179 | ||
| 8c9205b1 | 180 | if list_match ${port} $(port_get_children ${other_port}); then |
| 7e2b4356 MT |
181 | log ERROR "Cannot destroy port '${port}' which is child of port '${other_port}'." |
| 182 | return ${EXIT_ERROR} | |
| 98f4dae6 MT |
183 | fi |
| 184 | done | |
| 185 | ||
| 54c790c2 MT |
186 | # Shut down the port before destroying it |
| 187 | if ! port_remove "${port}"; then | |
| 188 | return ${EXIT_ERROR} | |
| 189 | fi | |
| 711ffac1 | 190 | |
| 8707df4b MT |
191 | if ! rm -rf "${NETWORK_PORTS_DIR}/${port}"; then |
| 192 | log ERROR "Could not destroy port ${port}" | |
| 193 | return ${EXIT_ERROR} | |
| 194 | fi | |
| 195 | ||
| 196 | log INFO "Destroyed port ${port}" | |
| 197 | return ${EXIT_OK} | |
| 711ffac1 MT |
198 | } |
| 199 | ||
| 1c6a4e30 | 200 | port_create() { |
| 2212045f | 201 | port_cmd "create" "$@" |
| 1ba6a2bb MT |
202 | } |
| 203 | ||
| 1c6a4e30 | 204 | port_remove() { |
| 1ba6a2bb MT |
205 | local port="${1}" |
| 206 | assert isset port | |
| 207 | ||
| 270aab39 MT |
208 | if ! port_exists "${port}"; then |
| 209 | log ERROR "Port ${port} does not exist" | |
| 210 | return ${EXIT_ERROR} | |
| 211 | fi | |
| 212 | ||
| 1ba6a2bb MT |
213 | # If the device is still up, we need to bring it down first. |
| 214 | if device_is_up "${port}"; then | |
| 215 | port_down "${port}" | |
| 216 | fi | |
| 217 | ||
| 218 | port_cmd "remove" "${port}" | |
| f90e550b MT |
219 | } |
| 220 | ||
| 270aab39 MT |
221 | # Restarts the port by removing it and then re-creating it |
| 222 | port_restart() { | |
| 223 | local port="${1}" | |
| 224 | assert isset port | |
| 225 | ||
| 226 | port_remove "${port}" | |
| 227 | ||
| 228 | port_create "${port}" | |
| 229 | } | |
| 230 | ||
| 1c6a4e30 | 231 | port_edit() { |
| 2212045f | 232 | port_cmd edit "$@" |
| 711ffac1 MT |
233 | } |
| 234 | ||
| 1c6a4e30 | 235 | port_up() { |
| f28e991c MT |
236 | assert [ $# -eq 1 ] |
| 237 | ||
| 238 | local port="${1}" | |
| 239 | ||
| 240 | # Check if the port exists | |
| 241 | if ! device_exists "${port}"; then | |
| 242 | log ERROR "Could not bring up port ${port} which has not been created" | |
| 243 | return ${EXIT_ERROR} | |
| 244 | fi | |
| 245 | ||
| 246 | port_cmd up "${port}" | |
| 711ffac1 MT |
247 | } |
| 248 | ||
| 1c6a4e30 | 249 | port_down() { |
| 2212045f | 250 | port_cmd down "$@" |
| 711ffac1 MT |
251 | } |
| 252 | ||
| 1c6a4e30 | 253 | port_status() { |
| 2212045f | 254 | port_cmd status "$@" |
| 711ffac1 MT |
255 | } |
| 256 | ||
| 1c6a4e30 | 257 | port_cmd() { |
| 711ffac1 MT |
258 | local cmd=${1} |
| 259 | local port=${2} | |
| 260 | shift 2 | |
| 261 | ||
| 262 | assert isset cmd | |
| 263 | assert isset port | |
| 264 | ||
| 265 | local hook=$(port_get_hook ${port}) | |
| 266 | ||
| 0c949dd5 MT |
267 | # Abort if we could not find a hook |
| 268 | if ! isset hook; then | |
| 269 | log CRITICAL "Port ${port} does not have a hook associated with it" | |
| 270 | return ${EXIT_ERROR} | |
| 271 | fi | |
| 711ffac1 | 272 | |
| 2212045f | 273 | hook_exec port ${hook} ${cmd} ${port} "$@" |
| 711ffac1 | 274 | } |
| f90e550b | 275 | |
| 1c6a4e30 | 276 | ports_get() { |
| f90e550b | 277 | local port |
| 0d994c19 | 278 | for port in $(list_directory "${NETWORK_PORTS_DIR}"); do |
| f90e550b MT |
279 | if port_exists ${port}; then |
| 280 | echo "${port}" | |
| 281 | fi | |
| 282 | done | |
| 283 | } | |
| 2ae0fb8d | 284 | |
| 1c6a4e30 | 285 | port_find_free() { |
| d76f5107 MT |
286 | local pattern=${1} |
| 287 | ||
| 288 | assert isset pattern | |
| 289 | ||
| 290 | local port | |
| 291 | local i=0 | |
| 292 | ||
| 293 | while [ ${i} -lt 99 ]; do | |
| 294 | port=${pattern//N/${i}} | |
| 295 | if ! port_exists ${port} && ! device_exists ${port}; then | |
| 296 | echo "${port}" | |
| a1a8f0f4 | 297 | return ${EXIT_OK} |
| d76f5107 MT |
298 | fi |
| 299 | i=$(( ${i} + 1 )) | |
| 300 | done | |
| a1a8f0f4 MT |
301 | |
| 302 | return ${EXIT_ERROR} | |
| d76f5107 | 303 | } |
| 98f4dae6 | 304 | |
| 1c6a4e30 | 305 | port_get_children() { |
| 98f4dae6 MT |
306 | local port=${1} |
| 307 | ||
| 7da8cf02 MT |
308 | assert port_exists "${port}" |
| 309 | ||
| 310 | port_cmd "children" "${port}" | |
| 98f4dae6 | 311 | } |
| 3a7fef62 | 312 | |
| 1c6a4e30 | 313 | port_zone() { |
| 3a7fef62 MT |
314 | # Get name of the zones, this port is configured in. |
| 315 | local port=${1} | |
| 316 | shift | |
| 317 | ||
| 318 | assert isset port | |
| 319 | ||
| 320 | local zone | |
| 321 | for zone in $(zones_get_all); do | |
| 322 | if zone_has_port ${zone} ${port}; then | |
| 323 | echo "${zone}" | |
| 324 | return ${EXIT_OK} | |
| 325 | fi | |
| 326 | done | |
| 327 | ||
| 328 | return ${EXIT_OK} | |
| 329 | } | |
| b8026986 | 330 | |
| 1c6a4e30 | 331 | port_hotplug_event() { |
| b8026986 MT |
332 | local port="${1}" |
| 333 | assert isset port | |
| 334 | ||
| 335 | hotplug_assert_in_hotplug_event | |
| 336 | ||
| 337 | port_cmd "hotplug" "${port}" | |
| 338 | } | |
| 339 | ||
| 1c6a4e30 | 340 | port_get_slaves() { |
| b8026986 MT |
341 | local port="${1}" |
| 342 | ||
| 343 | port_settings_read "${port}" \ | |
| 344 | --ignore-superfluous-settings SLAVES | |
| 345 | print "${SLAVES}" | |
| 346 | } | |
| 347 | ||
| 1c6a4e30 | 348 | port_device_is_slave() { |
| b8026986 MT |
349 | assert [ $# -eq 2 ] |
| 350 | ||
| 351 | local port="${1}" | |
| 352 | local device="${2}" | |
| 353 | ||
| 354 | # Get slaves of port | |
| 355 | local slaves="$(port_get_slaves "${port}")" | |
| 356 | ||
| 357 | # Returns true if device is in slaves | |
| 358 | list_match "${device}" ${slaves} | |
| 359 | } | |
| 360 | ||
| 1c6a4e30 | 361 | port_get_phy() { |
| b8026986 MT |
362 | local port="${1}" |
| 363 | ||
| 364 | port_settings_read "${port}" \ | |
| 365 | --ignore-superfluous-settings PHY | |
| 366 | print "${PHY}" | |
| 367 | } | |
| 368 | ||
| 1c6a4e30 | 369 | port_uses_phy() { |
| b8026986 MT |
370 | assert [ $# -eq 2 ] |
| 371 | ||
| 372 | local port="${1}" | |
| 373 | local phy="${2}" | |
| 374 | ||
| 375 | # Nothing to do if an empty argument is given | |
| 376 | if ! isset phy; then | |
| 377 | return ${EXIT_FALSE} | |
| 378 | fi | |
| 379 | ||
| 380 | phy="$(phy_get_address "${phy}")" | |
| 381 | ||
| 382 | local port_phy="$(port_get_phy "${port}")" | |
| 383 | [ "${port_phy}" = "${phy}" ] | |
| 384 | } | |
| 7ad5252c | 385 | |
| 1c6a4e30 | 386 | ports_lowest_address() { |
| 7ad5252c MT |
387 | local address |
| 388 | local addresses | |
| 389 | ||
| 390 | local port | |
| 391 | for port in $(port_list); do | |
| 392 | # Skip all ports that do not exist | |
| 393 | # any more or are not plugged in | |
| 394 | device_exists "${port}" || continue | |
| 395 | ||
| 396 | # Skip all ports that are not proper ethernet devices | |
| 397 | device_is_wireless "${port}" && continue | |
| 398 | device_is_ethernet "${port}" || continue | |
| 399 | ||
| 400 | list_append addresses "$(device_get_address "${port}")" | |
| 401 | done | |
| 402 | ||
| 403 | # Sort the list | |
| 404 | addresses="$(list_sort ${addresses})" | |
| 405 | ||
| 406 | # Get the first element which is the lowest MAC address | |
| 407 | list_head ${addresses} | |
| 408 | } | |
| f5ee091e MT |
409 | |
| 410 | port_identify() { | |
| 2212045f | 411 | device_identify "$@" |
| f5ee091e | 412 | } |
| 410d2e85 JS |
413 | |
| 414 | port_get_color() { | |
| 415 | # This function return the color of a port | |
| 416 | assert [ $# -eq 1 ] | |
| 417 | ||
| 418 | local name=${1} | |
| 419 | color_read "port" ${name} | |
| 420 | } | |
| 5de34b4c JS |
421 | |
| 422 | port_get_description_title() { | |
| 423 | assert [ $# -eq 1 ] | |
| 424 | ||
| 425 | local name=${1} | |
| 426 | description_title_read $(description_format_filename "port" "${name}") | |
| 427 | } |