From: Michael Tremer Date: Sun, 31 Aug 2014 21:55:11 +0000 (+0200) Subject: pppoe: Allow assigning ports to pppoe zones X-Git-Tag: 007~98 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=529141df70bc619df04407c0d0971cf663df00de;p=network.git pppoe: Allow assigning ports to pppoe zones --- diff --git a/src/functions/functions.zone b/src/functions/functions.zone index 75ee70eb..51351172 100644 --- a/src/functions/functions.zone +++ b/src/functions/functions.zone @@ -458,6 +458,24 @@ function zone_get_ports() { done } +function zone_get_ports_num() { + local zone="${1}" + assert isset zone + + local counter=0 + local port + for port in $(zone_dir "${zone}")/ports/*; do + port="$(basename "${port}")" + + if port_exists "${port}"; then + counter=$(( ${counter} + 1 )) + fi + done + + echo "${counter}" + return ${EXIT_OK} +} + function zone_has_port() { # Check, if the given port is configured # in this zone. diff --git a/src/hooks/zones/pppoe b/src/hooks/zones/pppoe index ee893464..3882a2b3 100644 --- a/src/hooks/zones/pppoe +++ b/src/hooks/zones/pppoe @@ -22,7 +22,7 @@ . /usr/lib/network/header-zone HOOK_SETTINGS="HOOK ACCESS_CONCENTRATOR AUTH USERNAME PASSWORD" -HOOK_SETTINGS="${HOOK_SETTINGS} SERVICE_NAME MTU PORT IPV6 PREFIX_DELEGATION" +HOOK_SETTINGS="${HOOK_SETTINGS} SERVICE_NAME MTU IPV6 PREFIX_DELEGATION" # User credentials for the dialin. USERNAME="" @@ -31,9 +31,6 @@ PASSWORD="" # Set the authentication mechanism. AUTH= -# The physical ethernet port the modem is connected to. -PORT="" - # Access Concentrator. ACCESS_CONCENTRATOR="" @@ -60,10 +57,6 @@ function hook_check() { isset AUTH && assert isoneof AUTH ${PPPOE_SUPPORTED_AUTH_METHODS} - # Check for a valid port setting. - assert isset PORT - assert port_exists ${PORT} - assert isset IPV6 assert isset PREFIX_DELEGATION } @@ -91,9 +84,6 @@ function hook_parse_cmdline() { --password=*) PASSWORD=$(cli_get_val ${1}) ;; - --port=*) - PORT=$(cli_get_val ${1}) - ;; --prefix-delegation=*) PREFIX_DELEGATION="$(cli_get_bool "${1}")" ;; @@ -118,8 +108,8 @@ function hook_up() { zone_config_read ${zone} # Bring up the port. - log DEBUG "Bringing up port '${PORT}'." - port_up ${PORT} + local port=$(__hook_get_port "${zone}") + port_up "${port}" # Start the ppp daemon. pppd_start ${zone} @@ -184,7 +174,11 @@ function hook_status() { cli_headline 2 "Configuration" cli_print_fmt1 2 "Username" "${USERNAME}" cli_print_fmt1 2 "Password" "" - cli_print_fmt1 2 "Port" "${PORT}" + + local port=$(__hook_get_port "${zone}") + if isset port; then + cli_print_fmt1 2 "Port" "${port}" + fi cli_space # Exit if zone is down @@ -235,6 +229,13 @@ function hook_ppp_write_config() { # Read in the configuration files. zone_config_read ${zone} + # A port has to be assigned for this action + local port=$(__hook_get_port "${zone}") + if ! isset port; then + error "No port assigned to pppoe hook of zone '${zone}'" + exit ${EXIT_ERROR} + fi + # Prepare the command line options for the pppoe plugin. local plugin_options @@ -249,7 +250,7 @@ function hook_ppp_write_config() { fi # The last argument must be the interface. - plugin_options="${plugin_options} ${PORT}" + plugin_options="${plugin_options} ${port}" pppd_write_config ${file} \ --interface="${zone}" \ @@ -264,3 +265,53 @@ function hook_ppp_write_config() { exit ${EXIT_OK} } + +function __hook_get_port() { + local zone="${1}" + + local port + for port in $(zone_get_ports "${zone}"); do + echo "${port}" + return ${EXIT_OK} + done + + return ${EXIT_ERROR} +} + +function hook_port_add() { + # Excepting at least two arguments here + assert [ $# -ge 2 ] + + local zone="${1}" + local port="${2}" + shift 2 + + # PPPoE can only use one port + local ports_num="$(zone_get_ports_num "${zone}")" + if [ ${ports_num} -ge 1 ]; then + local port=$(__hook_get_port "${zone}") + error "The pppoe zone hook only supports assigning one port" + error " port '${port}' has already been assigned to zone '${zone}'" + return ${EXIT_ERROR} + fi + + config_write "$(zone_dir "${zone}")/ports/${port}" + log INFO "Port '${port}' has been added to zone '${zone}'" + + exit ${EXIT_OK} +} + +function hook_port_remove() { + assert [ $# -eq 2 ] + + local zone="${1}" + local port="${2}" + + # Shut down the port (if possible) + port_down "${port}" + + log INFO "Port '${port}' has been removed from zone '${zone}'" + config_remove "$(zone_dir "${zone}")/ports/${port}" + + exit ${EXIT_OK} +}