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_delete() {
80 # Nothing to do, it device does not exist.
81 device_exists ${device} || return ${EXIT_OK}
84 cmd_quiet ip link delete ${device}
87 if [ ${ret} -ne ${EXIT_OK} ]; then
88 log ERROR "device
: Could not delete device
'${device}': ${ret}"
95 function device_has_flag() {
99 local flags=$(__device_get_file ${device} flags)
101 if [[ "$
(( ${flags} & ${flag} ))" -eq 0 ]]; then
108 # Check if the device is up
109 function device_is_up() {
112 device_exists ${device} || return ${EXIT_ERROR}
114 device_has_flag ${device} 0x1
117 function device_ifindex_to_name() {
121 local device device_idx
122 for device in ${SYS_CLASS_NET}/*; do
123 device=$(basename ${device})
124 device_exists ${device} || continue
126 device_idx=$(device_get_ifindex ${device})
128 if [ "${device_idx}" = "${idx}" ]; then
137 function device_get_ifindex() {
141 local path="${SYS_CLASS_NET}/${1}/ifindex
"
143 # Check if file can be read.
144 [ -r "${path}" ] || return ${EXIT_ERROR}
149 # Check if the device is a bonding device
150 function device_is_bonding() {
151 [ -d "/sys
/class
/net
/${1}/bonding
" ]
154 # Check if the device bonded in a bonding device
155 function device_is_bonded() {
158 [ -d "${SYS_CLASS_NET}/${device}/master
" ]
161 # Check if the device is a bridge
162 function device_is_bridge() {
163 [ -d "/sys
/class
/net
/${1}/bridge
" ]
166 function device_is_bridge_attached() {
169 [ -d "${SYS_CLASS_NET}/${device}/brport
" ]
172 function device_get_bridge() {
176 # Check if device is attached to a bridge.
177 device_is_bridge_attached ${device} || return ${EXIT_ERROR}
179 local ifindex_path="${SYS_CLASS_NET}/${device}/brport
/bridge
/ifindex
"
180 [ -r "${ifindex_path}" ] || return ${EXIT_ERROR}
182 local ifindex=$(<${ifindex_path})
185 device_ifindex_to_name ${ifindex}
188 # Check if the device is a virtual device
189 function device_is_virtual() {
192 [ -e "/proc
/net
/vlan
/${device}" ]
195 # Check if the device has virtual devices
196 function device_has_virtuals() {
199 if device_is_virtual ${device}; then
203 local virtuals=$(device_get_virtuals ${device})
204 [ -n "${virtuals}" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
207 function device_get_virtuals
() {
210 # If no 8021q module has been loaded into the kernel,
211 # we cannot do anything.
212 [ -r "/proc/net/vlan/config" ] ||
return ${EXIT_OK}
214 local dev spacer1 id spacer2 parent
215 while read dev spacer1 id spacer2 parent
; do
216 [ "${parent}" = "${device}" ] && echo "${dev}"
217 done < /proc
/net
/vlan
/config |
sort
220 # Check if the device is a ppp device
221 function device_is_ppp
() {
224 local type=$
(__device_get_file
${device} type)
226 [ "${type}" = "512" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
229 # Check if the device is a pointopoint device.
230 function device_is_ptp() {
233 device_has_flag ${device} 0x10
236 # Check if the device is a loopback device
237 function device_is_loopback() {
240 [ "${device}" = "lo
" ]
243 # Check if the device is a wireless device
244 function device_is_wireless() {
247 [ -d "${SYS_CLASS_NET}/${device}/phy80211
" ]
250 function device_is_serial() {
254 # Check if the device is a physical network interface
255 function device_is_ethernet() {
258 device_is_loopback ${device} && \
261 device_is_bonding ${device} && \
264 device_is_bridge ${device} && \
267 device_is_ppp ${device} && \
270 device_is_virtual ${device} && \
273 [ "$
(__device_get_file
${device} type)" != "1" ] && \
279 # Get the device type
280 function device_get_type() {
283 if device_is_virtual ${device}; then
286 elif device_is_bonding ${device}; then
289 elif device_is_bridge ${device}; then
292 elif device_is_ppp ${device}; then
295 elif device_is_loopback ${device}; then
298 elif device_is_wireless ${device}; then
301 elif device_is_ethernet ${device}; then
304 elif device_is_serial ${device}; then
312 function device_get_status() {
316 local status=${STATUS_DOWN}
318 if device_is_up ${device}; then
321 if ! device_has_carrier ${device}; then
322 status=${STATUS_NOCARRIER}
329 function device_get_address() {
332 cat ${SYS_CLASS_NET}/${device}/address 2>/dev/null
335 function device_set_address() {
339 if ! device_exists ${device}; then
340 error "Device
'${device}' does not exist.
"
344 log INFO "Setting address of
'${device}' to '${addr}' - was $(device_get_address ${device})."
347 if device_is_up ${device}; then
348 device_set_down ${device}
352 ip link set ${device} address ${addr}
355 if [ "${up}" = "1" ]; then
356 device_set_up ${device}
359 if [ "${ret}" != "0" ]; then
360 error_log "Could not set address '${addr}' on device '${device}'."
366 function device_get() {
370 for device in ${SYS_CLASS_NET}/*; do
371 device=$(basename ${device})
373 # bonding_masters is no device
374 [ "${device}" = "bonding_masters" ] && continue
376 devices="${devices} ${device}"
383 function devices_get_all() {
387 # Check if a device has a cable plugged in
388 function device_has_carrier() {
392 local carrier=$(__device_get_file ${device} carrier)
393 [ "${carrier}" = "1" ]
396 function device_is_promisc() {
399 device_has_flag ${device} 0x200
402 function device_set_promisc() {
406 assert device_exists ${device}
408 assert isoneof state on off
410 ip link set ${device} promisc ${state}
413 # Check if the device is free
414 function device_is_free() {
418 # Check if the device is used
419 function device_is_used() {
422 device_has_virtuals ${device} && \
424 device_is_bonded ${device} && \
426 device_is_bridge_attached ${device} && \
432 function device_hash() {
435 # Get mac address of device and remove all colons (:)
436 # that will result in a hash.
437 device=$(macify ${device})
442 # Give the device a new name
443 function device_set_name() {
445 local destination=${2}
447 # Check if devices exists
448 if ! device_exists ${source} || device_exists ${destination}; then
453 if device_is_up ${source}; then
454 ip link set ${source} down
458 ip link set ${source} name ${destination}
460 if [ "${up}" = "1" ]; then
461 ip link set ${destination} up
466 function device_set_up() {
469 # Silently fail if device was not found
470 [ -z "${device}" ] && return ${EXIT_ERROR}
472 # Do nothing if device is already up
473 device_is_up ${device} && return ${EXIT_OK}
475 device_set_parent_up ${device}
477 log DEBUG "Setting up device '${device}'"
479 ip link set ${device} up
482 function device_set_parent_up() {
486 if device_is_virtual ${device}; then
487 parent=$(virtual_get_parent ${device})
489 device_is_up ${parent} && return ${EXIT_OK}
491 log DEBUG "Setting up parent device '${parent}' of '${device}'"
493 device_set_up ${parent}
501 function device_set_down() {
507 if device_is_up ${device}; then
508 log DEBUG "Tearing down device '${device}'"
510 ip link set ${device} down
514 device_set_parent_down ${device}
519 function device_set_parent_down() {
523 if device_is_virtual ${device}; then
524 parent=$(virtual_get_parent ${device})
526 device_is_up ${parent} || return ${EXIT_OK}
528 if device_is_free ${parent}; then
529 log DEBUG "Tearing down parent device '${parent}' of '${device}'"
531 device_set_down ${parent}
538 function device_get_mtu() {
541 if ! device_exists ${device}; then
542 error "Device '${device}' does not exist."
546 echo $(<${SYS_CLASS_NET}/${device}/mtu)
549 # Set mtu to a device
550 function device_set_mtu() {
554 if ! device_exists ${device}; then
555 error "Device '${device}' does not exist."
559 local oldmtu=$(device_get_mtu ${device})
561 if [ "${oldmtu}" = "${mtu}" ]; then
562 # No need to set mtu.
566 log INFO "Setting mtu of '${device}' to '${mtu}' - was ${oldmtu}.
"
569 if device_is_up ${device}; then
570 device_set_down ${device}
574 ip link set ${device} mtu ${mtu}
577 if [ "${up}" = "1" ]; then
578 device_set_up ${device}
581 if [ "${ret}" != "0" ]; then
582 error_log "Could not
set mtu
'${mtu}' on device
'${device}'.
"
588 function device_discover() {
591 log INFO "Running discovery process on device
'${device}'.
"
594 for hook in $(hook_zone_get_all); do
595 hook_zone_exec ${hook} discover ${device}
599 function device_has_ip() {
604 assert device_exists ${device}
606 # IPv6 addresses must be fully imploded
607 local protocol=$(ip_detect_protocol ${addr})
608 case "${protocol}" in
610 addr=$(ipv6_implode ${addr})
614 listmatch ${addr} $(device_get_addresses ${device})
617 function device_get_addresses() {
620 assert device_exists ${device}
625 ip addr show ${device} | \
626 while read prot addr line; do
627 [ "${prot:0:4}" = "inet
" ] && echo "${addr}"
631 function __device_get_file() {
638 local path="${SYS_CLASS_NET}/${device}/${file}"
639 [ -r "${path}" ] || return ${EXIT_ERROR}
644 function device_get_rx_bytes() {
647 __device_get_file ${device} statistics/rx_bytes
650 function device_get_tx_bytes() {
653 __device_get_file ${device} statistics/tx_bytes
656 function device_get_rx_packets() {
659 __device_get_file ${device} statistics/rx_packets
662 function device_get_tx_packets() {
665 __device_get_file ${device} statistics/tx_packets
668 function device_get_rx_errors() {
671 __device_get_file ${device} statistics/rx_errors
674 function device_get_tx_errors() {
677 __device_get_file ${device} statistics/tx_errors
680 function device_get_speed() {
683 __device_get_file ${device} speed
686 function device_get_duplex() {
689 __device_get_file ${device} duplex