# HG changeset patch
# User Thomas Arendsen Hein <thomas@intevation.de>
# Date 1744805023 -7200
#      Wed Apr 16 14:03:43 2025 +0200
# Node ID 207ad078af0dcb50560ade38090d2281c65b2979
# Parent  af330e94de6c619e8d8475e09add127784955e7a
Added extensions for ipv6 routing

based on https://wiki.hetzner.de/index.php/Xen_4.6_auf_Debian_Stretch_with_multiple_ipv4_and_ipv6_subnet_addresses#Xen_Netzwerk_Konfiguration

diff -r af330e94de6c -r 207ad078af0d scripts/vif-route
--- a/scripts/vif-route	Wed Apr 16 13:39:09 2025 +0200
+++ b/scripts/vif-route	Wed Apr 16 14:03:43 2025 +0200
@@ -19,11 +19,39 @@
 dir=$(dirname "$0")
 . "${dir}/vif-common.sh"
 
+ip6_of()
+{
+        ip -6 addr show "$1" | perl -wane '/scope global/ && /inet6 (([0-9a-f]+:*)+)/ && print $1;'
+}
+
+dom0_ip6()
+{
+  local nd=${netdev:-eth0}
+  local result=$(ip6_of "$nd")
+  if [ -z "$result" ]
+  then
+        ""
+  else
+        echo "$result"
+  fi
+}
+
+is_ipv6()
+{
+        echo "$1" | perl -wane '/:/ && print "yes"'
+}
+
 main_ip=$(dom0_ip)
+main_ip6=$(dom0_ip6)
 
 case "${command}" in
     add|online)
+        log info "[vif-route] online request, ip ${ip} with main_ip ${main_ip} and main_ip6 ${main_ip6} for $dev."
         ifconfig ${dev} ${main_ip} netmask 255.255.255.255 up
+        if [ ! -z "${main_ip6}" ]; then
+                ip -6 addr add ${main_ip6} dev ${dev}
+                echo 1 >/proc/sys/net/ipv6/conf/${dev}/proxy_ndp
+        fi
         echo 1 >/proc/sys/net/ipv4/conf/${dev}/proxy_arp
         ipcmd='add'
         cmdprefix=''
@@ -50,7 +78,21 @@
 # If we've been given a list of IP addresses, then add routes from dom0 to
 # the guest using those addresses.
 for addr in ${ip} ; do
-    ${cmdprefix} ip route ${ipcmd} ${addr} dev ${dev} src ${main_ip} metric ${metric}
+    #${cmdprefix} ip route ${ipcmd} ${addr} dev ${dev} src ${main_ip} metric ${metric}
+    result=$(is_ipv6 "${addr}")
+    if [ -z "${result}" ] ; then
+        log info "[vif-route] Adding IPv4 address ${addr} with src ${main_ip} for $dev."
+        result=`${cmdprefix} ip route ${ipcmd} ${addr} dev ${dev} src ${main_ip} 2>&1`
+        log info "[vif-route] Result: ${result}"
+    else
+        log info "[vif-route] Adding IPv6 address ${addr} with src ${main_ip6} for $dev."
+        result=`${cmdprefix} ip -6 route ${ipcmd} ${addr} dev ${dev} src ${main_ip6} 2>&1`
+        log info "[vif-route] Result: ${result}"
+
+        log info "[vif-route] Adding IPv6 proxy ${addr} on ${netdev:-eth0}."
+        result=`${cmdprefix} ip -6 neighbor ${ipcmd} proxy ${addr} dev ${netdev:-eth0} 2>&1`
+        log info "[vif-route] Result: ${result}"
+    fi
 done
 
 handle_iptable
