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 batman-adv bridge
150 function device_is_batman_adv() {
151 [ -d "${SYS_CLASS_NET}/${1}/mesh
" ]
154 # Check if the device is a batman-adv bridge port
155 function device_is_batman_adv_port() {
156 [ -d "${SYS_CLASS_NET}/${1}/batman_adv
" ]
159 # Check if the device is a bonding device
160 function device_is_bonding() {
161 [ -d "/sys
/class
/net
/${1}/bonding
" ]
164 # Check if the device bonded in a bonding device
165 function device_is_bonded() {
168 [ -d "${SYS_CLASS_NET}/${device}/master
" ]
171 # Check if the device is a bridge
172 function device_is_bridge() {
173 [ -d "/sys
/class
/net
/${1}/bridge
" ]
176 function device_is_bridge_attached() {
178 [ -d "${SYS_CLASS_NET}/${device}/brport
" ]
181 function device_get_bridge() {
185 # Check if device is attached to a bridge.
186 device_is_bridge_attached ${device} || return ${EXIT_ERROR}
188 local ifindex_path="${SYS_CLASS_NET}/${device}/brport
/bridge
/ifindex
"
189 [ -r "${ifindex_path}" ] || return ${EXIT_ERROR}
191 local ifindex=$(<${ifindex_path})
194 device_ifindex_to_name ${ifindex}
197 # Check if the device is a vlan device
198 function device_is_vlan() {
202 [ -e "${PROC_NET_VLAN}/${device}" ]
205 # Check if the device has vlan devices
206 function device_has_vlans() {
210 if device_is_vlan ${device}; then
214 local vlans=$(device_get_vlans ${device})
215 [ -n "${vlans}" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
218 function device_get_vlans
() {
222 # If no 8021q module has been loaded into the kernel,
223 # we cannot do anything.
224 [ -r "${PROC_NET_VLAN_CONFIG}" ] ||
return ${EXIT_OK}
226 local dev spacer1 id spacer2 parent
227 while read dev spacer1 id spacer2 parent
; do
228 [ "${parent}" = "${device}" ] ||
continue
231 done < ${PROC_NET_VLAN_CONFIG}
234 # Check if the device is a ppp device
235 function device_is_ppp
() {
238 local type=$
(__device_get_file
${device} type)
240 [ "${type}" = "512" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
243 # Check if the device is a pointopoint device.
244 function device_is_ptp() {
247 device_has_flag ${device} 0x10
250 # Check if the device is a loopback device
251 function device_is_loopback() {
254 [ "${device}" = "lo
" ]
257 # Check if the device is a wireless device
258 function device_is_wireless() {
261 [ -d "${SYS_CLASS_NET}/${device}/phy80211
" ]
264 function device_is_serial() {
268 # Check if the device is a physical network interface
269 function device_is_ethernet() {
272 device_is_loopback ${device} && \
275 device_is_bonding ${device} && \
278 device_is_bridge ${device} && \
281 device_is_ppp ${device} && \
284 device_is_vlan ${device} && \
287 [ "$
(__device_get_file
${device} type)" != "1" ] && \
293 # Get the device type
294 function device_get_type() {
297 if device_is_vlan ${device}; then
300 elif device_is_bonding ${device}; then
303 elif device_is_bridge ${device}; then
306 elif device_is_ppp ${device}; then
309 elif device_is_batman_adv ${device}; then
312 elif device_is_batman_adv_port ${device}; then
313 echo "batman-adv-port
"
315 elif device_is_loopback ${device}; then
318 elif device_is_wireless ${device}; then
321 elif device_is_ethernet ${device}; then
324 elif device_is_serial ${device}; then
332 function device_get_status() {
336 local status=${STATUS_DOWN}
338 if device_is_up ${device}; then
341 if ! device_has_carrier ${device}; then
342 status=${STATUS_NOCARRIER}
349 function device_get_address() {
352 cat ${SYS_CLASS_NET}/${device}/address 2>/dev/null
355 function device_set_address() {
359 if ! device_exists ${device}; then
360 error "Device
'${device}' does not exist.
"
364 log INFO "Setting address of
'${device}' to '${addr}' - was $(device_get_address ${device})."
367 if device_is_up ${device}; then
368 device_set_down ${device}
372 ip link set ${device} address ${addr}
375 if [ "${up}" = "1" ]; then
376 device_set_up ${device}
379 if [ "${ret}" != "0" ]; then
380 error_log "Could not set address '${addr}' on device '${device}'."
386 function device_get() {
390 for device in ${SYS_CLASS_NET}/*; do
391 device=$(basename ${device})
393 # bonding_masters is no device
394 [ "${device}" = "bonding_masters" ] && continue
396 devices="${devices} ${device}"
403 function devices_get_all() {
407 # Check if a device has a cable plugged in
408 function device_has_carrier() {
412 local carrier=$(__device_get_file ${device} carrier)
413 [ "${carrier}" = "1" ]
416 function device_is_promisc() {
419 device_has_flag ${device} 0x200
422 function device_set_promisc() {
426 assert device_exists ${device}
428 assert isoneof state on off
430 ip link set ${device} promisc ${state}
433 # Check if the device is free
434 function device_is_free() {
438 # Check if the device is used
439 function device_is_used() {
442 device_has_vlans ${device} && \
444 device_is_bonded ${device} && \
446 device_is_bridge_attached ${device} && \
452 function device_hash() {
455 # Get mac address of device and remove all colons (:)
456 # that will result in a hash.
457 device=$(macify ${device})
462 # Give the device a new name
463 function device_set_name() {
465 local destination=${2}
467 # Check if devices exists
468 if ! device_exists ${source} || device_exists ${destination}; then
473 if device_is_up ${source}; then
474 ip link set ${source} down
478 ip link set ${source} name ${destination}
480 if [ "${up}" = "1" ]; then
481 ip link set ${destination} up
486 function device_set_up() {
489 # Silently fail if device was not found
490 [ -z "${device}" ] && return ${EXIT_ERROR}
492 # Do nothing if device is already up
493 device_is_up ${device} && return ${EXIT_OK}
495 device_set_parent_up ${device}
497 log DEBUG "Setting up device '${device}'"
499 ip link set ${device} up
502 function device_set_parent_up() {
506 if device_is_vlan ${device}; then
507 parent=$(vlan_get_parent ${device})
509 device_is_up ${parent} && return ${EXIT_OK}
511 log DEBUG "Setting up parent device '${parent}' of '${device}'"
513 device_set_up ${parent}
521 function device_set_down() {
527 if device_is_up ${device}; then
528 log DEBUG "Tearing down device '${device}'"
530 ip link set ${device} down
534 device_set_parent_down ${device}
539 function device_set_parent_down() {
543 if device_is_vlan ${device}; then
544 parent=$(vlan_get_parent ${device})
546 device_is_up ${parent} || return ${EXIT_OK}
548 if device_is_free ${parent}; then
549 log DEBUG "Tearing down parent device '${parent}' of '${device}'"
551 device_set_down ${parent}
558 function device_get_mtu() {
561 if ! device_exists ${device}; then
562 error "Device '${device}' does not exist."
566 echo $(<${SYS_CLASS_NET}/${device}/mtu)
569 # Set mtu to a device
570 function device_set_mtu() {
574 if ! device_exists ${device}; then
575 error "Device '${device}' does not exist."
579 local oldmtu=$(device_get_mtu ${device})
581 if [ "${oldmtu}" = "${mtu}" ]; then
582 # No need to set mtu.
586 log INFO "Setting mtu of '${device}' to '${mtu}' - was ${oldmtu}.
"
589 if device_is_up ${device}; then
590 device_set_down ${device}
594 ip link set ${device} mtu ${mtu}
597 if [ "${up}" = "1" ]; then
598 device_set_up ${device}
601 if [ "${ret}" != "0" ]; then
602 error_log "Could not
set mtu
'${mtu}' on device
'${device}'.
"
608 function device_discover() {
611 log INFO "Running discovery process on device
'${device}'.
"
614 for hook in $(hook_zone_get_all); do
615 hook_zone_exec ${hook} discover ${device}
619 function device_has_ip() {
624 assert device_exists ${device}
626 # IPv6 addresses must be fully imploded
627 local protocol=$(ip_detect_protocol ${addr})
628 case "${protocol}" in
630 addr=$(ipv6_implode ${addr})
634 listmatch ${addr} $(device_get_addresses ${device})
637 function device_get_addresses() {
640 assert device_exists ${device}
645 ip addr show ${device} | \
646 while read prot addr line; do
647 [ "${prot:0:4}" = "inet
" ] && echo "${addr}"
651 function __device_get_file() {
658 local path="${SYS_CLASS_NET}/${device}/${file}"
659 [ -r "${path}" ] || return ${EXIT_ERROR}
664 function device_get_rx_bytes() {
667 __device_get_file ${device} statistics/rx_bytes
670 function device_get_tx_bytes() {
673 __device_get_file ${device} statistics/tx_bytes
676 function device_get_rx_packets() {
679 __device_get_file ${device} statistics/rx_packets
682 function device_get_tx_packets() {
685 __device_get_file ${device} statistics/tx_packets
688 function device_get_rx_errors() {
691 __device_get_file ${device} statistics/rx_errors
694 function device_get_tx_errors() {
697 __device_get_file ${device} statistics/tx_errors
700 function device_get_speed() {
703 __device_get_file ${device} speed
706 function device_get_duplex() {
709 __device_get_file ${device} duplex