. /usr/lib/network/header-port
-HOOK_SETTINGS="HOOK ADDRESS PARENT_DEVICE TAG"
+HOOK_SETTINGS=(
+ "ADDRESS"
+ "ID"
+ "PARENT_PORT"
+)
-PORT_PARENTS_VAR="PARENT"
+PORT_PARENTS_VAR="PARENT_PORT"
-function hook_check() {
- assert isset PARENT_DEVICE
- assert isinteger TAG
+hook_check_settings() {
+ assert isset PARENT_PORT
+ assert isinteger ID
if isset ADDRESS; then
assert ismac ADDRESS
fi
- if [ ${TAG} -gt 4096 ]; then
- error "TAG is greater than 4096."
+ if [ ${ID} -gt 4096 ]; then
+ error "ID is greater than 4096."
exit ${EXIT_ERROR}
fi
local reserved
for reserved in 0 4095; do
- if [ "${TAG}" = "${reserved}" ]; then
- error "TAG=${reserved} is reserved."
+ if [ "${ID}" = "${reserved}" ]; then
+ error "ID=${reserved} is reserved."
exit ${EXIT_ERROR}
fi
done
}
-function hook_create() {
- while [ $# -gt 0 ]; do
- case "${1}" in
- --parent-device=*)
- PARENT_DEVICE=$(cli_get_val ${1})
- ;;
- --address=*)
- ADDRESS=$(cli_get_val ${1})
- ;;
- --tag=*)
- TAG=$(cli_get_val ${1})
- ;;
- *)
- warning "Unknown argument '${1}'"
- ;;
- esac
- shift
- done
-
- local port="${PARENT_DEVICE}${VLAN_PORT_INTERFIX}${TAG}"
+hook_find_port_name() {
+ assert isset ID
+ assert isset PARENT_PORT
- config_write $(port_file ${port}) ${HOOK_SETTINGS}
-
- exit ${EXIT_OK}
+ print "${PARENT_PORT}${VLAN_PORT_INTERFIX}${ID}"
}
-function hook_edit() {
- local port=${1}
- assert isset port
- shift
-
- config_read $(port_file ${port})
-
+hook_parse_cmdline() {
while [ $# -gt 0 ]; do
case "${1}" in
--address=*)
- ADDRESS=$(cli_get_val ${1})
+ ADDRESS=$(cli_get_val "${1}")
+
+ # Validate address
+ if ! mac_is_valid "${ADDRESS}"; then
+ error "Invalid MAC address given: ${ADDRESS}"
+ return ${EXIT_CONF_ERROR}
+ fi
+ ;;
+ --id=*)
+ ID=$(cli_get_val "${1}")
+ ;;
+ --port=*)
+ PARENT_PORT=$(cli_get_val "${1}")
+
+ # Check if PARENT_PORT exists
+ if ! port_exists "${PARENT_PORT}"; then
+ error "Port '${PARENT_PORT}' does not exist"
+ return ${EXIT_CONF_ERROR}
+ fi
;;
*)
- warning "Unknown argument '${1}'"
+ error "Unknown argument '${1}'"
+ return ${EXIT_CONF_ERROR}
;;
esac
shift
done
- config_write $(port_file ${port}) ${HOOK_SETTINGS}
-
- exit ${EXIT_OK}
+ # Generate a random MAC address if none given
+ if ! isset ADDRESS; then
+ ADDRESS="$(mac_generate)"
+ fi
}
-function hook_up() {
- local port=${1}
+hook_create() {
+ local port="${1}"
assert isset port
- if ! device_exists ${port}; then
- # Read configuration file.
- config_read $(port_file ${port}) ${HOOK_SETTINGS}
+ device_exists "${port}" && exit ${EXIT_OK}
- vlan_create ${port} ${PARENT_DEVICE} ${TAG} ${ADDRESS}
+ # Read configruation
+ if ! port_settings_read "${port}"; then
+ return ${EXIT_ERROR}
fi
- # Bring up the device.
- device_set_up ${port}
+ # Check if the parent port exists
+ if ! port_exists "${PARENT_PORT}"; then
+ error "Port '${PARENT_PORT}' does not exist"
+ return ${EXIT_ERROR}
+ fi
- exit ${EXIT_OK}
+ # Create the partent port first
+ if ! port_create "${PARENT_PORT}"; then
+ error "Could not bring up parent port: ${PARENT_PORT}"
+ return ${EXIT_ERROR}
+ fi
+
+ # Create the VLAN device
+ if ! vlan_create "${port}" \
+ --address="${ADDRESS}" \
+ --id="${id}" \
+ --parent="${PARENT_PORT}"; then
+ error "Could not create port: ${port}"
+ return ${EXIT_ERROR}
+ fi
+
+ return ${EXIT_OK}
}
-function hook_down() {
- local port=${1}
+hook_remove() {
+ local port="${1}"
assert isset port
- # Exit, if the port does not exist.
- if ! device_exists ${port}; then
- exit ${EXIT_OK}
+ if device_exists "${port}"; then
+ vlan_remove "${port}"
fi
- # Tear down the port.
- device_set_down ${port}
-
- # Remove the port.
- vlan_remove ${port}
-
exit ${EXIT_OK}
}