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 [ -d "${SYS_CLASS_NET}/${device}" ]
71 function device_has_flag
() {
75 local flags
=$
(__device_get_file
${device} flags
)
77 if [[ "$(( ${flags} & ${flag} ))" -eq 0 ]]; then
84 # Check if the device is up
85 function device_is_up
() {
88 device_exists
${device} ||
return ${EXIT_ERROR}
90 device_has_flag
${device} 0x1
93 # Check if the device is a bonding device
94 function device_is_bonding
() {
95 [ -d "/sys/class/net/${1}/bonding" ]
98 # Check if the device bonded in a bonding device
99 function device_is_bonded
() {
102 [ -d "${SYS_CLASS_NET}/${device}/master" ]
105 # Check if the device is a bridge
106 function device_is_bridge
() {
107 [ -d "/sys/class/net/${1}/bridge" ]
110 function device_is_bridge_attached
() {
113 [ -d "${SYS_CLASS_NET}/${device}/brport" ]
116 # Check if the device is a virtual device
117 function device_is_virtual
() {
120 [ -e "/proc/net/vlan/${device}" ]
123 # Check if the device has virtual devices
124 function device_has_virtuals
() {
127 if device_is_virtual
${device}; then
131 if [ ! -e "/proc/net/vlan/config" ]; then
134 grep -q "${1}$" /proc
/net
/vlan
/config
137 # Check if the device is a ppp device
138 function device_is_ppp
() {
141 local type=$
(__device_get_file
${device} type)
143 [ "${type}" = "512" ] && return ${EXIT_OK} || return ${EXIT_ERROR}
146 # Check if the device is a pointopoint device.
147 function device_is_ptp() {
150 device_has_flag ${device} 0x10
153 # Check if the device is a loopback device
154 function device_is_loopback() {
157 [ "${device}" = "lo
" ]
160 # Check if the device is a wireless device
161 function device_is_wireless() {
164 [ -d "${SYS_CLASS_NET}/${device}/phy80211
" ]
167 # Check if the device is a physical network interface
168 function device_is_real() {
171 device_is_loopback ${device} && \
174 device_is_bonding ${device} && \
177 device_is_bridge ${device} && \
180 device_is_ppp ${device} && \
183 device_is_virtual ${device} && \
186 [ "$
(__device_get_file
${device} type)" != "1" ] && \
192 # Get the device type
193 function device_get_type() {
196 if device_is_virtual ${device}; then
199 elif device_is_bonding ${device}; then
202 elif device_is_bridge ${device}; then
205 elif device_is_ppp ${device}; then
208 elif device_is_loopback ${device}; then
211 elif device_is_wireless ${device}; then
214 elif device_is_real ${device}; then
222 function device_get_status() {
227 local status=${STATUS_UNKNOWN}
229 if ! device_has_carrier ${device}; then
230 status=${STATUS_NOCARRIER}
231 elif device_is_up ${device}; then
233 elif device_is_down ${device}; then
234 status=${STATUS_DOWN}
242 function device_get_address() {
245 cat ${SYS_CLASS_NET}/${device}/address 2>/dev/null
248 function device_set_address() {
252 if ! device_exists ${device}; then
253 error "Device
'${device}' does not exist.
"
257 log INFO "Setting address of
'${device}' to '${addr}' - was $(device_get_address ${device})."
260 if device_is_up ${device}; then
261 device_set_down ${device}
265 ip link set ${device} address ${addr}
268 if [ "${up}" = "1" ]; then
269 device_set_up ${device}
272 if [ "${ret}" != "0" ]; then
273 error_log "Could not set address '${addr}' on device '${device}'."
279 function device_get() {
283 for device in ${SYS_CLASS_NET}/*; do
284 device=$(basename ${device})
286 # bonding_masters is no device
287 [ "${device}" = "bonding_masters" ] && continue
289 devices="${devices} ${device}"
296 function devices_get_all() {
300 # Check if a device has a cable plugged in
301 function device_has_carrier() {
305 [ "$(<${SYS_CLASS_NET}/${device}/carrier)" = "1" ]
308 function device_is_promisc() {
311 device_has_flag ${device} 0x200
314 function device_set_promisc() {
318 assert device_exists ${device}
320 assert isoneof state on off
322 ip link set ${device} promisc ${state}
325 # Check if the device is free
326 function device_is_free() {
330 # Check if the device is used
331 function device_is_used() {
334 device_has_virtuals ${device} && \
336 device_is_bonded ${device} && \
338 device_is_bridge_attached ${device} && \
344 function device_hash() {
347 # Get mac address of device and remove all colons (:)
348 # that will result in a hash.
349 device=$(macify ${device})
354 # Give the device a new name
355 function device_set_name() {
357 local destination=${2}
359 # Check if devices exists
360 if ! device_exists ${source} || device_exists ${destination}; then
365 if device_is_up ${source}; then
366 ip link set ${source} down
370 ip link set ${source} name ${destination}
372 if [ "${up}" = "1" ]; then
373 ip link set ${destination} up
378 function device_set_up() {
381 # Silently fail if device was not found
382 [ -z "${device}" ] && return ${EXIT_ERROR}
384 # Do nothing if device is already up
385 device_is_up ${device} && return ${EXIT_OK}
387 device_set_parent_up ${device}
389 log DEBUG "Setting up device '${device}'"
391 ip link set ${device} up
394 function device_set_parent_up() {
398 if device_is_virtual ${device}; then
399 parent=$(virtual_get_parent ${device})
401 device_is_up ${parent} && return ${EXIT_OK}
403 log DEBUG "Setting up parent device '${parent}' of '${device}'"
405 device_set_up ${parent}
413 function device_set_down() {
419 if device_is_up ${device}; then
420 log DEBUG "Tearing down device '${device}'"
422 ip link set ${device} down
426 device_set_parent_down ${device}
431 function device_set_parent_down() {
435 if device_is_virtual ${device}; then
436 parent=$(virtual_get_parent ${device})
438 device_is_up ${parent} || return ${EXIT_OK}
440 if device_is_free ${parent}; then
441 log DEBUG "Tearing down parent device '${parent}' of '${device}'"
443 device_set_down ${parent}
450 function device_get_mtu() {
453 if ! device_exists ${device}; then
454 error "Device '${device}' does not exist."
458 echo $(<${SYS_CLASS_NET}/${device}/mtu)
461 # Set mtu to a device
462 function device_set_mtu() {
466 if ! device_exists ${device}; then
467 error "Device '${device}' does not exist."
471 local oldmtu=$(device_get_mtu ${device})
473 if [ "${oldmtu}" = "${mtu}" ]; then
474 # No need to set mtu.
478 log INFO "Setting mtu of '${device}' to '${mtu}' - was ${oldmtu}.
"
481 if device_is_up ${device}; then
482 device_set_down ${device}
486 ip link set ${device} mtu ${mtu}
489 if [ "${up}" = "1" ]; then
490 device_set_up ${device}
493 if [ "${ret}" != "0" ]; then
494 error_log "Could not
set mtu
'${mtu}' on device
'${device}'.
"
500 function device_discover() {
503 log INFO "Running discovery process on device
'${device}'.
"
506 for hook in $(hook_zone_get_all); do
507 hook_zone_exec ${hook} discover ${device}
511 function device_has_ip() {
516 assert device_exists ${device}
518 # IPv6 addresses must be fully imploded
519 local protocol=$(ip_detect_protocol ${addr})
520 case "${protocol}" in
522 addr=$(ipv6_implode ${addr})
526 listmatch ${addr} $(device_get_addresses ${device})
529 function device_get_addresses() {
532 assert device_exists ${device}
537 ip addr show ${device} | \
538 while read prot addr line; do
539 [ "${prot:0:4}" = "inet
" ] && echo "${addr}"
543 function __device_get_file() {
550 local path="${SYS_CLASS_NET}/${device}/${file}"
551 [ -r "${path}" ] || return ${EXIT_ERROR}
556 function device_get_rx_bytes() {
559 __device_get_file ${device} statistics/rx_bytes
562 function device_get_tx_bytes() {
565 __device_get_file ${device} statistics/tx_bytes
568 function device_get_rx_packets() {
571 __device_get_file ${device} statistics/rx_packets
574 function device_get_tx_packets() {
577 __device_get_file ${device} statistics/tx_packets
580 function device_get_rx_errors() {
583 __device_get_file ${device} statistics/rx_errors
586 function device_get_tx_errors() {
589 __device_get_file ${device} statistics/tx_errors