]> git.ipfire.org Git - network.git/commitdiff
dhcp: merge hooks ipv4-dhcp and ipv6-dhcp
authorJonatan Schlag <jonatan.schlag@ipfire.org>
Tue, 11 Jul 2017 14:43:02 +0000 (16:43 +0200)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 12 Jul 2017 22:02:02 +0000 (18:02 -0400)
Fixes: #11404
Signed-off-by: Jonatan Schlag <jonatan.schlag@ipfire.org>
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/hooks/configs/dhcp [moved from src/hooks/configs/ipv4-dhcp with 57% similarity]
src/hooks/configs/ipv6-dhcp [deleted file]

index 32e71660609504801da01e9a23a6440f96826e46..2bbc537861ba468124e523fb7e785d4dc3c0ffa0 100644 (file)
@@ -196,10 +196,9 @@ INSTALL_EXEC_HOOKS += bridge-stp-install-hook
 UNINSTALL_EXEC_HOOKS += bridge-stp-uninstall-hook
 
 dist_hooks_configs_SCRIPTS = \
-       src/hooks/configs/ipv4-dhcp \
+       src/hooks/configs/dhcp \
        src/hooks/configs/ipv4-static \
        src/hooks/configs/ipv6-auto \
-       src/hooks/configs/ipv6-dhcp \
        src/hooks/configs/ipv6-static \
        src/hooks/configs/pppoe-server
 
similarity index 57%
rename from src/hooks/configs/ipv4-dhcp
rename to src/hooks/configs/dhcp
index 39e03127920771ba9e582b884d0b24b8df2ca076..5ac01b6fd8e9a9e6906f8ee685f7a3939b6c0ebb 100644 (file)
 
 . /usr/lib/network/header-config
 
-HOOK_CONFIG_SETTINGS="HOOK DELAY"
+HOOK_CONFIG_SETTINGS="HOOK DELAY ENABLE_IPV6 ENABLE_IPV4"
 
 # Default settings.
 DELAY=0
+ENABLE_IPV6="on"
+ENABLE_IPV4="on"
 
 hook_check_config_settings() {
        assert isset DELAY
        assert isinteger DELAY
+       assert isset ENABLE_IPV6
+       assert isbool ENABLE_IPV6
+       assert isset ENABLE_IPV4
+       assert isbool ENABLE_IPV4
 }
 
-hook_new() {
-       local zone="${1}"
-       shift
-
-       if zone_config_hook_is_configured ${zone} "ipv4-dhcp"; then
-               log ERROR "You can configure the ipv4-dhcp hook only once for a zone"
-               return ${EXIT_ERROR}
-       fi
-
+hook_parse_cmdline() {
        while [ $# -gt 0 ]; do
                case "${1}" in
                        --delay=*)
                                DELAY="$(cli_get_val "${1}")"
                                ;;
+                       --enable-ipv6)
+                               ENABLE_IPV6="on"
+                               ;;
+                       --disable-ipv6)
+                               ENABLE_IPV6="off"
+                               ;;
+                       --enable-ipv4)
+                               ENABLE_IPV4="on"
+                               ;;
+                       --disable-ipv4)
+                               ENABLE_IPV4="off"
+                               ;;
+                       *)
+                               warning "Ignoring unknown option '${1}'"
+                               ;;
                esac
                shift
        done
