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_matches_pattern() {
83 pattern="^
${pattern//N/[[:digit:]]+}$
"
85 [[ ${device} =~ ${pattern} ]] \
86 && return ${EXIT_TRUE} || return ${EXIT_FALSE}
89 function device_delete() {
93 # Nothing to do, it device does not exist.
94 device_exists ${device} || return ${EXIT_OK}
97 cmd_quiet ip link delete ${device}
100 if [ ${ret} -ne ${EXIT_OK} ]; then
101 log ERROR "device
: Could not delete device
'${device}': ${ret}"
108 function device_has_flag() {
112 local flags=$(__device_get_file ${device} flags)
114 if [[ "$
(( ${flags} & ${flag} ))" -eq 0 ]]; then
121 # Check if the device is up
122 function device_is_up() {
125 device_exists ${device} || return ${EXIT_ERROR}
127 device_has_flag ${device} 0x1
130 function device_ifindex_to_name() {
134 local device device_idx
135 for device in ${SYS_CLASS_NET}/*; do
136 device=$(basename ${device})
137 device_exists ${device} || continue
139 device_idx=$(device_get_ifindex ${device})
141 if [ "${device_idx}" = "${idx}" ]; then
150 function device_get_ifindex() {
154 local path="${SYS_CLASS_NET}/${1}/ifindex
"
156 # Check if file can be read.
157 [ -r "${path}" ] || return ${EXIT_ERROR}
162 # Check if the device is a batman-adv bridge
163 function device_is_batman_adv() {
164 [ -d "${SYS_CLASS_NET}/${1}/mesh
" ]
167 # Check if the device is a batman-adv bridge port
168 function device_is_batman_adv_port() {
171 if [ -d "${SYS_CLASS_NET}/${device}/batman_adv
" ]; then
172 local status="$
(<${SYS_CLASS_NET}/${device}/batman_adv
/iface_status
)"
187 # Check if the device is a bonding device
188 function device_is_bonding() {
189 [ -d "/sys
/class
/net
/${1}/bonding
" ]
192 # Check if the device bonded in a bonding device
193 function device_is_bonded() {
196 [ -d "${SYS_CLASS_NET}/${device}/bonding_slave
" ]
199 # Check if the device is a bridge
200 function device_is_bridge() {
201 [ -d "/sys
/class
/net
/${1}/bridge
" ]
204 function device_is_bridge_attached() {
206 [ -d "${SYS_CLASS_NET}/${device}/brport
" ]
209 function device_is_wireless_monitor() {
213 device_is_wireless "${device}" && \
214 device_matches_pattern "${device}" "${PORT_PATTERN_WIRELESS_MONITOR}"
217 function device_get_bridge() {
221 # Check if device is attached to a bridge.
222 device_is_bridge_attached ${device} || return ${EXIT_ERROR}
224 local ifindex_path="${SYS_CLASS_NET}/${device}/brport
/bridge
/ifindex
"
225 [ -r "${ifindex_path}" ] || return ${EXIT_ERROR}
227 local ifindex=$(<${ifindex_path})
230 device_ifindex_to_name ${ifindex}
233 # Check if the device is a vlan device
234 function device_is_vlan() {
238 [ -e "${PROC_NET_VLAN}/${device}" ]
241 # Check if the device has vlan devices
242 function device_has_vlans() {
246 if device_is_vlan ${device}; then
250 local vlans=$(device_get_vlans ${device})
251 [ -n "${vlans}" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
254 function device_get_vlans
() {
258 # If no 8021q module has been loaded into the kernel,
259 # we cannot do anything.
260 [ -r "${PROC_NET_VLAN_CONFIG}" ] ||
return ${EXIT_OK}
262 local dev spacer1 id spacer2 parent
263 while read dev spacer1 id spacer2 parent
; do
264 [ "${parent}" = "${device}" ] ||
continue
267 done < ${PROC_NET_VLAN_CONFIG}
270 # Check if the device is a ppp device
271 function device_is_ppp
() {
274 local type=$
(__device_get_file
${device} type)
276 [ "${type}" = "512" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
279 # Check if the device is a pointopoint device.
280 function device_is_ptp() {
283 device_has_flag ${device} 0x10
286 # Check if the device is a loopback device
287 function device_is_loopback() {
290 [ "${device}" = "lo
" ]
293 # Check if the device is a wireless device
294 function device_is_wireless() {
297 [ -d "${SYS_CLASS_NET}/${device}/phy80211
" ]
300 function device_get_phy() {
303 if device_is_wireless "${device}"; then
304 print "$
(<${SYS_CLASS_NET}/${device}/phy80211
/name
)"
311 function device_is_serial() {
315 # Check if the device is a physical network interface
316 function device_is_ethernet() {
319 device_is_loopback ${device} && \
322 device_is_bonding ${device} && \
325 device_is_bridge ${device} && \
328 device_is_ppp ${device} && \
331 device_is_vlan ${device} && \
334 [ "$
(__device_get_file
${device} type)" != "1" ] && \
340 # Get the device type
341 function device_get_type() {
344 if device_is_vlan ${device}; then
347 elif device_is_bonding ${device}; then
350 elif device_is_bridge ${device}; then
353 elif device_is_ppp ${device}; then
356 elif device_is_batman_adv ${device}; then
359 elif device_is_batman_adv_port ${device}; then
360 echo "batman-adv-port
"
362 elif device_is_loopback ${device}; then
365 elif device_is_wireless ${device}; then
368 elif device_is_ethernet ${device}; then
371 elif device_is_serial ${device}; then
379 function device_get_status() {
383 local status=${STATUS_DOWN}
385 if device_is_up ${device}; then
388 if ! device_has_carrier ${device}; then
389 status=${STATUS_NOCARRIER}
396 function device_get_address() {
399 cat ${SYS_CLASS_NET}/${device}/address 2>/dev/null
402 function device_set_address() {
408 if ! device_exists "${device}"; then
409 error "Device
'${device}' does not exist.
"
413 # Do nothing if the address has not changed
414 local old_addr="$
(device_get_address
"${device}")"
415 if [ -n "${old_addr}" -a "${addr}" = "${old_addr}" ]; then
419 log DEBUG "Setting address of
'${device}' from '${old_addr}' to '${addr}'"
422 if device_is_up "${device}"; then
423 device_set_down "${device}"
427 ip link set "${device}" address "${addr}"
430 if [ "${up}" = "1" ]; then
431 device_set_up "${device}"
434 if [ "${ret}" != "0" ]; then
435 error_log "Could not
set address
'${addr}' on device
'${device}'"
441 function device_get() {
445 for device in ${SYS_CLASS_NET}/*; do
446 device=$(basename ${device})
448 # bonding_masters is no device
449 [ "${device}" = "bonding_masters
" ] && continue
451 devices="${devices} ${device}"
458 function devices_get_all() {
462 # Check if a device has a cable plugged in
463 function device_has_carrier() {
467 local carrier=$(__device_get_file ${device} carrier)
468 [ "${carrier}" = "1" ]
471 function device_is_promisc() {
474 device_has_flag ${device} 0x200
477 function device_set_promisc() {
481 assert device_exists ${device}
483 assert isoneof state on off
485 ip link set ${device} promisc ${state}
488 # Check if the device is free
489 function device_is_free() {
493 # Check if the device is used
494 function device_is_used() {
497 device_has_vlans ${device} && \
499 device_is_bonded ${device} && \
501 device_is_bridge_attached ${device} && \
507 function device_hash() {
510 # Get mac address of device and remove all colons (:)
511 # that will result in a hash.
512 device=$(macify ${device})
517 # Give the device a new name
518 function device_set_name() {
520 local destination=${2}
522 # Check if devices exists
523 if ! device_exists ${source} || device_exists ${destination}; then
528 if device_is_up ${source}; then
529 ip link set ${source} down
533 ip link set ${source} name ${destination}
535 if [ "${up}" = "1" ]; then
536 ip link set ${destination} up
541 function device_set_up() {
544 # Silently fail if device was not found
545 [ -z "${device}" ] && return ${EXIT_ERROR}
547 # Do nothing if device is already up
548 device_is_up ${device} && return ${EXIT_OK}
550 device_set_parent_up ${device}
552 log DEBUG "Setting up device
'${device}'"
554 ip link set ${device} up
557 function device_set_parent_up() {
561 if device_is_vlan ${device}; then
562 parent=$(vlan_get_parent ${device})
564 device_is_up ${parent} && return ${EXIT_OK}
566 log DEBUG "Setting up parent device
'${parent}' of
'${device}'"
568 device_set_up ${parent}
576 function device_set_down() {
582 if device_is_up ${device}; then
583 log DEBUG "Tearing down device
'${device}'"
585 ip link set ${device} down
589 device_set_parent_down ${device}
594 function device_set_parent_down() {
598 if device_is_vlan ${device}; then
599 parent=$(vlan_get_parent ${device})
601 device_is_up ${parent} || return ${EXIT_OK}
603 if device_is_free ${parent}; then
604 log DEBUG "Tearing down parent device
'${parent}' of
'${device}'"
606 device_set_down ${parent}
613 function device_get_mtu() {
616 if ! device_exists ${device}; then
617 error "Device
'${device}' does not exist.
"
621 echo $(<${SYS_CLASS_NET}/${device}/mtu)
624 # Set mtu to a device
625 function device_set_mtu() {
629 if ! device_exists ${device}; then
630 error "Device
'${device}' does not exist.
"
634 local oldmtu=$(device_get_mtu ${device})
636 if [ "${oldmtu}" = "${mtu}" ]; then
637 # No need to set mtu.
641 log INFO "Setting mtu of
'${device}' to '${mtu}' - was ${oldmtu}."
644 if device_is_up ${device}; then
645 device_set_down ${device}
649 ip link set ${device} mtu ${mtu}
652 if [ "${up}" = "1" ]; then
653 device_set_up ${device}
656 if [ "${ret}" != "0" ]; then
657 error_log "Could not set mtu '${mtu}' on device '${device}'."
663 function device_adjust_mtu() {
667 local other_device="${2}"
669 local mtu="$(device_get_mtu "${other_device}")"
670 device_set_mtu "${device}" "${mtu}"
673 function device_discover() {
676 log INFO "Running discovery process on device '${device}'."
679 for hook in $(hook_zone_get_all); do
680 hook_zone_exec ${hook} discover ${device}
684 function device_has_ip() {
689 assert device_exists ${device}
691 # IPv6 addresses must be fully imploded
692 local protocol=$(ip_detect_protocol ${addr})
693 case "${protocol}" in
695 addr=$(ipv6_implode ${addr})
699 listmatch ${addr} $(device_get_addresses ${device})
702 function device_get_addresses() {
705 assert device_exists ${device}
710 ip addr show ${device} | \
711 while read prot addr line; do
712 [ "${prot:0:4}" = "inet" ] && echo "${addr}"
716 function __device_get_file() {
723 local path="${SYS_CLASS_NET}/${device}/${file}"
724 [ -r "${path}" ] || return ${EXIT_ERROR}
729 function __device_set_file() {
736 local path="${SYS_CLASS_NET}/${device}/${file}"
737 if [ ! -w "${path}" ]; then
738 log DEBUG "Cannot write to file '${file}' (${value})"
742 echo "${value}" > "${path}"
745 function device_get_rx_bytes() {
748 __device_get_file ${device} statistics/rx_bytes
751 function device_get_tx_bytes() {
754 __device_get_file ${device} statistics/tx_bytes
757 function device_get_rx_packets() {
760 __device_get_file ${device} statistics/rx_packets
763 function device_get_tx_packets() {
766 __device_get_file ${device} statistics/tx_packets
769 function device_get_rx_errors() {
772 __device_get_file ${device} statistics/rx_errors
775 function device_get_tx_errors() {
778 __device_get_file ${device} statistics/tx_errors
781 function device_get_speed() {
784 __device_get_file ${device} speed
787 function device_get_duplex() {
790 __device_get_file ${device} duplex