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 ###############################################################################
27 if device_exists
${device}; then
33 for d
in $
(devices_get_all
); do
34 if [ "$(device_get_address ${d})" = "${device}" ]; then
48 if mac_is_valid
${device}; then
53 if device_exists
${device}; then
54 device_get_address
${device}
61 # Check if the device exists
62 function device_exists
() {
65 # If device name was not found, exit.
66 [ -n "${device}" ] ||
return ${EXIT_ERROR}
68 # Check for a normal network device.
69 [ -d "${SYS_CLASS_NET}/${device}" ] && return ${EXIT_OK}
71 # If the check above, did not find a result,
72 # we check for serial devices.
73 serial_exists ${device}
76 function device_has_flag() {
80 local flags=$(__device_get_file ${device} flags)
82 if [[ "$
(( ${flags} & ${flag} ))" -eq 0 ]]; then
89 # Check if the device is up
90 function device_is_up() {
93 device_exists ${device} || return ${EXIT_ERROR}
95 device_has_flag ${device} 0x1
98 # Check if the device is a bonding device
99 function device_is_bonding() {
100 [ -d "/sys
/class
/net
/${1}/bonding
" ]
103 # Check if the device bonded in a bonding device
104 function device_is_bonded() {
107 [ -d "${SYS_CLASS_NET}/${device}/master
" ]
110 # Check if the device is a bridge
111 function device_is_bridge() {
112 [ -d "/sys
/class
/net
/${1}/bridge
" ]
115 function device_is_bridge_attached() {
118 [ -d "${SYS_CLASS_NET}/${device}/brport
" ]
121 # Check if the device is a virtual device
122 function device_is_virtual() {
125 [ -e "/proc
/net
/vlan
/${device}" ]
128 # Check if the device has virtual devices
129 function device_has_virtuals() {
132 if device_is_virtual ${device}; then
136 local virtuals=$(device_get_virtuals ${device})
137 [ -n "${virtuals}" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
140 function device_get_virtuals
() {
143 local dev spacer1 id spacer2 parent
144 while read dev spacer1 id spacer2 parent
; do
145 [ "${parent}" = "${device}" ] && echo "${dev}"
146 done < /proc
/net
/vlan
/config |
sort
149 # Check if the device is a ppp device
150 function device_is_ppp
() {
153 local type=$
(__device_get_file
${device} type)
155 [ "${type}" = "512" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
158 # Check if the device is a pointopoint device.
159 function device_is_ptp() {
162 device_has_flag ${device} 0x10
165 # Check if the device is a loopback device
166 function device_is_loopback() {
169 [ "${device}" = "lo
" ]
172 # Check if the device is a wireless device
173 function device_is_wireless() {
176 [ -d "${SYS_CLASS_NET}/${device}/phy80211
" ]
179 function device_is_serial() {
183 # Check if the device is a physical network interface
184 function device_is_ethernet() {
187 device_is_loopback ${device} && \
190 device_is_bonding ${device} && \
193 device_is_bridge ${device} && \
196 device_is_ppp ${device} && \
199 device_is_virtual ${device} && \
202 [ "$
(__device_get_file
${device} type)" != "1" ] && \
208 # Get the device type
209 function device_get_type() {
212 if device_is_virtual ${device}; then
215 elif device_is_bonding ${device}; then
218 elif device_is_bridge ${device}; then
221 elif device_is_ppp ${device}; then
224 elif device_is_loopback ${device}; then
227 elif device_is_wireless ${device}; then
230 elif device_is_ethernet ${device}; then
233 elif device_is_serial ${device}; then
241 function device_get_status() {
245 local status=${STATUS_DOWN}
247 if device_is_up ${device}; then
250 if ! device_has_carrier ${device}; then
251 status=${STATUS_NOCARRIER}
258 function device_get_address() {
261 cat ${SYS_CLASS_NET}/${device}/address 2>/dev/null
264 function device_set_address() {
268 if ! device_exists ${device}; then
269 error "Device
'${device}' does not exist.
"
273 log INFO "Setting address of
'${device}' to '${addr}' - was $(device_get_address ${device})."
276 if device_is_up ${device}; then
277 device_set_down ${device}
281 ip link set ${device} address ${addr}
284 if [ "${up}" = "1" ]; then
285 device_set_up ${device}
288 if [ "${ret}" != "0" ]; then
289 error_log "Could not set address '${addr}' on device '${device}'."
295 function device_get() {
299 for device in ${SYS_CLASS_NET}/*; do
300 device=$(basename ${device})
302 # bonding_masters is no device
303 [ "${device}" = "bonding_masters" ] && continue
305 devices="${devices} ${device}"
312 function devices_get_all() {
316 # Check if a device has a cable plugged in
317 function device_has_carrier() {
321 local carrier=$(__device_get_file ${device} carrier)
322 [ "${carrier}" = "1" ]
325 function device_is_promisc() {
328 device_has_flag ${device} 0x200
331 function device_set_promisc() {
335 assert device_exists ${device}
337 assert isoneof state on off
339 ip link set ${device} promisc ${state}
342 # Check if the device is free
343 function device_is_free() {
347 # Check if the device is used
348 function device_is_used() {
351 device_has_virtuals ${device} && \
353 device_is_bonded ${device} && \
355 device_is_bridge_attached ${device} && \
361 function device_hash() {
364 # Get mac address of device and remove all colons (:)
365 # that will result in a hash.
366 device=$(macify ${device})
371 # Give the device a new name
372 function device_set_name() {
374 local destination=${2}
376 # Check if devices exists
377 if ! device_exists ${source} || device_exists ${destination}; then
382 if device_is_up ${source}; then
383 ip link set ${source} down
387 ip link set ${source} name ${destination}
389 if [ "${up}" = "1" ]; then
390 ip link set ${destination} up
395 function device_set_up() {
398 # Silently fail if device was not found
399 [ -z "${device}" ] && return ${EXIT_ERROR}
401 # Do nothing if device is already up
402 device_is_up ${device} && return ${EXIT_OK}
404 device_set_parent_up ${device}
406 log DEBUG "Setting up device '${device}'"
408 ip link set ${device} up
411 function device_set_parent_up() {
415 if device_is_virtual ${device}; then
416 parent=$(virtual_get_parent ${device})
418 device_is_up ${parent} && return ${EXIT_OK}
420 log DEBUG "Setting up parent device '${parent}' of '${device}'"
422 device_set_up ${parent}
430 function device_set_down() {
436 if device_is_up ${device}; then
437 log DEBUG "Tearing down device '${device}'"
439 ip link set ${device} down
443 device_set_parent_down ${device}
448 function device_set_parent_down() {
452 if device_is_virtual ${device}; then
453 parent=$(virtual_get_parent ${device})
455 device_is_up ${parent} || return ${EXIT_OK}
457 if device_is_free ${parent}; then
458 log DEBUG "Tearing down parent device '${parent}' of '${device}'"
460 device_set_down ${parent}
467 function device_get_mtu() {
470 if ! device_exists ${device}; then
471 error "Device '${device}' does not exist."
475 echo $(<${SYS_CLASS_NET}/${device}/mtu)
478 # Set mtu to a device
479 function device_set_mtu() {
483 if ! device_exists ${device}; then
484 error "Device '${device}' does not exist."
488 local oldmtu=$(device_get_mtu ${device})
490 if [ "${oldmtu}" = "${mtu}" ]; then
491 # No need to set mtu.
495 log INFO "Setting mtu of '${device}' to '${mtu}' - was ${oldmtu}.
"
498 if device_is_up ${device}; then
499 device_set_down ${device}
503 ip link set ${device} mtu ${mtu}
506 if [ "${up}" = "1" ]; then
507 device_set_up ${device}
510 if [ "${ret}" != "0" ]; then
511 error_log "Could not
set mtu
'${mtu}' on device
'${device}'.
"
517 function device_discover() {
520 log INFO "Running discovery process on device
'${device}'.
"
523 for hook in $(hook_zone_get_all); do
524 hook_zone_exec ${hook} discover ${device}
528 function device_has_ip() {
533 assert device_exists ${device}
535 # IPv6 addresses must be fully imploded
536 local protocol=$(ip_detect_protocol ${addr})
537 case "${protocol}" in
539 addr=$(ipv6_implode ${addr})
543 listmatch ${addr} $(device_get_addresses ${device})
546 function device_get_addresses() {
549 assert device_exists ${device}
554 ip addr show ${device} | \
555 while read prot addr line; do
556 [ "${prot:0:4}" = "inet
" ] && echo "${addr}"
560 function __device_get_file() {
567 local path="${SYS_CLASS_NET}/${device}/${file}"
568 [ -r "${path}" ] || return ${EXIT_ERROR}
573 function device_get_rx_bytes() {
576 __device_get_file ${device} statistics/rx_bytes
579 function device_get_tx_bytes() {
582 __device_get_file ${device} statistics/tx_bytes
585 function device_get_rx_packets() {
588 __device_get_file ${device} statistics/rx_packets
591 function device_get_tx_packets() {
594 __device_get_file ${device} statistics/tx_packets
597 function device_get_rx_errors() {
600 __device_get_file ${device} statistics/rx_errors
603 function device_get_tx_errors() {
606 __device_get_file ${device} statistics/tx_errors
609 function device_get_speed() {
612 __device_get_file ${device} speed
615 function device_get_duplex() {
618 __device_get_file ${device} duplex