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
() {
138 local device
=$
(devicify
${1})
139 [ "${device}" = "lo" ]
142 # Check if the device is a physical network interface
143 function device_is_real
() {
146 device_is_loopback
${device} && \
149 device_is_bonding
${device} && \
152 device_is_bridge
${device} && \
155 device_is_ppp
${device} && \
158 device_is_virtual
${device} && \
161 [ "$(__device_get_file ${device} type)" != "1" ] && \
167 # Get the device type
168 function device_get_type
() {
169 local device
=$
(devicify
${1})
171 if device_is_virtual
${device}; then
174 elif device_is_bonding
${device}; then
177 elif device_is_bridge
${device}; then
180 elif device_is_ppp
${device}; then
183 elif device_is_loopback
${device}; then
186 elif device_is_real
${device}; then
194 function device_get_status
() {
199 local status
=${STATUS_UNKNOWN}
201 if ! device_has_carrier
${device}; then
202 status
=${STATUS_NOCARRIER}
203 elif device_is_up
${device}; then
205 elif device_is_down
${device}; then
206 status
=${STATUS_DOWN}
214 function device_get_address
() {
217 cat ${SYS_CLASS_NET}/${device}/address
2>/dev
/null
220 function device_set_address
() {
224 if ! device_exists
${device}; then
225 error
"Device '${device}' does not exist."
229 log INFO
"Setting address of '${device}' to '${addr}' - was $(device_get_address ${device})."
232 if device_is_up
${device}; then
233 device_set_down
${device}
237 ip link
set ${device} address
${addr}
240 if [ "${up}" = "1" ]; then
241 device_set_up
${device}
244 if [ "${ret}" != "0" ]; then
245 error_log
"Could not set address '${addr}' on device '${device}'."
251 function device_get
() {
255 for device
in ${SYS_CLASS_NET}/*; do
256 device
=$
(basename ${device})
258 # bonding_masters is no device
259 [ "${device}" = "bonding_masters" ] && continue
261 devices
="${devices} ${device}"
268 function devices_get_all
() {
272 # Check if a device has a cable plugged in
273 function device_has_carrier
() {
274 local device
=$
(devicify
${1})
275 [ "$(<${SYS_CLASS_NET}/${device}/carrier)" = "1" ]
278 function device_is_promisc
() {
281 ip link show
${device} |
grep -qE "<.*PROMISC.*>"
284 function device_set_promisc
() {
288 assert device_exists
${device}
290 assert isoneof state on off
292 ip link
set ${device} promisc
${state}
295 # Check if the device is free
296 function device_is_free
() {
300 # Check if the device is used
301 function device_is_used
() {
302 local device
=$
(devicify
${1})
304 device_has_virtuals
${device} && \
306 device_is_bonded
${device} && \
308 device_is_bridge_attached
${device} && \
314 function device_hash
() {
317 # Get mac address of device and remove all colons (:)
318 # that will result in a hash.
319 device
=$
(macify
${device})
324 # Give the device a new name
325 function device_set_name
() {
327 local destination
=${2}
329 # Check if devices exists
330 if ! device_exists
${source} || device_exists
${destination}; then
335 if device_is_up
${source}; then
336 ip link
set ${source} down
340 ip link
set ${source} name
${destination}
342 if [ "${up}" = "1" ]; then
343 ip link
set ${destination} up
348 function device_set_up
() {
349 local device
=$
(devicify
${1})
351 # Silently fail if device was not found
352 [ -z "${device}" ] && return ${EXIT_ERROR}
354 # Do nothing if device is already up
355 device_is_up
${device} && return ${EXIT_OK}
357 device_set_parent_up
${device}
359 log DEBUG
"Setting up device '${device}'"
361 ip link
set ${device} up
364 function device_set_parent_up
() {
368 if device_is_virtual
${device}; then
369 parent
=$
(virtual_get_parent
${device})
371 device_is_up
${parent} && return ${EXIT_OK}
373 log DEBUG
"Setting up parent device '${parent}' of '${device}'"
375 device_set_up
${parent}
383 function device_set_down
() {
384 local device
=$
(devicify
${1})
388 if device_is_up
${device}; then
389 log DEBUG
"Tearing down device '${device}'"
391 ip link
set ${device} down
395 device_set_parent_down
${device}
400 function device_set_parent_down
() {
404 if device_is_virtual
${device}; then
405 parent
=$
(virtual_get_parent
${device})
407 device_is_up
${parent} ||
return ${EXIT_OK}
409 if device_is_free
${parent}; then
410 log DEBUG
"Tearing down parent device '${parent}' of '${device}'"
412 device_set_down
${parent}
419 function device_get_mtu
() {
422 if ! device_exists
${device}; then
423 error
"Device '${device}' does not exist."
427 echo $
(<${SYS_CLASS_NET}/${device}/mtu
)
430 # Set mtu to a device
431 function device_set_mtu
() {
435 if ! device_exists
${device}; then
436 error
"Device '${device}' does not exist."
440 local oldmtu
=$
(device_get_mtu
${device})
442 if [ "${oldmtu}" = "${mtu}" ]; then
443 # No need to set mtu.
447 log INFO
"Setting mtu of '${device}' to '${mtu}' - was ${oldmtu}."
450 if device_is_up
${device}; then
451 device_set_down
${device}
455 ip link
set ${device} mtu
${mtu}
458 if [ "${up}" = "1" ]; then
459 device_set_up
${device}
462 if [ "${ret}" != "0" ]; then
463 error_log
"Could not set mtu '${mtu}' on device '${device}'."
469 function device_discover
() {
472 log INFO
"Running discovery process on device '${device}'."
475 for hook
in $
(hook_zone_get_all
); do
476 hook_zone_exec
${hook} discover
${device}
480 function device_has_ip
() {
485 assert device_exists
${device}
487 # IPv6 addresses must be fully imploded
488 local protocol
=$
(ip_detect_protocol
${addr})
489 case "${protocol}" in
491 addr
=$
(ipv6_implode
${addr})
495 listmatch
${addr} $
(device_get_addresses
${device})
498 function device_get_addresses
() {
501 assert device_exists
${device}
506 ip addr show
${device} | \
507 while read prot addr line
; do
508 [ "${prot:0:4}" = "inet" ] && echo "${addr}"
512 function __device_get_file
() {
519 cat ${SYS_CLASS_NET}/${device}/${file}
522 function device_get_rx_bytes
() {
525 __device_get_file
${device} statistics
/rx_bytes
528 function device_get_tx_bytes
() {
531 __device_get_file
${device} statistics
/tx_bytes
534 function device_get_rx_packets
() {
537 __device_get_file
${device} statistics
/rx_packets
540 function device_get_tx_packets
() {
543 __device_get_file
${device} statistics
/tx_packets
546 function device_get_rx_errors
() {
549 __device_get_file
${device} statistics
/rx_errors
552 function device_get_tx_errors
() {
555 __device_get_file
${device} statistics
/tx_errors
558 function device_hotplug
() {
563 assert device_exists
${device}
565 if ! device_is_free
${device}; then
566 log ERROR
"The device '${device}' is in use."
570 if ! device_is_real
${device}; then
571 log DEBUG
"Don't rename any virtual devices."
575 for port
in $
(ports_get_all
); do
576 port_cmd hotplug
${port} ${device}
577 if [ $?
-eq ${EXIT_OK} ]; then
583 # If no port configuration could be found, we search for the next
584 # unused name and return that.
585 local port
=$
(port_find_free
${PORT_PATTERN})
587 log DEBUG
"Could not find an existing port configuration for '${device}'."
588 log DEBUG
"${device} --> ${port}"