#!/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 . # # # ############################################################################### IP_SUPPORTED_PROTOCOLS="${IP_SUPPORTED_PROTOCOLS} ipv6" function ipv6_device_autoconf_enable() { local device="${1}" assert device_exists "${device}" sysctl_set "net.ipv6.conf.${device}.accept_ra" 1 sysctl_set "net.ipv6.conf.${device}.autoconf" 1 } function ipv6_device_autoconf_disable() { local device="${1}" assert device_exists "${device}" sysctl_set "net.ipv6.conf.${device}.accept_ra" 0 sysctl_set "net.ipv6.conf.${device}.autoconf" 0 } # Enable IPv6 RFC3041 privacy extensions if desired function ipv6_device_privacy_extensions_enable() { local device="${1}" assert device_exists "${device}" sysctl_set "net.ipv6.conf.${device}.use_tempaddr" 2 } function ipv6_device_privacy_extensions_disable() { local device="${1}" assert device_exists "${device}" sysctl_set "net.ipv6.conf.${device}.use_tempaddr" 0 } function ipv6_is_valid() { ipcalc --ipv6 -c $@ >/dev/null 2>&1 case "$?" in 0) return ${EXIT_OK} ;; *) return ${EXIT_ERROR} ;; esac } function ipv6_prefix_is_valid() { local prefix=${1} assert isset prefix [ ${prefix} -le 0 ] && return ${EXIT_FALSE} [ ${prefix} -gt 128 ] && return ${EXIT_FALSE} return ${EXIT_TRUE} } function ipv6_get_prefix() { ip_get_prefix "$@" } function ipv6_split_prefix() { ip_split_prefix "$@" } function ipv6_implode() { local address=${1} assert isset address local ADDRESS6_IMPL eval $(ipcalc -6 -i ${address} 2>/dev/null) assert isset ADDRESS6_IMPL print "${ADDRESS6_IMPL}" } function ipv6_explode() { local address=${1} assert isset address # Nothing to do if the length of the address is 39. if [ ${#address} -eq 39 ]; then print "${address}" return ${EXIT_OK} fi local ADDRESS6_EXPL eval $(ipcalc -6 -e ${address} 2>/dev/null) assert isset ADDRESS6_EXPL print "${ADDRESS6_EXPL}" } function ipv6_addr_eq() { local addr1=${1} assert isset addr1 local addr2=${2} assert isset addr2 local addr for addr in addr1 addr2; do printf -v ${addr} "%s" $(ipv6_explode ${!addr}) done [[ "${addr1}" = "${addr2}" ]] \ && return ${EXIT_TRUE} || return ${EXIT_FALSE} } function ipv6_addr_gt() { local addr1=${1} assert isset addr1 local addr2=${2} assert isset addr2 local addr for addr in addr1 addr2; do printf -v ${addr} "%s" $(ipv6_explode ${!addr}) done local i addr1_oct addr2_oct for i in 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30; do addr1_oct="0x${addr1:${i}:2}" addr2_oct="0x${addr2:${i}:2}" [[ ${addr1_oct} -gt ${addr2_oct} ]] && return ${EXIT_TRUE} done return ${EXIT_FALSE} } function ipv6_hash() { local address=${1} assert isset address # Explode address address=$(ipv6_explode ${address}) echo "${address//:/}" } function ipv6_get_network() { local addr=${1} assert isset addr # Check if a prefix (e.g. /64) is provided. local prefix=$(ip_get_prefix ${addr}) assert ipv6_prefix_is_valid ${prefix} local PREFIX6 eval $(ipcalc --ipv6 -p ${addr}) assert isset PREFIX6 print "${PREFIX6}/${prefix}" } function ipv6_6rd_format_address() { local isp_prefix="${1}" assert ipv6_is_valid "${isp_prefix}" local client_address="${2}" assert ipv4_is_valid "${client_address}" local prefix="$(ipv6_get_prefix "${isp_prefix}")" isp_prefix="$(ipv6_split_prefix "${isp_prefix}")" # This only works for prefix lengths up to 32 bit. assert [ "${prefix}" -le 32 ] assert [ "${prefix}" -gt 0 ] # Explode the address and throw away the second 32 bit. local address="$(ipv6_explode "${isp_prefix}")" client_address="$(ipv6_6rd_format_client_address ${client_address})" assert isset client_address local block1="0x${address:0:4}" local block2="0x${address:5:4}" local block3="0x${address:10:4}" local block4="0x${address:15:4}" address="$(( (${block1} << 48) + (${block2} << 32) + (${block3} << 16) + ${block4} ))" assert [ "${address}" -gt 0 ] block1="0x${client_address:0:4}" block2="0x${client_address:5:4}" client_address="$(( (${block1} << 48) + (${block2} << 32) ))" # Fix for numbers that are interpreted by bash as negative # numbers and therefore filled up with ones when shifted to # the right. Weird. if [ "${client_address}" -gt 0 ]; then client_address="$(( ${client_address} >> ${prefix} ))" else local bitmask="$(( 1 << 63 ))" client_address="$(( ${client_address} >> 1 ))" client_address="$(( ${client_address} ^ ${bitmask} ))" client_address="$(( ${client_address} >> $(( ${prefix} - 1 )) ))" fi assert [ "${client_address}" -gt 0 ] # XOR everything together address="$(( ${address} ^ ${client_address} ))" prefix="$(( ${prefix} + 32 ))" local block formatted_address=":" while [ ${address} -gt 0 ]; do printf -v block "%x" "$(( ${address} & 0xffff ))" formatted_address="${block}:${formatted_address}" address="$(( ${address} >> 16 ))" done assert ipv6_is_valid "${formatted_address}" # Implode the output IP address. formatted_address="$(ipv6_implode "${formatted_address}")" print "${formatted_address}/${prefix}" } function ipv6_6rd_format_client_address() { local address="${1}" assert isset address print "%02x%02x:%02x%02x" ${address//\./ } }