From 3a7fef62da13150f2025ee5cc6d587899ba0710c Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Wed, 3 Aug 2011 17:17:24 +0000 Subject: [PATCH] Add hotplugging for ethernet devices. --- functions.colors | 12 +++++-- functions.ports | 18 ++++++++++ functions.util | 5 +++ functions.zone | 32 ++++++++++++++++++ udev/network-hotplug | 70 +++++++++++++++++++++++++++++++++++++++ udev/rules.d/60-net.rules | 2 +- 6 files changed, 136 insertions(+), 3 deletions(-) create mode 100755 udev/network-hotplug diff --git a/functions.colors b/functions.colors index f0da0b4c..d3a6e2c1 100644 --- a/functions.colors +++ b/functions.colors @@ -45,10 +45,18 @@ COLOUR_STP_DISCARDING=${COLOUR_RED} COLOUR_STP_LEARNING=${COLOUR_YELLOW} COLOUR_STP_BLOCKING=${COLOUR_YELLOW} +COLOUR_VARIABLES="COLOUR_GREEN COLOUR_RED COLOUR_NORMAL COLOUR_YELLOW" +COLOUR_VARIABLES="${COLOUR_VARIABLES} COLOUR_BOLD COLOUR_DOWN COLOUR_ERROR" +COLOUR_VARIABLES="${COLOUR_VARIABLES} COLOUR_ERROR COLOUR_OK COLOUR_UP" +COLOUR_VARIABLES="${COLOUR_VARIABLES} COLOUR_WARM COLOUR_ENABLED COLOUR_DISABLED" +COLOUR_VARIABLES="${COLOUR_VARIABLES} COLOUR_LOG COLOUR_STP_FORWARDING" +COLOUR_VARIABLES="${COLOUR_VARIABLES} COLOUR_STP_DISCARDING COLOUR_STP_LEARNING" +COLOUR_VARIABLES="${COLOUR_VARIABLES} COLOUR_STP_BLOCKING" + function colours_disable() { local line - for line in $(set | grep "^COLOUR_"); do - unset ${line%%=*} + for line in ${COLOUR_VARIABLES}; do + unset ${line} done } diff --git a/functions.ports b/functions.ports index 6460ab19..cb247a16 100644 --- a/functions.ports +++ b/functions.ports @@ -260,3 +260,21 @@ function port_get_children() { port_get_info ${port} PORT_CHILDREN } + +function port_zone() { + # Get name of the zones, this port is configured in. + local port=${1} + shift + + assert isset port + + local zone + for zone in $(zones_get_all); do + if zone_has_port ${zone} ${port}; then + echo "${zone}" + return ${EXIT_OK} + fi + done + + return ${EXIT_OK} +} diff --git a/functions.util b/functions.util index fa9005f6..7da58f7a 100644 --- a/functions.util +++ b/functions.util @@ -444,3 +444,8 @@ function dec() { printf "%d\n" "${hex}" } + +function network_is_running() { + # Check, if the network service is running. + service_is_active network +} diff --git a/functions.zone b/functions.zone index 31c66fb1..79c6e0c0 100644 --- a/functions.zone +++ b/functions.zone @@ -321,6 +321,20 @@ function zone_get_ports() { done } +function zone_has_port() { + # Check, if the given port is configured + # in this zone. + + local zone=${1} + local port=${2} + shift 2 + + assert isset zone + assert isset port + + [ -e "$(zone_dir ${zone})/ports/${port}" ] +} + # XXX overwritten some lines below function zone_config() { local zone=${1} @@ -374,6 +388,24 @@ function zone_config() { esac } +function zone_config_option() { + local zone=${1} + local option=${2} + local default=${3} + shift 2 + + assert isset zone + assert isset option + + ( + VALUE="${default}" + zone_config_read ${zone} + + VALUE="${!option}" + echo "${VALUE}" + ) +} + function zone_config_create() { local zone=${1} shift diff --git a/udev/network-hotplug b/udev/network-hotplug new file mode 100755 index 00000000..d4a76cb9 --- /dev/null +++ b/udev/network-hotplug @@ -0,0 +1,70 @@ +#!/bin/bash +############################################################################### +# # +# IPFire.org - A linux based firewall # +# Copyright (C) 2011 Michael Tremer & Christian Schmidt # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +############################################################################### + +. /lib/network/functions + +# Do nothing, if the network isn't running. +# That happens for example on boot time. +if ! network_is_running; then + exit 0 +fi + +# Check if the udev environment variables are properly set. +assert isset ACTION +assert isset INTERFACE + +# Check, if the device is a physical network interface and +# if we can handle it. +if ! device_is_real ${INTERFACE}; then + exit ${EXIT_OK} +fi + +# Check, if there is a configuration for that device. +if port_exists ${INTERFACE}; then + port=${INTERFACE} + +else + # If the given device was not configured, + # we create an initial configuration. + port_create ethernet ${INTERFACE} + exit ${EXIT_OK} +fi + +case "${ACTION}" in + add|register) + zone=$(port_zone ${port}) + + # Check, if the device is configured in a zone. + # If not, there is nothing to do. + isset zone || exit ${EXIT_OK} + + boot=$(zone_config_option ${zone} BOOT) + if enabled boot; then + zone_up ${zone} + fi + ;; + + remove|unregister) + port_down ${port} + ;; +esac + +exit ${EXIT_OK} diff --git a/udev/rules.d/60-net.rules b/udev/rules.d/60-net.rules index 7c89ccf1..6d7bc0b3 100644 --- a/udev/rules.d/60-net.rules +++ b/udev/rules.d/60-net.rules @@ -5,4 +5,4 @@ ACTION=="add", SUBSYSTEM=="net", PROGRAM="/lib/udev/network-hotplug-rename", RESULT=="?*", ENV{INTERFACE_NAME}="$result" # Handle all plugged-in devices. -#SUBSYSTEM=="net", RUN+="/lib/udev/network-hotlug" +SUBSYSTEM=="net", RUN+="/lib/udev/network-hotplug" -- 2.47.2