]> git.ipfire.org Git - people/arne_f/network.git/commitdiff
network: Improve IPV4 support.
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 31 Jul 2010 10:55:15 +0000 (12:55 +0200)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 31 Jul 2010 10:55:15 +0000 (12:55 +0200)
  Add support for duplicate IP addresses in network.
  Announce new address to neighbours.

functions.ipv4 [new file with mode: 0644]
hooks/zones/bridge.configs/ipv4-static

diff --git a/functions.ipv4 b/functions.ipv4
new file mode 100644 (file)
index 0000000..04b49a6
--- /dev/null
@@ -0,0 +1,76 @@
+#!/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/>.       #
+#                                                                             #
+###############################################################################
+
+function ipv4_split_prefix() {
+       ip_split_prefix $@
+}
+
+function ipv4_is_valid() {
+       local address=${1}
+
+       assert isset address
+
+       # Cut the /24 if there is one given
+       address=$(ipv4_split_prefix ${address})
+
+       local IFS="."
+       local octet
+       local count
+       for octet in ${address}; do
+               if [ ${octet} -ge 0 ] && [ ${octet} -le 255 ]; then
+                       count=$(( ${count} + 1 ))
+                       continue
+               fi
+
+               # If we get here the address was not valid
+               break
+       done
+
+       if [ ${count} -eq 4 ]; then
+               return ${EXIT_OK}
+       fi
+
+       return ${EXIT_ERROR}
+}
+
+function ipv4_detect_duplicate() {
+       local device=${1}
+       local address=${2}
+
+       assert isset address
+       assert isset device
+       assert device_exists ${device}
+
+       if ! arping -q -c 2 -w 3 -D -I ${device} ${address}; then
+               log DEBUG "Detected duplicate address '${address}' on device '${device}'."
+               return ${EXIT_ERROR}
+       fi
+
+       return ${EXIT_OK}
+}
+
+function ipv4_update_neighbours() {
+       local device=${1}
+       local address=${2}
+
+       arping -q -A -c 1 -I ${device} ${address}
+       ( sleep 2; arping -q -U -c 1 -I ${device} ${address} ) >/dev/null 2>&1 </dev/null &
+}
index f1fc2d6ade39acee9cae69d75a83592b3cd1147f..5793321c9a9bb3fa5c3019359dda8c9a53209b2b 100755 (executable)
@@ -78,9 +78,16 @@ function _up() {
        config_read $(zone_dir ${zone})/configs/${config}
 
        if ! zone_has_ipv4 ${zone} ${ADDRESS}/${PREFIX}; then
+               if ipv4_detect_duplicate ${zone} ${ADDRESS}; then
+                       error_log "Duplicate address detected on zone '${zone}' (${address})."
+                       error_log "Cannot continue."
+                       exit ${EXIT_ERROR}
+               fi
+
                ip addr add ${ADDRESS}/${PREFIX} dev ${zone}
-       else
-               warning "Do not set IPv4 address '${ADDRESS}/${PREFIX}' because it was already configured on zone '${zone}'."
+
+               # Announce our new address to the neighbours
+               ipv4_update_neighbours ${zone} ${ADDRESS}
        fi
 
        if zone_is_nonlocal ${zone} && [ -n "${GATEWAY}" ]; then