EXIT_ERROR=1
EXIT_CONF_ERROR=2
+VALID_ZONES="blue green orange red grey"
+
[ -n "${DEBUG}" ] || DEBUG=
[ -n "${VERBOSE}" ] || VERBOSE=
function is_mac() {
- egrep -q "^[0-9a-f][0-9a-f]\:[0-9a-f][0-9a-f]\:[0-9a-f][0-9a-f]\:[0-9a-f][0-9a-f]\:[0-9a-f][0-9a-f]\:[0-9a-f][0-9a-f]$" <<<$1
+ [[ $1 =~ ^[0-9a-f][0-9a-f]\:[0-9a-f][0-9a-f]\:[0-9a-f][0-9a-f]\:[0-9a-f][0-9a-f]\:[0-9a-f][0-9a-f]\:[0-9a-f][0-9a-f]$ ]]
}
function get_device_by_mac() {
- local mac
+ local mac=${1}
local device
- mac=$1
-
for device in /sys/class/net/*; do
if [ "$(cat $device/address)" = "$mac" ]; then
device=${device##*/}
for hook in $(find ${dir} -type f); do
(
. ${hook}
- if [ -n "${HOOK}" ] && hook_exists ${HOOK}; then
- /lib/network/hooks/${HOOK} --config=${hook} $@ ${action}
+ if [ -n "${HOOK}" ]; then
+ hook_run ${HOOK} --config=${hook} $@ ${action}
RET=$?
else
echo -e "${FAILURE}Unable to process ${hook}. Either"
log_failure_msg "Hook ${hook} cannot be found or is not executeable."
return ${EXIT_ERROR}
fi
- decho "Running hook: ${hook} $@"
+ [ -n "${DEBUG}" ] && echo "Running hook: ${hook} $@"
DEBUG=${DEBUG} VERBOSE=${VERBOSE} ${HOOKS_DIR}/${hook} $@
return $?
}
DEBUG=${DEBUG} VERBOSE=${VERBOSE} ${HOME_DIR}/zone --zone=${zone} $@
return $?
}
+
+function zone_valid_name() {
+ local zone=${1}
+ local match
+
+ local i
+ for i in ${VALID_ZONES}; do
+ match="${match}|${i}[0-9]{1,5}"
+ done
+ [[ ${zone} =~ ${match:1:${#match}} ]]
+ return $?
+}
+
+function isset() {
+ local key=${1}
+ [ -n "${!key}" ] && return
+ if [[ ${key} =~ port|zone ]]; then
+ echo "ERROR: The --${key} flag is not set." >&2
+ else
+ echo "ERROR: The \"${key}\" variable is not set properly." >&2
+ fi
+ return 1
+}
+
+# Test if device is attached to the given bridge
+function zone_has_device_attached () {
+ local zone=${1}
+ local device=${2}
+
+ if device_exists ${device} || zone_exists ${zone}; then
+ return 2
+ fi
+
+ [ -d "/sys/class/net/${zone}/brif/${device}" ]
+ return $?
+}
+
+function device_has_ipv4() {
+ local device=${1}
+ local ip=${2}
+ ip addr show ${device} | grep inet | fgrep -q ${ip}
+}
+
+function check_config() {
+ local failed
+ local i
+
+ for i in $@; do
+ isset ${i} || failed=1
+ done
+ if [ "${failed}" = "1" ]; then
+ echo "Exiting..."
+ exit ${EXIT_ERROR}
+ fi
+}
HOOK_NAME=ethernet
HOOK_TYPE=port
-function check_config() {
- : # XXX TODO
-}
-
function port_name() {
echo ${zone}s+
}
function do_up() {
- : # Do nothing
+ device_is_up ${port} || ip link set $(devicify ${port}) up
}
function do_down() {
- : # Do nothing
+ : # TODO
+ # Check if there are depending devices (like vlans, etc)
+ # If not, then pull down the device.
}
function do_attach() {
+ if zone_has_device_attached ${zone} $(get_device ${port}); then
+ # Device is already attached to the bridge
+ return 0
+ fi
device_rename $(get_device ${port}) $(port_name)
zone_add_port ${zone} $(get_device_by_mac ${port})
}
function do_detach() {
- zone_del_port ${zone} $(get_device_by_mac ${port})
- device_rename $(get_device_by_mac ${port}) ${COMMON_DEVICE}
+ if zone_has_device_attached ${zone} $(get_device ${port}); then
+ zone_del_port ${zone} $(get_device_by_mac ${port})
+ device_rename $(get_device_by_mac ${port}) ${COMMON_DEVICE}
+ fi
}
function do_status() {
;;
up)
+ check_config port zone
do_up
- ;;
+ ;;
down)
+ check_config port zone
do_down
- ;;
+ ;;
add)
cat <<EOF > ${CONFIG_PORTS}/${port}/ethernet
EOF
ln -sf ${CONFIG_PORTS}/${port}/ethernet \
${CONFIG_ZONES}/${zone}/port-${port}-ethernet
- ;;
+ ;;
remove)
+ check_config port zone
do_detach
- #do_down
+ do_down
rm -f \
${CONFIG_ZONES}/${zone}/port-${port}-ethernet \
${CONFIG_PORTS}/${port}/ethernet
- ;;
+ ;;
attach)
+ check_config port zone
do_up
do_attach
- ;;
+ ;;
detach)
+ check_config port zone
do_detach
- ;;
+ do_down
+ ;;
status)
+ check_config port zone
do_status
exit ${?}
- ;;
+ ;;
*)
echo "Usage: ${0} [interface] {up|down|add|remove|attach|detach|status}"
exit 1
- ;;
+ ;;
esac
# End $NETWORK_DEVICES/services/ethernet
post-up)
check_config
- MESSAGE="Adding IPv4 address ${IP} to zone ${zone} interface..."
- ip addr add ${args} dev ${zone}
- evaluate_retval
+ if ! device_has_ipv4 ${zone} ${IP}; then
+ MESSAGE="Adding IPv4 address ${IP} to zone ${zone} interface..."
+ ip addr add ${args} dev ${zone}
+ evaluate_retval
+ fi
if [ -n "${GATEWAY}" ]; then
if ip route | grep -q default; then
evaluate_retval
fi
- MESSAGE="Removing IPv4 address ${IP} from zone ${zone}..."
- ip addr del ${args} dev ${zone}
- evaluate_retval
+ if device_has_ipv4 ${zone} ${IP}; then
+ MESSAGE="Removing IPv4 address ${IP} from zone ${zone}..."
+ ip addr del ${args} dev ${zone}
+ evaluate_retval
+ fi
;;
post-down)
;;
config)
- shift 2
while [ $# -gt 0 ]; do
case "$1" in
--ip=*)
esac
shift
done
- check_config
+ #check_config
cat <<EOF >${CONFIG_ZONES}/${zone}/ipv4-static_$IP
HOOK="${HOOK_NAME}"
IP="${IP}"
HOOK_NAME=vlan
HOOK_TYPE=port
-function check_config() {
- : # TODO
-}
-
function port_name() {
- echo "${ZONE}v${ID}"
+ echo "${zone}v${ID}"
}
function do_up() {
if ! port_is_up $(port_name); then
grep -q ^8021q /proc/modules || modprobe 8021q
MESSAGE="Adding VLAN ${ID} to port ${port}..."
- vconfig add ${device} ${ID} >/dev/null
+ vconfig add $(devicify ${port}) ${ID} >/dev/null
evaluate_retval
+ ebtables -t broute -A BROUTING -p 802_1Q -i $(devicify ${port}) -j DROP
fi
}
function do_down() {
if port_is_up $(port_name); then
MESSAGE="Removing VLAN ${ID} from port ${port}..."
- vconfig rem ${device} ${ID} >/dev/null
+ vconfig rem $(get_device_by_mac_and_vid ${port} ${ID}) >/dev/null
evaluate_retval
+ ebtables -t broute -D BROUTING -p 802_1Q -i $(devicify ${port}) -j DROP
fi
}
function do_attach() {
- rename_device $(get_device_by_mac_and_vid ${port} ${ID}) $(port_name)
- zone_add_port ${ZONE} $(get_device ${port} ${ID})
+ device_rename $(get_device_by_mac_and_vid ${port} ${ID}) $(port_name)
+ zone_add_port ${zone} $(get_device ${port} ${ID})
}
function do_detach() {
- zone_del_port ${ZONE} $(get_device_by_mac_and_vid ${port} ${ID})
+ zone_del_port ${zone} $(get_device_by_mac_and_vid ${port} ${ID})
}
function do_status() {
case "${1}" in
--port=*)
port=$(macify ${1#--port=})
- device=$(devicify ${port})
;;
--config=*)
. ${1#--config=}
done
case "${action}" in
+ help)
+ ;;
+
+ info)
+ echo "HOOK_NAME=${HOOK_NAME}"
+ echo "HOOK_TYPE=${HOOK_TYPE}"
+ ;;
+
up)
+ check_config port ID
do_up
- ;;
+ ;;
down)
+ check_config port ID
do_down
- ;;
+ ;;
add)
- ID=$3
+ ID=$1
+ check_config port zone ID
cat <<EOF > ${CONFIG_PORTS}/${port}/vlan-${ID}
HOOK=vlan
ID=${ID}
EOF
ln -sf ${CONFIG_PORTS}/${port}/vlan-${ID} \
- ${CONFIG_ZONES}/${ZONE}/port-${port}-vlan-${ID}
- ;;
+ ${CONFIG_ZONES}/${zone}/port-${port}-vlan-${ID}
+ ;;
remove)
+ check_config port zone ID
do_detach
do_down
rm -f \
${CONFIG_PORTS}/${port}/vlan-${ID} \
- ${CONFIG_ZONES}/${ZONE}/port-${port}-vlan-${ID}
- ;;
+ ${CONFIG_ZONES}/${zone}/port-${port}-vlan-${ID}
+ ;;
attach)
+ check_config port zone ID
+ do_up
do_attach
- ;;
-
+ ;;
+
detach)
+ check_config port zone ID
do_detach
- ;;
+ do_down
+ ;;
status)
+ check_config zone ID
do_status
exit ${?}
- ;;
+ ;;
*)
echo "Usage: ${0} [interface] {up|down|add|remove|attach|detach|status}"
exit 1
fi
-device=$(devicify ${port})
-
for arg in ${@-$(find ${CONFIG_PORTS}/${port}/)}; do
[ -L "${arg}" ] && arg=$(readlink ${arg})
arg=${arg##*/}
;;
attach)
- ip link set $(devicify ${port}) up # is this required here?
;;
detach)
fi
;;
+ reload)
+ if ! zone_is_up ${zone}; then
+ $0 --zone=${zone} start
+ exit $?
+ fi
+
+ # Attach all ports
+ for config in $(find ${CONFIG_ZONES}/${zone}/ -name "port-*" 2>/dev/null); do
+ port=${config##*/}; port=${port#port-}; port=${port%%-*}
+ /lib/network/port --port=${port} --zone=${zone} attach ${config}
+ done
+
+ run_hooks post-up ${CONFIG_ZONES}/${zone} --zone=${zone}
+ ;;
+
*)
exit 1
;;
error "Zone ${BOLD}${zone}${NORMAL} already exists."
return 1
fi
+
+ if ! zone_valid_name ${zone}; then
+ error "The given zone name is not valid."
+ return 1
+ fi
mkdir -p ${CONFIG_ZONES}/${zone}
vecho "Successfully added zone ${BOLD}${zone}${NORMAL}."