让OpenVPN支持IPv6隧道,让客户端访问IPv6网络

2011-02-04 09:55

tunnelbroker.net申请到了免费的IPv6地址,当然不能浪费着,目前天朝的IPv6太遥远的,所以我要想办法让在天朝的电脑访问IPv6的网络,鉴于翻墙必备openvpn,所在打算在openvpn上改造,想法是让openvpn分配ipv6地址给客户端电脑,Google了一下,果真找到有相同想法的网友,并且成功实现。
本人的系统配置环境为:Debian 5(服务器),Windows 7(客户机)
首先在服务器上配置好openvpn及he.net的IPv6隧道,openvpn配置见:/post/1221/
IPv6隧道配置方法如下:
首先在:http://tunnelbroker.net注册账号,使用你的服务器的IPv4地址申请一块IPv6地址,申请非常的容易,这里不再介绍,Google一下也大把的。

申请成功后你会得到以下几个IP地址:
服务端的IPv4地址,服务端的IPv6地址,你的IPv4地址,你的IPv6地址,你得到的IPv6地址块

下面以Debian 5为例进行配置网络:
编辑:

/etc/network/interfaces

添加:

auto hetunnel
iface hetunnel inet6 v4tunnel
	address 2001:470:1f0e:990::2 ##你的ipv6地址
	netmask 64
	ttl 64
	gateway 2001:470:1f0e:990::1 ##服务器的ipv6地址
	endpoint 216.218.224.42 ##服务器的ipv4地址
	local 69.XXX.XXX.XXX ##你的ipv4地址,即你自已服务器的ipv4地址

不明白者请参考图片添加。
添加好之后重启网络:

/etc/init.d/networking restart

或重启系统。
修改openvpn配置文件:
编缉:

/etc/sysctl.conf

添加:

net.ipv6.conf.all.forwarding = 1

编缉:

/etc/openvpn/server.conf

在最后添加:

script-security 2
client-connect /etc/openvpn/client-connect.sh
client-disconnect /etc/openvpn/client-disconnect.sh

其中/etc/openvpn/client-connect.sh这个文件的内容为:

#!/bin/bash
# This is a script that is run each time a remote client connects
# to this openvpn server.
# it will setup the ipv6 tunnel depending on the ip address that was
# given to the client
BASERANGE="2001:470:baa2" ##你的48位的ipv6地址块前缀,见图
# v6net is the last section of the ipv4 address that openvpn allocated
V6NET=$(echo ${ifconfig_pool_remote_ip} | awk -F. '{print $NF}')
SITID="sit${V6NET}"
# setup the sit between the local and remote openvpn addresses
/sbin/ip tunnel add ${SITID} mode sit ttl 64 remote ${ifconfig_pool_remote_ip} local ${ifconfig_local}
/sbin/ip link set dev ${SITID} up
# config routing for the new network
/sbin/ip -6 addr add ${BASERANGE}:${V6NET}::1/64 dev ${SITID}
/sbin/ip -6 route add ${BASERANGE}:${V6NET}::/64 via ${BASERANGE}:${V6NET}::2 dev ${SITID} metric 1
# log to syslog
echo "${script_type} client_ip:${trusted_ip} common_name:${common_name} local_ip:${ifconfig_local} 
remote_ip:${ifconfig_pool_remote_ip} sit:${SITID} ipv6net:${V6NET}" | /usr/bin/logger -t ovpn

表示openvpn客户端连接成功后,会自动添加一条ipv6隧道,并在openvpn服务器上生成一块名为sitX的网卡, X为客户端的IP地址最后一位数字。
比方:客户端IP为10.168.1.6,openvpn服务器IP为10.168.1.1,那么,openvpn服务器上自动添加网卡sit6,网卡的IP地址为:2001:470:baa2::6:1,而客户端的ipv6地址为:2001:470:baa2::6:2
然后配置客户端Windows 7,以管理员身份运行cmd.exe,运行以下命令:

netsh interface ipv6 add v6v4tunnel interface=IP6Tunnel 10.168.1.6 10.168.1.1
netsh interface ipv6 add address IP6Tunnel 2001:470:baa2:6::2/64
netsh interface ipv6 add route ::/0 IP6Tunnel 2001:470:baa2:6::1

然后连上openvpn,即可访问ipv6。
而/etc/openvpn/client-disconnect.sh文件即为客户端断开openvpn连接时删除相应的隧道及网卡。
内容为:

#!/bin/bash
# This is a script that is run each time a remote client disconnects
# to this openvpn server.
BASERANGE="2001:470:baa2"
# v6net is the last section of the ipv4 address that openvpn allocated
V6NET=$(echo ${ifconfig_pool_remote_ip} | awk -F. '{print $NF}')
SITID="sit${V6NET}"
/sbin/ip -6 addr del ${BASERANGE}:${V6NET}::1/64 dev ${SITID}
# remove the sit between the local and:q
#remote openvpn addresses
/sbin/ip link set dev ${SITID} down
/sbin/ip tunnel del ${SITID} mode sit ttl 64 remote ${ifconfig_pool_remote_ip} local ${ifconfig_local}
# log to syslog
echo "${script_type} client_ip:${trusted_ip} common_name:${common_name} local_ip:${ifconfig_local} 
remote_ip:${ifconfig_pool_remote_ip} sit:${SITID} ipv6net:${V6NET} duration:${time_duration} 
received:${bytes_received} sent:${bytes_sent}" | /usr/bin/logger -t ovpn

 

5 条回复

  • 111

    111 2024-05-08 23:25 回复

    并不遥远,基本普及,全部移动网络,运营商猫上都有了

  • John

    John 2011-08-03 23:00 回复

    不用,跳过这步

  • 灵曦

    灵曦 2011-08-03 18:07 回复

    感谢博主分享经验,今天参考博主的文章外加几篇教程已经配置完成。请问如果服务器本身有IPv6接入权限,是否还需要在HE做tunnel呢?

  • John

    John 2011-02-21 09:57 回复

    这个好像要安装打了ipv6补丁的openvpn,linux上有,windows上不清楚哦,openvpn默认配置是ipv4网络的vpn,直接会给ipv6的客户机分配一个局域网的ipv4地址的。你配置l2tp应该会更容易。

  • longlong

    longlong 2011-02-12 23:58 回复

    博主你好,我想问一下,我有个服务器是windows 2003的,同时有ipv4和ipv6的地址,然后客户机只有ipv6网络,我得怎么使用openvpn来使我的客户机通过这个服务器访问ipv4的网络呢?谢谢了

说两句