2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2010 Michael Tremer & Christian Schmidt #
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. #
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. #
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/>. #
20 ###############################################################################
25 if device_exists
${device}; then
31 for d
in $
(devices_get_all
); do
32 if [ "$(device_get_address ${d})" = "${device}" ]; then
44 if mac_is_valid
${device}; then
49 if device_exists
${device}; then
50 device_get_address
${device}
57 # Check if the device exists
58 function device_exists
() {
61 # If device name was not found, exit.
62 [ -n "${device}" ] ||
return ${EXIT_ERROR}
64 [ -d "${SYS_CLASS_NET}/${device}" ]
67 # Check if the device is up
68 function device_is_up
() {
71 device_exists
${device} ||
return ${EXIT_ERROR}
73 ip link show
${device} 2>/dev
/null |
grep -qE "<.*UP.*>"
76 # Check if the device is a bonding device
77 function device_is_bonding
() {
78 [ -d "/sys/class/net/${1}/bonding" ]
81 # Check if the device bonded in a bonding device
82 function device_is_bonded
() {
84 for dev
in /sys
/class
/net
/*; do
86 [ -d "${dev}" ] ||
continue
88 # Continue if not a bonding device
89 device_is_bonding
"${dev##*/}" ||
continue
91 if grep -q "\<${1}\>" ${dev}/bonding
/slaves
; then
99 # Check if the device is a bridge
100 function device_is_bridge
() {
101 [ -d "/sys/class/net/${1}/bridge" ]
104 # Check if the device is a virtual device
105 function device_is_virtual
() {
108 [ -e "/proc/net/vlan/${device}" ]
111 # Check if the device has virtual devices
112 function device_has_virtuals
() {
113 if [ ! -e "/proc/net/vlan/config" ]; then
116 grep -q "${1}$" /proc
/net
/vlan
/config
119 function device_is_vlan
() { # XXX Compat function
120 log DEBUG
"Deprecated function device_is_vlan() was used."
125 # Check if the device is a ppp device
126 function device_is_ppp
() {
129 ip link show
${device} 2>/dev
/null |
grep -qE "<.*POINTOPOINT.*>"
132 # Check if the device is a loopback device
133 function device_is_loopback
() {
134 local device
=$
(devicify
${1})
135 [ "${device}" = "lo" ]
138 # Check if the device is a physical network interface
139 function device_is_real
() {
142 device_is_loopback
${device} && \
145 device_is_bonding
${device} && \
148 device_is_bridge
${device} && \
151 device_is_ppp
${device} && \
154 device_is_virtual
${device} && \
160 # Get the device type
161 function device_get_type
() {
162 local device
=$
(devicify
${1})
164 if device_is_vlan
${device}; then
167 elif device_is_bonding
${device}; then
170 elif device_is_bridge
${device}; then
173 elif device_is_ppp
${device}; then
176 elif device_is_loopback
${device}; then
179 elif device_is_real
${device}; then
187 function device_get_address
() {
190 cat ${SYS_CLASS_NET}/${device}/address
2>/dev
/null
193 function device_set_address
() {
197 if ! device_exists
${device}; then
198 error
"Device '${device}' does not exist."
202 log INFO
"Setting address of '${device}' to '${addr}' - was $(device_get_address ${device})."
205 if device_is_up
${device}; then
206 device_set_down
${device}
210 ip link
set ${device} address
${addr}
213 if [ "${up}" = "1" ]; then
214 device_set_up
${device}
217 if [ "${ret}" != "0" ]; then
218 error_log
"Could not set address '${addr}' on device '${device}'."
224 function devices_get_all
() {
226 for device
in ${SYS_CLASS_NET}/*; do
227 echo "$(basename ${device})"
231 # Check if a device has a cable plugged in
232 function device_has_carrier
() {
233 local device
=$
(devicify
${1})
234 [ "$(<${SYS_CLASS_NET}/${device}/carrier)" = "1" ]
237 # Check if the device is free
238 function device_is_free
() {
241 device_is_used
${device} && \
247 # Check if the device is used
248 function device_is_used
() {
249 local device
=$
(devicify
${1})
251 device_has_vlans
${device} && \
253 device_is_bonded
${device} && \
259 # XXX to be removed I think
260 function device_get_free
() {
261 local destination
=${1}
263 # Replace + by a valid number
264 if grep -q "+$" <<<${destination}; then
266 destination
=$
(sed -e "s/+//" <<<$destination)
267 while [ "${number}" -le "100" ]; do
268 if ! device_exists
"${destination}${number}"; then
269 destination
="${destination}${number}"
272 number
=$
(($number + 1))
275 echo "${destination}"
278 function device_rename
() {
279 warning_log
"Called deprecated function 'device_rename'"
284 function device_hash
() {
287 macify
${device} |
tr -d ':'
290 # Give the device a new name
291 function device_set_name
() {
293 local destination
=$
(device_get_free
${2})
295 # Check if devices exists
296 if ! device_exists
${source} || device_exists
${destination}; then
301 if device_is_up
${source}; then
302 ip link
set ${source} down
306 ip link
set ${source} name
${destination}
308 if [ "${up}" = "1" ]; then
309 ip link
set ${destination} up
314 function device_set_up
() {
315 local device
=$
(devicify
${1})
317 # Do nothing if device is already up
318 device_is_up
${device} && return ${EXIT_OK}
320 log DEBUG
"Setting up device $@"
321 ip link
set ${device} up
325 function device_set_down
() {
326 local device
=$
(devicify
${1})
328 # Do nothing if device is not up
329 device_is_up
${device} ||
return ${EXIT_OK}
331 log DEBUG
"Tearing down device $@"
332 ip link
set ${device} down
335 # Set new address to a device
336 function device_set_mac
() {
337 warning_log
"Called deprecated function 'device_set_mac'"
339 device_set_address $@
342 function device_get_mtu
() {
345 if ! device_exists
${device}; then
346 error
"Device '${device}' does not exist."
350 cat ${SYS_CLASS_NET}/${device}/mtu
353 # Set mtu to a device
354 function device_set_mtu
() {
358 if ! device_exists
${device}; then
359 error
"Device '${device}' does not exist."
363 local oldmtu
=$
(device_get_mtu
${device})
365 if [ "${oldmtu}" = "${mtu}" ]; then
366 # No need to set mtu.
370 log INFO
"Setting mtu of '${device}' to '${mtu}' - was ${oldmtu}."
373 if device_is_up
${device}; then
374 device_set_down
${device}
378 ip link
set ${device} mtu
${mtu}
381 if [ "${up}" = "1" ]; then
382 device_set_up
${device}
385 if [ "${ret}" != "0" ]; then
386 error_log
"Could not set mtu '${mtu}' on device '${device}'."
392 function device_discover
() {
395 log INFO
"Running discovery process on device '${device}'."
398 for hook
in $
(hooks_get_all
); do
399 hook_exec
${hook} discover
${device}
403 function device_create_virtual
() {
404 log WARN
"Called deprecated function device_create_virtual"
405 device_virtual_create $@
408 function device_virtual_create
() {
409 local port
=$
(devicify
${1})
412 local newport
=${port}v
${vid}
414 if [ -z "${mac}" ]; then
418 log INFO
"Creating virtual device '${newport}' with address '${mac}'."
420 # Bring up the parent device
421 # XXX Do we need this here?
422 #device_set_up ${port}
424 vconfig set_name_type DEV_PLUS_VID_NO_PAD
>/dev
/null
425 vconfig add
${port} ${vid} >/dev
/null
427 if [ $?
-ne ${EXIT_OK} ]; then
428 error_log
"Could not create virtual device '${newport}'."
432 # The device is expected to be named like ${port}.${vid}
433 # and will be renamed to the virtual schema
434 device_set_name
${port}.${vid} ${newport}
436 if [ $?
-ne ${EXIT_OK} ]; then
437 error_log
"Could not set name of virtual device '${newport}'."
441 # Setting new mac address
442 device_set_address
${newport} ${mac}
444 if [ $?
-ne ${EXIT_OK} ]; then
445 error_log
"Could not set address '${mac}' to virtual device '${newport}'."
449 # Bring up the new device
450 device_set_up
${newport}
455 function device_virtual_remove
() {
456 local device
=$
(devicify
${1})
458 log INFO
"Removing virtual device '${device}' with address '$(macify ${devive})'."
460 device_set_down
${device}
462 vconfig rem
${device} >/dev
/null
464 if [ $?
-ne ${EXIT_OK} ]; then
465 error_log
"Could not remote virtual device '${newport}'."
472 function device_bonding_create
() {
476 [ -z "${mac}" ] && mac
=$
(mac_generate
)
478 log INFO
"Creating bonding device '${device}' (${mac})."
480 echo "+${device}" > /sys
/class
/net
/bonding_masters
481 device_set_mac
${mac}
482 device_set_up
${device}
485 function device_bonding_remove
() {
486 local device
=$
(devicify
${1})
488 log INFO
"Remove bonding device '${device}'."
490 device_set_down
${device}
491 echo "-${device}" > /sys
/class
/net
/bonding_masters
494 function bonding_set_mode
() {
498 log INFO
"Setting bonding mode on '${device}' '${mode}'."
500 echo "${mode}" > /sys
/class
/net
/${device}/bonding
/mode
503 function bonding_enslave_device
() {
504 local device
=$
(devicify
${1})
505 local slave
=$
(devicify
${2})
508 log INFO
"Enslaving slave '${slave}' to '${device}'."
510 device_set_down
${slave}
511 echo "+${slave}" > /sys
/class
/net
/${device}/bonding
/slaves
514 function bridge_attach_device
() {
518 if ! device_exists
${bridge}; then
519 error
"Bridge '${bridge}' does not exist."
523 if ! device_exists
${device}; then
524 error
"Device '${device}' does not exist."
528 log INFO
"Attaching device '${device}' to bridge '${bridge}'."
530 # XXX device_set_up ${device} # Do we need this here?
532 brctl addif
${bridge} ${device}
535 function bridge_detach_device
() {
539 if ! device_exists
${bridge}; then
540 error
"Bridge '${bridge}' does not exist."
544 if ! device_exists
${device}; then
545 error
"Device '${device}' does not exist."
549 log INFO
"Detaching device '${device}' from bridge '${bridge}'."
551 brctl delif
${bridge} ${device}
553 device_set_down
${device}
556 function bridge_is_forwarding
() {
560 bridge_has_carrier
${zone} ||
return ${EXIT_ERROR}
563 while [ ${seconds} -gt 0 ]; do
564 for device
in ${SYS_CLASS_NET}/${zone}/brif
/*; do
565 [ -e "${device}/state" ] ||
continue
566 if [ "$(<${device}/state)" = "3" ]; then
571 seconds
=$
((${seconds} - 1))
577 function bridge_has_carrier
() {
580 local has_carrier
=${EXIT_ERROR}
583 for device
in ${SYS_CLASS_NET}/${zone}/brif
/*; do
584 device
=$
(basename ${device})
585 device_exists
${device} ||
continue
587 device_has_carrier
${device} && has_carrier
=${EXIT_OK}
590 return ${has_carrier}
593 function device_has_ipv4
() {
597 if ! device_exists
${device}; then
598 error
"Device '${device}' does not exist."
602 ip addr show
${device} |
grep -q -e "inet " -e "${addr}"