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 # Check if the device is up
72 function device_is_up
() {
75 device_exists
${device} ||
return ${EXIT_ERROR}
77 ip link show
${device} 2>/dev
/null |
grep -qE "<.*UP.*>"
80 # Check if the device is a bonding device
81 function device_is_bonding
() {
82 [ -d "/sys/class/net/${1}/bonding" ]
85 # Check if the device bonded in a bonding device
86 function device_is_bonded
() {
89 [ -d "${SYS_CLASS_NET}/${device}/master" ]
92 # Check if the device is a bridge
93 function device_is_bridge
() {
94 [ -d "/sys/class/net/${1}/bridge" ]
97 function device_is_bridge_attached
() {
100 [ -d "${SYS_CLASS_NET}/${device}/brport" ]
103 # Check if the device is a virtual device
104 function device_is_virtual
() {
107 [ -e "/proc/net/vlan/${device}" ]
110 # Check if the device has virtual devices
111 function device_has_virtuals
() {
114 if device_is_virtual
${device}; then
118 if [ ! -e "/proc/net/vlan/config" ]; then
121 grep -q "${1}$" /proc
/net
/vlan
/config
124 function device_is_vlan
() { # XXX Compat function
125 log DEBUG
"Deprecated function device_is_vlan() was used."
130 # Check if the device is a ppp device
131 function device_is_ppp
() {
134 ip link show
${device} 2>/dev
/null |
grep -qE "<.*POINTOPOINT.*>"
137 # Check if the device is a loopback device
138 function device_is_loopback
() {
139 local device
=$
(devicify
${1})
140 [ "${device}" = "lo" ]
143 # Check if the device is a physical network interface
144 function device_is_real
() {
147 device_is_loopback
${device} && \
150 device_is_bonding
${device} && \
153 device_is_bridge
${device} && \
156 device_is_ppp
${device} && \
159 device_is_virtual
${device} && \
165 # Get the device type
166 function device_get_type
() {
167 local device
=$
(devicify
${1})
169 if device_is_vlan
${device}; then
172 elif device_is_bonding
${device}; then
175 elif device_is_bridge
${device}; then
178 elif device_is_ppp
${device}; then
181 elif device_is_loopback
${device}; then
184 elif device_is_real
${device}; then
192 function device_get_status
() {
197 local status
=${STATUS_UNKNOWN}
199 if ! device_has_carrier
${device}; then
200 status
=${STATUS_NOCARRIER}
201 elif device_is_up
${device}; then
203 elif device_is_down
${device}; then
204 status
=${STATUS_DOWN}
212 function device_get_address
() {
215 cat ${SYS_CLASS_NET}/${device}/address
2>/dev
/null
218 function device_set_address
() {
222 if ! device_exists
${device}; then
223 error
"Device '${device}' does not exist."
227 log INFO
"Setting address of '${device}' to '${addr}' - was $(device_get_address ${device})."
230 if device_is_up
${device}; then
231 device_set_down
${device}
235 ip link
set ${device} address
${addr}
238 if [ "${up}" = "1" ]; then
239 device_set_up
${device}
242 if [ "${ret}" != "0" ]; then
243 error_log
"Could not set address '${addr}' on device '${device}'."
249 function device_get
() {
252 while [ $# -gt 0 ]; do
266 if [ "${from_config}" != "0" ]; then
267 devices
="${devices} $(device_config_list)"
270 if [ "${from_config}" != "1" ]; then
272 for device
in ${SYS_CLASS_NET}/*; do
273 devices
="${devices} $(basename ${device})"
281 function devices_get_all
() {
285 # Check if a device has a cable plugged in
286 function device_has_carrier
() {
287 local device
=$
(devicify
${1})
288 [ "$(<${SYS_CLASS_NET}/${device}/carrier)" = "1" ]
291 function device_is_promisc
() {
294 ip link show
${device} |
grep -qE "<.*PROMISC.*>"
297 # Check if the device is free
298 function device_is_free
() {
302 # Check if the device is used
303 function device_is_used
() {
304 local device
=$
(devicify
${1})
306 device_has_virtuals
${device} && \
308 device_is_bonded
${device} && \
310 device_is_bridge_attached
${device} && \
316 # XXX to be removed I think
317 function device_get_free
() {
318 local destination
=${1}
320 # Replace + by a valid number
321 if grep -q "+$" <<<${destination}; then
323 destination
=$
(sed -e "s/+//" <<<$destination)
324 while [ "${number}" -le "100" ]; do
325 if ! device_exists
"${destination}${number}"; then
326 destination
="${destination}${number}"
329 number
=$
(($number + 1))
332 echo "${destination}"
335 function device_rename
() {
336 warning_log
"Called deprecated function 'device_rename'"
341 function device_hash
() {
344 # Get mac address of device and remove all colons (:)
345 # that will result in a hash.
346 device
=$
(macify
${device})
351 # Give the device a new name
352 function device_set_name
() {
354 local destination
=$
(device_get_free
${2})
356 # Check if devices exists
357 if ! device_exists
${source} || device_exists
${destination}; then
362 if device_is_up
${source}; then
363 ip link
set ${source} down
367 ip link
set ${source} name
${destination}
369 if [ "${up}" = "1" ]; then
370 ip link
set ${destination} up
375 function device_set_up
() {
376 local device
=$
(devicify
${1})
378 # Silently fail if device was not found
379 [ -z "${device}" ] && return ${EXIT_ERROR}
381 # Do nothing if device is already up
382 device_is_up
${device} && return ${EXIT_OK}
384 device_set_parent_up
${device}
386 log DEBUG
"Setting up device '${device}'"
388 ip link
set ${device} up
391 function device_set_parent_up
() {
395 if device_is_virtual
${device}; then
396 parent
=$
(device_virtual_get_parent
${device})
398 device_is_up
${parent} && return ${EXIT_OK}
400 log DEBUG
"Setting up parent device '${parent}' of '${device}'"
402 device_set_up
${parent}
410 function device_set_down
() {
411 local device
=$
(devicify
${1})
415 if device_is_up
${device}; then
416 log DEBUG
"Tearing down device '${device}'"
418 ip link
set ${device} down
422 device_set_parent_down
${device}
427 function device_set_parent_down
() {
431 if device_is_virtual
${device}; then
432 parent
=$
(device_virtual_get_parent
${device})
434 device_is_up
${parent} ||
return ${EXIT_OK}
436 if device_is_free
${parent}; then
437 log DEBUG
"Tearing down parent device '${parent}' of '${device}'"
439 device_set_down
${parent}
446 function device_get_mtu
() {
449 if ! device_exists
${device}; then
450 error
"Device '${device}' does not exist."
454 echo $
(<${SYS_CLASS_NET}/${device}/mtu
)
457 # Set mtu to a device
458 function device_set_mtu
() {
462 if ! device_exists
${device}; then
463 error
"Device '${device}' does not exist."
467 local oldmtu
=$
(device_get_mtu
${device})
469 if [ "${oldmtu}" = "${mtu}" ]; then
470 # No need to set mtu.
474 log INFO
"Setting mtu of '${device}' to '${mtu}' - was ${oldmtu}."
477 if device_is_up
${device}; then
478 device_set_down
${device}
482 ip link
set ${device} mtu
${mtu}
485 if [ "${up}" = "1" ]; then
486 device_set_up
${device}
489 if [ "${ret}" != "0" ]; then
490 error_log
"Could not set mtu '${mtu}' on device '${device}'."
496 function device_discover
() {
499 log INFO
"Running discovery process on device '${device}'."
502 for hook
in $
(hook_zone_get_all
); do
503 hook_zone_exec
${hook} discover
${device}
507 function device_create_virtual
() {
508 log WARN
"Called deprecated function device_create_virtual"
509 device_virtual_create $@
512 function device_virtual_create
() {
513 local port
=$
(devicify
${1})
516 local newport
=${port}v
${vid}
518 if [ -z "${mac}" ]; then
522 log INFO
"Creating virtual device '${newport}' with address '${mac}'."
524 local oldport
=$
(device_virtual_get_by_parent_and_vid
${port} ${vid})
526 if device_exists
${oldport}; then
529 if [ "${oldport}" != "${newport}" ]; then
530 differences
="${differences} name"
532 if [ "$(device_get_address ${oldport})" != "${mac}" ]; then
533 differences
="${differences} address"
536 echo "differences: $differences"
538 if [ -n "${differences}" ]; then
539 if device_is_used
${oldport}; then
540 error_log
"There was a device '${oldport}' set up with VID '${vid}' and parent '${port}' which is used somewhere else. Cannot go on."
543 log DEBUG
"There is a device '${oldport}' but it not used, so we grab it to ourselves."
546 log DEBUG
"Device '${newport}' already exists and reflects our configuration. Go on."
548 device_set_up
${oldport}
553 log DEBUG
"Virtual device '${newport}' does not exist, yet."
555 vconfig set_name_type DEV_PLUS_VID_NO_PAD
>/dev
/null
556 vconfig add
${port} ${vid} >/dev
/null
558 if [ $?
-ne ${EXIT_OK} ]; then
559 error_log
"Could not create virtual device '${newport}'."
563 oldport
=$
(device_virtual_get_by_parent_and_vid
${port} ${vid})
567 assert device_exists
${oldport}
569 if ! device_exists
${oldport}; then
570 error
"Could not determine the created virtual device '${newport}'."
574 # The device is expected to be named like ${port}.${vid}
575 # and will be renamed to the virtual schema
576 device_set_name
${oldport} ${newport}
578 if [ $?
-ne ${EXIT_OK} ]; then
579 error_log
"Could not set name of virtual device '${newport}'."
583 assert device_exists
${newport}
585 # Setting new mac address
586 device_set_address
${newport} ${mac}
588 if [ $?
-ne ${EXIT_OK} ]; then
589 error_log
"Could not set address '${mac}' to virtual device '${newport}'."
593 # Bring up the new device
594 device_set_up
${newport}
599 function device_virtual_remove
() {
600 local device
=$
(devicify
${1})
602 log INFO
"Removing virtual device '${device}' with address '$(macify ${device})'."
604 device_set_down
${device}
606 vconfig rem
${device} >/dev
/null
608 if [ $?
-ne ${EXIT_OK} ]; then
609 error_log
"Could not remote virtual device '${newport}'."
616 function device_virtual_get_parent
() {
619 local parent
=$
(grep "^${device}" < /proc
/net
/vlan
/config |
awk '{ print $NF }')
621 if device_exists
${parent}; then
629 function device_virtual_get_by_parent_and_vid
() {
640 assert
[ -e "/proc/net/vlan/config" ]
642 fgrep
'|' < /proc
/net
/vlan
/config |
tr -d '|' | \
643 while read v_port v_id v_parent
; do
644 if [ "${v_parent}" = "${parent}" ] && [ "${v_id}" = "${vid}" ]; then
653 function bridge_attach_device
() {
660 assert device_exists
${bridge}
661 assert device_exists
${device}
663 # If device is already attached, exit silently
664 if listmatch
${device} $
(bridge_get_members
${bridge}); then
668 log INFO
"Attaching device '${device}' to bridge '${bridge}'."
670 # XXX device_set_up ${device} # Do we need this here?
672 brctl addif
${bridge} ${device}
675 function bridge_detach_device
() {
682 if ! device_exists
${bridge}; then
683 error
"Bridge '${bridge}' does not exist."
687 if ! device_exists
${device}; then
688 error
"Device '${device}' does not exist."
692 log INFO
"Detaching device '${device}' from bridge '${bridge}'."
694 brctl delif
${bridge} ${device}
696 #device_set_down ${device}
699 function bridge_get_members
() {
705 for member
in ${SYS_CLASS_NET}/${bridge}/brif
/*; do
706 member
=$
(basename ${master})
707 if device_exists
${member}; then
713 function bridge_is_forwarding
() {
717 bridge_has_carrier
${zone} ||
return ${EXIT_ERROR}
720 while [ ${seconds} -gt 0 ]; do
721 for device
in ${SYS_CLASS_NET}/${zone}/brif
/*; do
722 [ -e "${device}/state" ] ||
continue
723 if [ "$(<${device}/state)" = "3" ]; then
728 seconds
=$
((${seconds} - 1))
734 function bridge_has_carrier
() {
737 local has_carrier
=${EXIT_ERROR}
740 for device
in ${SYS_CLASS_NET}/${zone}/brif
/*; do
741 device
=$
(basename ${device})
742 device_exists
${device} ||
continue
744 device_has_carrier
${device} && has_carrier
=${EXIT_OK}
747 return ${has_carrier}
750 function device_has_ipv4
() {
754 if ! device_exists
${device}; then
755 error
"Device '${device}' does not exist."
759 ip addr show
${device} |
grep -q -e "inet " -e "${addr}"
762 function device_has_ipv6
() {
766 if ! device_exists
${device}; then
767 error
"Device '${device}' does not exist."
771 local prefix
=${addr##*/}
772 addr
=$
(ipv6_implode
${addr%%/*})
774 if [ -n "${prefix}" ]; then
775 addr
="${addr}/${prefix}"
778 ip addr show
${device} |
grep -q "inet6 ${addr}"
781 function __device_get_file
() {
788 cat ${SYS_CLASS_NET}/${device}/${file}
791 function device_get_rx_bytes
() {
794 __device_get_file
${device} statistics
/rx_bytes
797 function device_get_tx_bytes
() {
800 __device_get_file
${device} statistics
/tx_bytes
803 function device_get_rx_packets
() {
806 __device_get_file
${device} statistics
/rx_packets
809 function device_get_tx_packets
() {
812 __device_get_file
${device} statistics
/tx_packets
815 function device_get_rx_errors
() {
818 __device_get_file
${device} statistics
/rx_errors
821 function device_get_tx_errors
() {
824 __device_get_file
${device} statistics
/tx_errors