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() {
158 if [ -d "${SYS_CLASS_NET}/${device}/batman_adv
" ]; then
159 local status="$
(<${SYS_CLASS_NET}/${device}/batman_adv
/iface_status
)"
174 # Check if the device is a bonding device
175 function device_is_bonding() {
176 [ -d "/sys
/class
/net
/${1}/bonding
" ]
179 # Check if the device bonded in a bonding device
180 function device_is_bonded() {
183 [ -d "${SYS_CLASS_NET}/${device}/master
" ]
186 # Check if the device is a bridge
187 function device_is_bridge() {
188 [ -d "/sys
/class
/net
/${1}/bridge
" ]
191 function device_is_bridge_attached() {
193 [ -d "${SYS_CLASS_NET}/${device}/brport
" ]
196 function device_get_bridge() {
200 # Check if device is attached to a bridge.
201 device_is_bridge_attached ${device} || return ${EXIT_ERROR}
203 local ifindex_path="${SYS_CLASS_NET}/${device}/brport
/bridge
/ifindex
"
204 [ -r "${ifindex_path}" ] || return ${EXIT_ERROR}
206 local ifindex=$(<${ifindex_path})
209 device_ifindex_to_name ${ifindex}
212 # Check if the device is a vlan device
213 function device_is_vlan() {
217 [ -e "${PROC_NET_VLAN}/${device}" ]
220 # Check if the device has vlan devices
221 function device_has_vlans() {
225 if device_is_vlan ${device}; then
229 local vlans=$(device_get_vlans ${device})
230 [ -n "${vlans}" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
233 function device_get_vlans
() {
237 # If no 8021q module has been loaded into the kernel,
238 # we cannot do anything.
239 [ -r "${PROC_NET_VLAN_CONFIG}" ] ||
return ${EXIT_OK}
241 local dev spacer1 id spacer2 parent
242 while read dev spacer1 id spacer2 parent
; do
243 [ "${parent}" = "${device}" ] ||
continue
246 done < ${PROC_NET_VLAN_CONFIG}
249 # Check if the device is a ppp device
250 function device_is_ppp
() {
253 local type=$
(__device_get_file
${device} type)
255 [ "${type}" = "512" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
258 # Check if the device is a pointopoint device.
259 function device_is_ptp() {
262 device_has_flag ${device} 0x10
265 # Check if the device is a loopback device
266 function device_is_loopback() {
269 [ "${device}" = "lo
" ]
272 # Check if the device is a wireless device
273 function device_is_wireless() {
276 [ -d "${SYS_CLASS_NET}/${device}/phy80211
" ]
279 function device_get_phy() {
282 if device_is_wireless "${device}"; then
283 print "$
(<${SYS_CLASS_NET}/${device}/phy80211
/name
)"
290 function device_is_serial() {
294 # Check if the device is a physical network interface
295 function device_is_ethernet() {
298 device_is_loopback ${device} && \
301 device_is_bonding ${device} && \
304 device_is_bridge ${device} && \
307 device_is_ppp ${device} && \
310 device_is_vlan ${device} && \
313 [ "$
(__device_get_file
${device} type)" != "1" ] && \
319 # Get the device type
320 function device_get_type() {
323 if device_is_vlan ${device}; then
326 elif device_is_bonding ${device}; then
329 elif device_is_bridge ${device}; then
332 elif device_is_ppp ${device}; then
335 elif device_is_batman_adv ${device}; then
338 elif device_is_batman_adv_port ${device}; then
339 echo "batman-adv-port
"
341 elif device_is_loopback ${device}; then
344 elif device_is_wireless ${device}; then
347 elif device_is_ethernet ${device}; then
350 elif device_is_serial ${device}; then
358 function device_get_status() {
362 local status=${STATUS_DOWN}
364 if device_is_up ${device}; then
367 if ! device_has_carrier ${device}; then
368 status=${STATUS_NOCARRIER}
375 function device_get_address() {
378 cat ${SYS_CLASS_NET}/${device}/address 2>/dev/null
381 function device_set_address() {
387 if ! device_exists "${device}"; then
388 error "Device
'${device}' does not exist.
"
392 # Do nothing if the address has not changed
393 local old_addr="$
(device_get_address
"${device}")"
394 if [ -n "${old_addr}" -a "${addr}" = "${old_addr}" ]; then
398 log DEBUG "Setting address of
'${device}' from '${old_addr}' to '${addr}'"
401 if device_is_up "${device}"; then
402 device_set_down "${device}"
406 ip link set "${device}" address "${addr}"
409 if [ "${up}" = "1" ]; then
410 device_set_up "${device}"
413 if [ "${ret}" != "0" ]; then
414 error_log "Could not
set address
'${addr}' on device
'${device}'"
420 function device_get() {
424 for device in ${SYS_CLASS_NET}/*; do
425 device=$(basename ${device})
427 # bonding_masters is no device
428 [ "${device}" = "bonding_masters
" ] && continue
430 devices="${devices} ${device}"
437 function devices_get_all() {
441 # Check if a device has a cable plugged in
442 function device_has_carrier() {
446 local carrier=$(__device_get_file ${device} carrier)
447 [ "${carrier}" = "1" ]
450 function device_is_promisc() {
453 device_has_flag ${device} 0x200
456 function device_set_promisc() {
460 assert device_exists ${device}
462 assert isoneof state on off
464 ip link set ${device} promisc ${state}
467 # Check if the device is free
468 function device_is_free() {
472 # Check if the device is used
473 function device_is_used() {
476 device_has_vlans ${device} && \
478 device_is_bonded ${device} && \
480 device_is_bridge_attached ${device} && \
486 function device_hash() {
489 # Get mac address of device and remove all colons (:)
490 # that will result in a hash.
491 device=$(macify ${device})
496 # Give the device a new name
497 function device_set_name() {
499 local destination=${2}
501 # Check if devices exists
502 if ! device_exists ${source} || device_exists ${destination}; then
507 if device_is_up ${source}; then
508 ip link set ${source} down
512 ip link set ${source} name ${destination}
514 if [ "${up}" = "1" ]; then
515 ip link set ${destination} up
520 function device_set_up() {
523 # Silently fail if device was not found
524 [ -z "${device}" ] && return ${EXIT_ERROR}
526 # Do nothing if device is already up
527 device_is_up ${device} && return ${EXIT_OK}
529 device_set_parent_up ${device}
531 log DEBUG "Setting up device
'${device}'"
533 ip link set ${device} up
536 function device_set_parent_up() {
540 if device_is_vlan ${device}; then
541 parent=$(vlan_get_parent ${device})
543 device_is_up ${parent} && return ${EXIT_OK}
545 log DEBUG "Setting up parent device
'${parent}' of
'${device}'"
547 device_set_up ${parent}
555 function device_set_down() {
561 if device_is_up ${device}; then
562 log DEBUG "Tearing down device
'${device}'"
564 ip link set ${device} down
568 device_set_parent_down ${device}
573 function device_set_parent_down() {
577 if device_is_vlan ${device}; then
578 parent=$(vlan_get_parent ${device})
580 device_is_up ${parent} || return ${EXIT_OK}
582 if device_is_free ${parent}; then
583 log DEBUG "Tearing down parent device
'${parent}' of
'${device}'"
585 device_set_down ${parent}
592 function device_get_mtu() {
595 if ! device_exists ${device}; then
596 error "Device
'${device}' does not exist.
"
600 echo $(<${SYS_CLASS_NET}/${device}/mtu)
603 # Set mtu to a device
604 function device_set_mtu() {
608 if ! device_exists ${device}; then
609 error "Device
'${device}' does not exist.
"
613 local oldmtu=$(device_get_mtu ${device})
615 if [ "${oldmtu}" = "${mtu}" ]; then
616 # No need to set mtu.
620 log INFO "Setting mtu of
'${device}' to '${mtu}' - was ${oldmtu}."
623 if device_is_up ${device}; then
624 device_set_down ${device}
628 ip link set ${device} mtu ${mtu}
631 if [ "${up}" = "1" ]; then
632 device_set_up ${device}
635 if [ "${ret}" != "0" ]; then
636 error_log "Could not set mtu '${mtu}' on device '${device}'."
642 function device_adjust_mtu() {
646 local other_device="${2}"
648 local mtu="$(device_get_mtu "${other_device}")"
649 device_set_mtu "${device}" "${mtu}"
652 function device_discover() {
655 log INFO "Running discovery process on device '${device}'."
658 for hook in $(hook_zone_get_all); do
659 hook_zone_exec ${hook} discover ${device}
663 function device_has_ip() {
668 assert device_exists ${device}
670 # IPv6 addresses must be fully imploded
671 local protocol=$(ip_detect_protocol ${addr})
672 case "${protocol}" in
674 addr=$(ipv6_implode ${addr})
678 listmatch ${addr} $(device_get_addresses ${device})
681 function device_get_addresses() {
684 assert device_exists ${device}
689 ip addr show ${device} | \
690 while read prot addr line; do
691 [ "${prot:0:4}" = "inet" ] && echo "${addr}"
695 function __device_get_file() {
702 local path="${SYS_CLASS_NET}/${device}/${file}"
703 [ -r "${path}" ] || return ${EXIT_ERROR}
708 function __device_set_file() {
715 local path="${SYS_CLASS_NET}/${device}/${file}"
716 if [ ! -w "${path}" ]; then
717 log DEBUG "Cannot write to file '${file}' (${value})"
721 echo "${value}" > "${path}"
724 function device_get_rx_bytes() {
727 __device_get_file ${device} statistics/rx_bytes
730 function device_get_tx_bytes() {
733 __device_get_file ${device} statistics/tx_bytes
736 function device_get_rx_packets() {
739 __device_get_file ${device} statistics/rx_packets
742 function device_get_tx_packets() {
745 __device_get_file ${device} statistics/tx_packets
748 function device_get_rx_errors() {
751 __device_get_file ${device} statistics/rx_errors
754 function device_get_tx_errors() {
757 __device_get_file ${device} statistics/tx_errors
760 function device_get_speed() {
763 __device_get_file ${device} speed
766 function device_get_duplex() {
769 __device_get_file ${device} duplex