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 # Check if the device is a ppp device
125 function device_is_ppp
() {
128 local type=$
(__device_get_file
${device} type)
129 if [ "${type}" = "512" ]; then
136 # Check if the device is a loopback device
137 function device_is_loopback
() {
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} && \
162 [ "$(__device_get_file ${device} type)" != "1" ] && \
168 # Get the device type
169 function device_get_type
() {
172 if device_is_virtual
${device}; then
175 elif device_is_bonding
${device}; then
178 elif device_is_bridge
${device}; then
181 elif device_is_ppp
${device}; then
184 elif device_is_loopback
${device}; then
187 elif device_is_real
${device}; then
195 function device_get_status
() {
200 local status
=${STATUS_UNKNOWN}
202 if ! device_has_carrier
${device}; then
203 status
=${STATUS_NOCARRIER}
204 elif device_is_up
${device}; then
206 elif device_is_down
${device}; then
207 status
=${STATUS_DOWN}
215 function device_get_address
() {
218 cat ${SYS_CLASS_NET}/${device}/address
2>/dev
/null
221 function device_set_address
() {
225 if ! device_exists
${device}; then
226 error
"Device '${device}' does not exist."
230 log INFO
"Setting address of '${device}' to '${addr}' - was $(device_get_address ${device})."
233 if device_is_up
${device}; then
234 device_set_down
${device}
238 ip link
set ${device} address
${addr}
241 if [ "${up}" = "1" ]; then
242 device_set_up
${device}
245 if [ "${ret}" != "0" ]; then
246 error_log
"Could not set address '${addr}' on device '${device}'."
252 function device_get
() {
256 for device
in ${SYS_CLASS_NET}/*; do
257 device
=$
(basename ${device})
259 # bonding_masters is no device
260 [ "${device}" = "bonding_masters" ] && continue
262 devices
="${devices} ${device}"
269 function devices_get_all
() {
273 # Check if a device has a cable plugged in
274 function device_has_carrier
() {
278 [ "$(<${SYS_CLASS_NET}/${device}/carrier)" = "1" ]
281 function device_is_promisc
() {
284 ip link show
${device} |
grep -qE "<.*PROMISC.*>"
287 function device_set_promisc
() {
291 assert device_exists
${device}
293 assert isoneof state on off
295 ip link
set ${device} promisc
${state}
298 # Check if the device is free
299 function device_is_free
() {
303 # Check if the device is used
304 function device_is_used
() {
307 device_has_virtuals
${device} && \
309 device_is_bonded
${device} && \
311 device_is_bridge_attached
${device} && \
317 function device_hash
() {
320 # Get mac address of device and remove all colons (:)
321 # that will result in a hash.
322 device
=$
(macify
${device})
327 # Give the device a new name
328 function device_set_name
() {
330 local destination
=${2}
332 # Check if devices exists
333 if ! device_exists
${source} || device_exists
${destination}; then
338 if device_is_up
${source}; then
339 ip link
set ${source} down
343 ip link
set ${source} name
${destination}
345 if [ "${up}" = "1" ]; then
346 ip link
set ${destination} up
351 function device_set_up
() {
354 # Silently fail if device was not found
355 [ -z "${device}" ] && return ${EXIT_ERROR}
357 # Do nothing if device is already up
358 device_is_up
${device} && return ${EXIT_OK}
360 device_set_parent_up
${device}
362 log DEBUG
"Setting up device '${device}'"
364 ip link
set ${device} up
367 function device_set_parent_up
() {
371 if device_is_virtual
${device}; then
372 parent
=$
(virtual_get_parent
${device})
374 device_is_up
${parent} && return ${EXIT_OK}
376 log DEBUG
"Setting up parent device '${parent}' of '${device}'"
378 device_set_up
${parent}
386 function device_set_down
() {
392 if device_is_up
${device}; then
393 log DEBUG
"Tearing down device '${device}'"
395 ip link
set ${device} down
399 device_set_parent_down
${device}
404 function device_set_parent_down
() {
408 if device_is_virtual
${device}; then
409 parent
=$
(virtual_get_parent
${device})
411 device_is_up
${parent} ||
return ${EXIT_OK}
413 if device_is_free
${parent}; then
414 log DEBUG
"Tearing down parent device '${parent}' of '${device}'"
416 device_set_down
${parent}
423 function device_get_mtu
() {
426 if ! device_exists
${device}; then
427 error
"Device '${device}' does not exist."
431 echo $
(<${SYS_CLASS_NET}/${device}/mtu
)
434 # Set mtu to a device
435 function device_set_mtu
() {
439 if ! device_exists
${device}; then
440 error
"Device '${device}' does not exist."
444 local oldmtu
=$
(device_get_mtu
${device})
446 if [ "${oldmtu}" = "${mtu}" ]; then
447 # No need to set mtu.
451 log INFO
"Setting mtu of '${device}' to '${mtu}' - was ${oldmtu}."
454 if device_is_up
${device}; then
455 device_set_down
${device}
459 ip link
set ${device} mtu
${mtu}
462 if [ "${up}" = "1" ]; then
463 device_set_up
${device}
466 if [ "${ret}" != "0" ]; then
467 error_log
"Could not set mtu '${mtu}' on device '${device}'."
473 function device_discover
() {
476 log INFO
"Running discovery process on device '${device}'."
479 for hook
in $
(hook_zone_get_all
); do
480 hook_zone_exec
${hook} discover
${device}
484 function device_has_ip
() {
489 assert device_exists
${device}
491 # IPv6 addresses must be fully imploded
492 local protocol
=$
(ip_detect_protocol
${addr})
493 case "${protocol}" in
495 addr
=$
(ipv6_implode
${addr})
499 listmatch
${addr} $
(device_get_addresses
${device})
502 function device_get_addresses
() {
505 assert device_exists
${device}
510 ip addr show
${device} | \
511 while read prot addr line
; do
512 [ "${prot:0:4}" = "inet" ] && echo "${addr}"
516 function __device_get_file
() {
523 cat ${SYS_CLASS_NET}/${device}/${file}
526 function device_get_rx_bytes
() {
529 __device_get_file
${device} statistics
/rx_bytes
532 function device_get_tx_bytes
() {
535 __device_get_file
${device} statistics
/tx_bytes
538 function device_get_rx_packets
() {
541 __device_get_file
${device} statistics
/rx_packets
544 function device_get_tx_packets
() {
547 __device_get_file
${device} statistics
/tx_packets
550 function device_get_rx_errors
() {
553 __device_get_file
${device} statistics
/rx_errors
556 function device_get_tx_errors
() {
559 __device_get_file
${device} statistics
/tx_errors
562 function device_hotplug
() {
568 # Just check if the device has already vanished.
569 device_exists
${device} ||
return ${EXIT_ERROR}
571 if ! device_is_free
${device}; then
572 log ERROR
"The device '${device}' is in use."
576 if ! device_is_real
${device}; then
577 log DEBUG
"Don't rename any virtual devices."
581 for port
in $
(ports_get_all
); do
582 port_cmd hotplug
${port} ${device}
583 if [ $?
-eq ${EXIT_OK} ]; then
589 # If no port configuration could be found, we search for the next
590 # unused name and return that.
591 local port
=$
(port_find_free
${PORT_PATTERN})
593 log DEBUG
"Could not find an existing port configuration for '${device}'."
594 log DEBUG
"${device} --> ${port}"