+}
+
+hook_new() {
+       local zone="${1}"
+       shift
+
+       if zone_config_hook_is_configured ${zone} "dhcp"; then
+               log ERROR "You can configure the dhcp hook only once for a zone"
+               return ${EXIT_ERROR}
+       fi
+
+       if ! hook_parse_cmdline $@; then
+               # Return an error if the parsing of the cmd line fails
+               return ${EXIT_ERROR}
+       fi
+
+       # Check if the user disabled ipv4 and ipv6
+
+       if ! enabled ENABLE_IPV6 && ! enabled ENABLE_IPV4; then
+               log ERROR "You disabled IPv6 and IPv4. At least one must be enabled"
+               return ${EXIT_ERROR}
+       fi
 
        zone_config_settings_write "${zone}" "${HOOK}"
 
@@ -64,8 +99,17 @@ hook_up() {
                exit ${EXIT_ERROR}
        fi
 
-       # Start dhclient for IPv4 on this zone.
-       dhclient_start ${zone} ipv4
+       zone_config_settings_read "${zone}" "${config}"
+
+       # Start dhclient for IPv6 on this zone if enabled.
+       if enabled ENABLE_IPV6; then
+               dhclient_start ${zone} ipv6
+       fi
+
+       # Start dhclient for IPv4 on this zone if enabled.
+       if enabled ENABLE_IPV4; then
+               dhclient_start ${zone} ipv4
+       fi
 
        exit ${EXIT_OK}
 }
@@ -80,6 +124,9 @@ hook_down() {
                exit ${EXIT_ERROR}
        fi
 
+       # Stop dhclient for IPv6 on this zone.
+       dhclient_stop ${zone} ipv6
+
        # Stop dhclient for IPv4 on this zone.
        dhclient_stop ${zone} ipv4
 
@@ -99,24 +146,45 @@ hook_status() {
        zone_config_settings_read "${zone}" "${config}"
 
        local status
-       if dhclient_status ${zone} ipv4; then
+       if dhclient_status ${zone} ipv4 || dhclient_status ${zone} ipv6; then
                status="${MSG_HOOK_UP}"
        else
                status="${MSG_HOOK_DOWN}"
        fi
        cli_statusline 3 "${HOOK}" "${status}"
 
-       cli_print_fmt1 3 "IPv4 address" "$(db_get "${zone}/ipv4/local-ip-address")"
-       local gateway="$(db_get "${zone}/ipv4/remote-ip-address")"
-       if isset gateway; then
-               cli_print_fmt1 3 "Gateway" "${gateway}"
-               cli_space
-       fi
-       local dns_servers="$(db_get "${zone}/ipv4/domain-name-servers")"
-       if isset dns_servers; then
-               cli_print_fmt1 3 "DNS-Servers" "${dns_servers}"
+       cli_space
+
+       local proto
+       for proto in "IPv6" "IPv4"; do
+               local _proto=${proto,,}
+
+               cli_print_fmt1 3 "${proto}"
+
+               if enabled ENABLE_${proto^^}; then
+                       cli_print_fmt1 4 "Status" "enabled"
+
+                       local address="$(db_get "${zone}/${_proto}/local-ip-address")"
+                       if isset address; then
+                               cli_print_fmt1 4 "Address" "${address}"
+                       fi
+
+                       local gateway="$(db_get "${zone}/${_proto}/remote-ip-address")"
+                       if isset gateway; then
+                               cli_print_fmt1 4 "Gateway" "${gateway}"
+                       fi
+
+                       local dns_servers="$(db_get "${zone}/${_proto}/domain-name-servers")"
+                       if isset dns_servers; then
+                               cli_print_fmt1 4 "DNS Servers" "${dns_servers}"
+                       fi
+               else
+                       cli_print_fmt1 4 "Status" "disabled"
+               fi
+
                cli_space
-       fi
+
+       done
 
        exit ${EXIT_OK}
 }
diff --git a/src/hooks/configs/ipv6-dhcp b/src/hooks/configs/ipv6-dhcp
deleted file mode 100644 (file)
index 74ec765..0000000
+++ /dev/null
@@ -1,100 +0,0 @@
-#!/bin/bash
-###############################################################################
-#                                                                             #
-# IPFire.org - A linux based firewall                                         #
-# Copyright (C) 2010  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 <http://www.gnu.org/licenses/>.       #
-#                                                                             #
-###############################################################################
-
-. /usr/lib/network/header-config
-
-HOOK_CONFIG_SETTINGS="HOOK"
-
-hook_new() {
-       local zone="${1}"
-       shift
-
-       if zone_config_hook_is_configured ${zone} "ipv6-dhcp"; then
-               log ERROR "You can configure the ipv6-dhcp hook only once for a zone"
-               return ${EXIT_ERROR}
-       fi
-
-       zone_config_settings_write "${zone}" "${HOOK}"
-
-       exit ${EXIT_OK}
-}
-
-hook_up() {
-       local zone="${1}"
-       local config="${2}"
-       shift 2
-
-       if ! device_exists "${zone}"; then
-               error "Zone '${zone}' doesn't exist."
-               exit ${EXIT_ERROR}
-       fi
-
-       # Start dhclient for IPv6 on this zone.
-       dhclient_start "${zone}" "ipv6"
-
-       exit ${EXIT_OK}
-}
-
-hook_down() {
-       local zone="${1}"
-       local config="${2}"
-       shift 2
-
-       if ! device_exists "${zone}"; then
-               error "Zone '${zone}' doesn't exist."
-               exit ${EXIT_ERROR}
-       fi
-
-       # Stop dhclient for IPv6 on this zone.
-       dhclient_stop "${zone}" "ipv6"
-
-       exit ${EXIT_OK}
-}
-
-hook_status() {
-       local zone="${1}"
-       local config="${2}"
-       shift 2
-
-       if ! device_exists "${zone}"; then
-               error "Zone '${zone}' doesn't exist."
-               exit ${EXIT_ERROR}
-       fi
-
-       zone_config_settings_read "${zone}" "${config}"
-
-       local status
-       if dhclient_status "${zone}" "ipv6"; then
-               status="${MSG_HOOK_UP}"
-       else
-               status="${MSG_HOOK_DOWN}"
-       fi
-       cli_statusline 3 "${HOOK}" "${status}"
-
-       cli_print_fmt1 3 "IPv6 address" "$(db_get "${zone}/ipv6/local-ip-address")"
-       local gateway="$(db_get "${zone}/ipv6/remote-ip-address")"
-       if isset gateway; then
-               cli_print_fmt1 3 "Gateway" "${gateway}"
-       fi
-       cli_space
-
-       exit ${EXIT_OK}
-}