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 # If no 8021q module has been loaded into the kernel,
144 # we cannot do anything.
145 [ -r "/proc/net/vlan/config" ] ||
return ${EXIT_OK}
147 local dev spacer1 id spacer2 parent
148 while read dev spacer1 id spacer2 parent
; do
149 [ "${parent}" = "${device}" ] && echo "${dev}"
150 done < /proc
/net
/vlan
/config |
sort
153 # Check if the device is a ppp device
154 function device_is_ppp
() {
157 local type=$
(__device_get_file
${device} type)
159 [ "${type}" = "512" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
162 # Check if the device is a pointopoint device.
163 function device_is_ptp() {
166 device_has_flag ${device} 0x10
169 # Check if the device is a loopback device
170 function device_is_loopback() {
173 [ "${device}" = "lo
" ]
176 # Check if the device is a wireless device
177 function device_is_wireless() {
180 [ -d "${SYS_CLASS_NET}/${device}/phy80211
" ]
183 function device_is_serial() {
187 # Check if the device is a physical network interface
188 function device_is_ethernet() {
191 device_is_loopback ${device} && \
194 device_is_bonding ${device} && \
197 device_is_bridge ${device} && \
200 device_is_ppp ${device} && \
203 device_is_virtual ${device} && \
206 [ "$
(__device_get_file
${device} type)" != "1" ] && \
212 # Get the device type
213 function device_get_type() {
216 if device_is_virtual ${device}; then
219 elif device_is_bonding ${device}; then
222 elif device_is_bridge ${device}; then
225 elif device_is_ppp ${device}; then
228 elif device_is_loopback ${device}; then
231 elif device_is_wireless ${device}; then
234 elif device_is_ethernet ${device}; then
237 elif device_is_serial ${device}; then
245 function device_get_status() {
249 local status=${STATUS_DOWN}
251 if device_is_up ${device}; then
254 if ! device_has_carrier ${device}; then
255 status=${STATUS_NOCARRIER}
262 function device_get_address() {
265 cat ${SYS_CLASS_NET}/${device}/address 2>/dev/null
268 function device_set_address() {
272 if ! device_exists ${device}; then
273 error "Device
'${device}' does not exist.
"
277 log INFO "Setting address of
'${device}' to '${addr}' - was $(device_get_address ${device})."
280 if device_is_up ${device}; then
281 device_set_down ${device}
285 ip link set ${device} address ${addr}
288 if [ "${up}" = "1" ]; then
289 device_set_up ${device}
292 if [ "${ret}" != "0" ]; then
293 error_log "Could not set address '${addr}' on device '${device}'."
299 function device_get() {
303 for device in ${SYS_CLASS_NET}/*; do
304 device=$(basename ${device})
306 # bonding_masters is no device
307 [ "${device}" = "bonding_masters" ] && continue
309 devices="${devices} ${device}"
316 function devices_get_all() {
320 # Check if a device has a cable plugged in
321 function device_has_carrier() {
325 local carrier=$(__device_get_file ${device} carrier)
326 [ "${carrier}" = "1" ]
329 function device_is_promisc() {
332 device_has_flag ${device} 0x200
335 function device_set_promisc() {
339 assert device_exists ${device}
341 assert isoneof state on off
343 ip link set ${device} promisc ${state}
346 # Check if the device is free
347 function device_is_free() {
351 # Check if the device is used
352 function device_is_used() {
355 device_has_virtuals ${device} && \
357 device_is_bonded ${device} && \
359 device_is_bridge_attached ${device} && \
365 function device_hash() {
368 # Get mac address of device and remove all colons (:)
369 # that will result in a hash.
370 device=$(macify ${device})
375 # Give the device a new name
376 function device_set_name() {
378 local destination=${2}
380 # Check if devices exists
381 if ! device_exists ${source} || device_exists ${destination}; then
386 if device_is_up ${source}; then
387 ip link set ${source} down
391 ip link set ${source} name ${destination}
393 if [ "${up}" = "1" ]; then
394 ip link set ${destination} up
399 function device_set_up() {
402 # Silently fail if device was not found
403 [ -z "${device}" ] && return ${EXIT_ERROR}
405 # Do nothing if device is already up
406 device_is_up ${device} && return ${EXIT_OK}
408 device_set_parent_up ${device}
410 log DEBUG "Setting up device '${device}'"
412 ip link set ${device} up
415 function device_set_parent_up() {
419 if device_is_virtual ${device}; then
420 parent=$(virtual_get_parent ${device})
422 device_is_up ${parent} && return ${EXIT_OK}
424 log DEBUG "Setting up parent device '${parent}' of '${device}'"
426 device_set_up ${parent}
434 function device_set_down() {
440 if device_is_up ${device}; then
441 log DEBUG "Tearing down device '${device}'"
443 ip link set ${device} down
447 device_set_parent_down ${device}
452 function device_set_parent_down() {
456 if device_is_virtual ${device}; then
457 parent=$(virtual_get_parent ${device})
459 device_is_up ${parent} || return ${EXIT_OK}
461 if device_is_free ${parent}; then
462 log DEBUG "Tearing down parent device '${parent}' of '${device}'"
464 device_set_down ${parent}
471 function device_get_mtu() {
474 if ! device_exists ${device}; then
475 error "Device '${device}' does not exist."
479 echo $(<${SYS_CLASS_NET}/${device}/mtu)
482 # Set mtu to a device
483 function device_set_mtu() {
487 if ! device_exists ${device}; then
488 error "Device '${device}' does not exist."
492 local oldmtu=$(device_get_mtu ${device})
494 if [ "${oldmtu}" = "${mtu}" ]; then
495 # No need to set mtu.
499 log INFO "Setting mtu of '${device}' to '${mtu}' - was ${oldmtu}.
"
502 if device_is_up ${device}; then
503 device_set_down ${device}
507 ip link set ${device} mtu ${mtu}
510 if [ "${up}" = "1" ]; then
511 device_set_up ${device}
514 if [ "${ret}" != "0" ]; then
515 error_log "Could not
set mtu
'${mtu}' on device
'${device}'.
"
521 function device_discover() {
524 log INFO "Running discovery process on device
'${device}'.
"
527 for hook in $(hook_zone_get_all); do
528 hook_zone_exec ${hook} discover ${device}
532 function device_has_ip() {
537 assert device_exists ${device}
539 # IPv6 addresses must be fully imploded
540 local protocol=$(ip_detect_protocol ${addr})
541 case "${protocol}" in
543 addr=$(ipv6_implode ${addr})
547 listmatch ${addr} $(device_get_addresses ${device})
550 function device_get_addresses() {
553 assert device_exists ${device}
558 ip addr show ${device} | \
559 while read prot addr line; do
560 [ "${prot:0:4}" = "inet
" ] && echo "${addr}"
564 function __device_get_file() {
571 local path="${SYS_CLASS_NET}/${device}/${file}"
572 [ -r "${path}" ] || return ${EXIT_ERROR}
577 function device_get_rx_bytes() {
580 __device_get_file ${device} statistics/rx_bytes
583 function device_get_tx_bytes() {
586 __device_get_file ${device} statistics/tx_bytes
589 function device_get_rx_packets() {
592 __device_get_file ${device} statistics/rx_packets
595 function device_get_tx_packets() {
598 __device_get_file ${device} statistics/tx_packets
601 function device_get_rx_errors() {
604 __device_get_file ${device} statistics/rx_errors
607 function device_get_tx_errors() {
610 __device_get_file ${device} statistics/tx_errors
613 function device_get_speed() {
616 __device_get_file ${device} speed
619 function device_get_duplex() {
622 __device_get_file ${device} duplex