Skip to content

Instantly share code, notes, and snippets.

@mrngm
Created January 19, 2017 19:54
Show Gist options
  • Save mrngm/951f981200c05eb2a11461d5b9c726fe to your computer and use it in GitHub Desktop.
Save mrngm/951f981200c05eb2a11461d5b9c726fe to your computer and use it in GitHub Desktop.
#!/bin/bash
#============================================================================
# ${XEN_SCRIPT_DIR}/vif-route
#
# Script for configuring a vif in routed mode.
# The hotplugging system will call this script if it is specified either in
# the device configuration given to Xend, or the default Xend configuration
# in ${XEN_CONFIG_DIR}/xend-config.sxp. If the script is specified in
# neither of those places, then vif-bridge is the default.
#
# Usage:
# vif-route (add|remove|online|offline)
#
# Environment vars:
# dev vif interface name (required).
# XENBUS_PATH path to this device's details in the XenStore (required).
#
# Read from the store:
# ip list of IP networks for the vif, space-separated (default given in
# this script).
#============================================================================
# Initial IPv6 patch by BenV
# http://notes.benv.junerules.com/all/software/xen-and-routed-ipv6/
# Additional IPv6 patch by mrngm for delivering subnets to domU
# http://mrngm.com/hetzner-ipv6.txt
dir=$(dirname "$0")
. "${dir}/vif-common.sh"
ip6_of()
{
ip -6 addr show "$1" | perl -wane '/scope global/ && /inet6 (([0-9a-f]+:*)+)/ && print $1;'
}
ip6_get_subnet_part()
{
echo "$1" | perl -wane '/(([0-9a-f]+:*)+)/ && print $1;' | cut -d \: -f 5
}
ip6_get_subnet()
{
echo "$1" | perl -wane '/(([0-9a-f]+:*)+)/ && print $1;' | cut -d \: -f -5
}
dom0_ip6()
{
local nd=${netdev:-eth0}
local result=$(ip6_of "$nd")
if [ -z "$result" ]
then
""
else
echo "$result"
fi
}
is_ipv6()
{
echo "$1" | grep -q ':' && echo "yes" || echo "no"
}
main_ip=$(dom0_ip)
main_ip6=$(dom0_ip6)
case "${command}" in
online)
log info "[vif-route] online request, ip ${ip} with main_ip ${main_ip} and main_ip6 ${main_ip6} for $vif."
ifconfig ${vif} ${main_ip} netmask 255.255.255.255 up
if [ ! -z "${main_ip6}" ]; then
ip -6 addr add ${main_ip6} dev ${vif}
echo 1 > /proc/sys/net/ipv6/conf/${vif}/proxy_ndp
fi
echo 1 >/proc/sys/net/ipv4/conf/${vif}/proxy_arp
ipcmd='add'
cmdprefix=''
;;
offline)
do_without_error ifdown ${dev}
ipcmd='del'
cmdprefix='do_without_error'
;;
esac
if [ "${ip}" ] ; then
# 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 ${vif} src ${main_ip}
result=$(is_ipv6 "${addr}")
if [ "${result}" == "no" ] ; then
log info "[vif-route] Adding IPv4 address ${addr} with src ${main_ip} for $vif."
result=`${cmdprefix} ip route ${ipcmd} ${addr} dev ${vif} src ${main_ip} 2>&1`
else
log info "[vif-route] Adding IPv6 address ${addr} with src ${main_ip6} for $vif."
result=`${cmdprefix} ip -6 route ${ipcmd} ${addr} dev ${vif} src ${main_ip6} 2>&1`
log info "[vif-route] Adding IPv6 route for $(ip6_get_subnet "${addr}")::/80 to ${addr}."
result=`${cmdprefix} ip -6 route ${ipcmd} $(ip6_get_subnet "${addr}")::/80 via ${addr} 2>&1`
log info "[vif-route] Adding IPv6 neighbor proxy for ${addr}."
result=`${cmdprefix} ip -6 neighbor ${ipcmd} proxy ${addr} dev ${netdev:-eth0} 2>&1`
fi
done
fi
handle_iptable
call_hooks vif post
log debug "Successful vif-route ${command} for ${dev}."
if [ "${command}" = "online" ]
then
success
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment