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 vlan device
189 function device_is_vlan() {
193 [ -e "${PROC_NET_VLAN}/${device}" ]
196 # Check if the device has vlan devices
197 function device_has_vlans() {
201 if device_is_vlan ${device}; then
205 local vlans=$(device_get_vlans ${device})
206 [ -n "${vlans}" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
209 function device_get_vlans
() {
213 # If no 8021q module has been loaded into the kernel,
214 # we cannot do anything.
215 [ -r "${PROC_NET_VLAN_CONFIG}" ] ||
return ${EXIT_OK}
217 local dev spacer1 id spacer2 parent
218 while read dev spacer1 id spacer2 parent
; do
219 [ "${parent}" = "${device}" ] ||
continue
222 done < ${PROC_NET_VLAN_CONFIG}
225 # Check if the device is a ppp device
226 function device_is_ppp
() {
229 local type=$
(__device_get_file
${device} type)
231 [ "${type}" = "512" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
234 # Check if the device is a pointopoint device.
235 function device_is_ptp() {
238 device_has_flag ${device} 0x10
241 # Check if the device is a loopback device
242 function device_is_loopback() {
245 [ "${device}" = "lo
" ]
248 # Check if the device is a wireless device
249 function device_is_wireless() {
252 [ -d "${SYS_CLASS_NET}/${device}/phy80211
" ]
255 function device_is_serial() {
259 # Check if the device is a physical network interface
260 function device_is_ethernet() {
263 device_is_loopback ${device} && \
266 device_is_bonding ${device} && \
269 device_is_bridge ${device} && \
272 device_is_ppp ${device} && \
275 device_is_vlan ${device} && \
278 [ "$
(__device_get_file
${device} type)" != "1" ] && \
284 # Get the device type
285 function device_get_type() {
288 if device_is_vlan ${device}; then
291 elif device_is_bonding ${device}; then
294 elif device_is_bridge ${device}; then
297 elif device_is_ppp ${device}; then
300 elif device_is_loopback ${device}; then
303 elif device_is_wireless ${device}; then
306 elif device_is_ethernet ${device}; then
309 elif device_is_serial ${device}; then
317 function device_get_status() {
321 local status=${STATUS_DOWN}
323 if device_is_up ${device}; then
326 if ! device_has_carrier ${device}; then
327 status=${STATUS_NOCARRIER}
334 function device_get_address() {
337 cat ${SYS_CLASS_NET}/${device}/address 2>/dev/null
340 function device_set_address() {
344 if ! device_exists ${device}; then
345 error "Device
'${device}' does not exist.
"
349 log INFO "Setting address of
'${device}' to '${addr}' - was $(device_get_address ${device})."
352 if device_is_up ${device}; then
353 device_set_down ${device}
357 ip link set ${device} address ${addr}
360 if [ "${up}" = "1" ]; then
361 device_set_up ${device}
364 if [ "${ret}" != "0" ]; then
365 error_log "Could not set address '${addr}' on device '${device}'."
371 function device_get() {
375 for device in ${SYS_CLASS_NET}/*; do
376 device=$(basename ${device})
378 # bonding_masters is no device
379 [ "${device}" = "bonding_masters" ] && continue
381 devices="${devices} ${device}"
388 function devices_get_all() {
392 # Check if a device has a cable plugged in
393 function device_has_carrier() {
397 local carrier=$(__device_get_file ${device} carrier)
398 [ "${carrier}" = "1" ]
401 function device_is_promisc() {
404 device_has_flag ${device} 0x200
407 function device_set_promisc() {
411 assert device_exists ${device}
413 assert isoneof state on off
415 ip link set ${device} promisc ${state}
418 # Check if the device is free
419 function device_is_free() {
423 # Check if the device is used
424 function device_is_used() {
427 device_has_vlans ${device} && \
429 device_is_bonded ${device} && \
431 device_is_bridge_attached ${device} && \
437 function device_hash() {
440 # Get mac address of device and remove all colons (:)
441 # that will result in a hash.
442 device=$(macify ${device})
447 # Give the device a new name
448 function device_set_name() {
450 local destination=${2}
452 # Check if devices exists
453 if ! device_exists ${source} || device_exists ${destination}; then
458 if device_is_up ${source}; then
459 ip link set ${source} down
463 ip link set ${source} name ${destination}
465 if [ "${up}" = "1" ]; then
466 ip link set ${destination} up
471 function device_set_up() {
474 # Silently fail if device was not found
475 [ -z "${device}" ] && return ${EXIT_ERROR}
477 # Do nothing if device is already up
478 device_is_up ${device} && return ${EXIT_OK}
480 device_set_parent_up ${device}
482 log DEBUG "Setting up device '${device}'"
484 ip link set ${device} up
487 function device_set_parent_up() {
491 if device_is_vlan ${device}; then
492 parent=$(vlan_get_parent ${device})
494 device_is_up ${parent} && return ${EXIT_OK}
496 log DEBUG "Setting up parent device '${parent}' of '${device}'"
498 device_set_up ${parent}
506 function device_set_down() {
512 if device_is_up ${device}; then
513 log DEBUG "Tearing down device '${device}'"
515 ip link set ${device} down
519 device_set_parent_down ${device}
524 function device_set_parent_down() {
528 if device_is_vlan ${device}; then
529 parent=$(vlan_get_parent ${device})
531 device_is_up ${parent} || return ${EXIT_OK}
533 if device_is_free ${parent}; then
534 log DEBUG "Tearing down parent device '${parent}' of '${device}'"
536 device_set_down ${parent}
543 function device_get_mtu() {
546 if ! device_exists ${device}; then
547 error "Device '${device}' does not exist."
551 echo $(<${SYS_CLASS_NET}/${device}/mtu)
554 # Set mtu to a device
555 function device_set_mtu() {
559 if ! device_exists ${device}; then
560 error "Device '${device}' does not exist."
564 local oldmtu=$(device_get_mtu ${device})
566 if [ "${oldmtu}" = "${mtu}" ]; then
567 # No need to set mtu.
571 log INFO "Setting mtu of '${device}' to '${mtu}' - was ${oldmtu}.
"
574 if device_is_up ${device}; then
575 device_set_down ${device}
579 ip link set ${device} mtu ${mtu}
582 if [ "${up}" = "1" ]; then
583 device_set_up ${device}
586 if [ "${ret}" != "0" ]; then
587 error_log "Could not
set mtu
'${mtu}' on device
'${device}'.
"
593 function device_discover() {
596 log INFO "Running discovery process on device
'${device}'.
"
599 for hook in $(hook_zone_get_all); do
600 hook_zone_exec ${hook} discover ${device}
604 function device_has_ip() {
609 assert device_exists ${device}
611 # IPv6 addresses must be fully imploded
612 local protocol=$(ip_detect_protocol ${addr})
613 case "${protocol}" in
615 addr=$(ipv6_implode ${addr})
619 listmatch ${addr} $(device_get_addresses ${device})
622 function device_get_addresses() {
625 assert device_exists ${device}
630 ip addr show ${device} | \
631 while read prot addr line; do
632 [ "${prot:0:4}" = "inet
" ] && echo "${addr}"
636 function __device_get_file() {
643 local path="${SYS_CLASS_NET}/${device}/${file}"
644 [ -r "${path}" ] || return ${EXIT_ERROR}
649 function device_get_rx_bytes() {
652 __device_get_file ${device} statistics/rx_bytes
655 function device_get_tx_bytes() {
658 __device_get_file ${device} statistics/tx_bytes
661 function device_get_rx_packets() {
664 __device_get_file ${device} statistics/rx_packets
667 function device_get_tx_packets() {
670 __device_get_file ${device} statistics/tx_packets
673 function device_get_rx_errors() {
676 __device_get_file ${device} statistics/rx_errors
679 function device_get_tx_errors() {
682 __device_get_file ${device} statistics/tx_errors
685 function device_get_speed() {
688 __device_get_file ${device} speed
691 function device_get_duplex() {
694 __device_get_file ${device} duplex