IPSec on FreeBSD

[ Nob's Home | FreeBSD ]

FreeBSD 4.4Rで IPSecを利用してVPNを構築してみます。

Information

Network構成

          global         private 
BOX1      a.b.c.d        192.168.0.1/24
BOX2      e.f.g.h        192.168.1.1/24
このBOX1とBOX2の間に gif interfaceでトンネルを設置します。

Kernel再構築

IPSecを利用するにはBOX1とBOX2の kernel の config で

options         IPSEC                   #IP security
options         IPSEC_ESP               #IP security (crypto; define w/ IPSEC)
options         IPSEC_DEBUG             #debug for IP security
pseudo-device gif
を設定し、recompileします。

/etc/ipsec.conf

kernel内の Security Policy Database (SPD) として知られるテーブルを作成する
setkey(8) コマンドの動作を設定します。
最初はracoonを使用せず、鍵を手動で設定します。

BOX1では
########################################################
# ESP tunnel from a.b.c.d      to   e.f.g.h
#                 192.168.0.1       192.168.1.1
#
flush;
spdflush;

add a.b.c.d   e.f.g.h esp       9991       -E simple "258mojiInainoMojiretsu1"
add e.f.g.h   a.b.c.d esp       9992       -E simple "258mojiInainoMojiretsu2"
#   src       dst     protocol  spi        algorithm 
#                     暗号化    256以上の  -E 暗号化のアルゴリズム "key"
#                     するので  数字          des-cbc               64bit =  8byte
#                     esp                     3des-cbc             192    = 24
#                                             simple               0-2048 = 0-256
#                                             blowfish-cbc         40-448 = 5-56
#                                             cast128-cbc          40-128 = 5-16
#                                             des-deriv             64    = 8
#                                             3des-deriv           192    = 24
#                                             rijndael-cbc         128/192/256=16/24/32
#
spdadd 192.168.0.0/24 192.168.1.0/24 any
        -P out ipsec esp/tunnel/a.b.c.d-e.f.g.h/require;
spdadd 192.168.1.0/24 192.168.0.0/24 any
        -P in  ipsec esp/tunnel/e.f.g.h-a.b.c.d/require;
########################################################
BOX2では
########################################################
# ESP tunnel from e.f.g.h      to   a.b.c.d
#                 192.168.1.1       192.168.0.1	
#
flush;
spdflush;

add a.b.c.d   e.f.g.h esp       9991 -E simple "258mojiInainoMojiretsu1"
add e.f.g.h   a.b.c.d esp       9992 -E simple "258mojiInainoMojiretsu2"
#   src       dst     protocol  spi  algorithm  
	
spdadd 192.168.1.0/24 192.168.0.0/24 any
        -P out ipsec esp/tunnel/e.f.g.h-a.b.c.d/require;
spdadd 192.168.0.0/24 192.168.1.0/24 any
        -P in  ipsec esp/tunnel/a.b.c.d-e.f.g.h/require;
########################################################
起動時に/etc/rc.networkで setkey -f /etc/ipsec.conf が実行されます。

SPDの確認

現在のルールを確認するのには -D, -DP オプションを使います。
BOX2では以下のようになります。
# setkey -DP

192.168.0.0/24[any] 192.168.1.0/24[any] any
        in ipsec
        esp/tunnel/a.b.c.d-e.f.g.h/require
        spid=10 seq=1 pid=6583
        refcnt=1
192.168.1.0/24[any] 192.168.0.0/24[any] any
        out ipsec
        esp/tunnel/e.f.g.h-a.b.c.d/require
        spid=9 seq=0 pid=6583
        refcnt=1
# setkey -D
e.f.g.h  a.b.c.d
        esp mode=any spi=9992(0x00002708) reqid=0(0x00000000)
        E: null  7473746f 6d616b69 6f6b61
        replay=0 flags=0x00000040 state=mature seq=1 pid=6584
        created: Feb  2 08:49:09 2002   current: Feb  2 08:55:34 2002
        diff: 385(s)    hard: 0(s)      soft: 0(s)
        last: Feb  2 08:49:24 2002      hard: 0(s)      soft: 0(s)
        current: 348(bytes)     hard: 0(bytes)  soft: 0(bytes)
        allocated: 3    hard: 0 soft: 0
        refcnt=2
a.b.c.d  e.f.g.h
        esp mode=any spi=9991(0x00002707) reqid=0(0x00000000)
        E: null  6d616b69 6f6b6174 6f7473
        replay=0 flags=0x00000040 state=mature seq=0 pid=6584
        created: Feb  2 08:49:09 2002   current: Feb  2 08:55:34 2002
        diff: 385(s)    hard: 0(s)      soft: 0(s)
        last: Feb  2 08:49:24 2002      hard: 0(s)      soft: 0(s)
        current: 252(bytes)     hard: 0(bytes)  soft: 0(bytes)
        allocated: 3    hard: 0 soft: 0
        refcnt=1

動作の確認

まず ifconfig で tunnel を作成。その中に経路を設定します。
# ifconfig gif0 create tunnel a.b.c.d  e.f.g.h
# ifconfig gif0 192.168.0.1  192.168.1.1 netmask 255.255.255.255
# route add 192.168.1.0/24 192.168.1.1
ifconfig と netstatで確認します。
# ifconfig gif0
gif0: flags=8051 mtu 1280
        tunnel inet a.b.c.d --> e.f.g.h
        inet6 fexx::xx:xx:xx:xxxxx prefixlen 64 scopeid 0x5
        inet 192.168.0.1 --> 192.168.1.1 netmask 0xffffffff

# netstat -nr 
略
192.168.1          192.168.1.1        UGSc        0        0   gif0
192.168.1.1        192.168.0.1        UH          1       51   gif0
略
pingなどして接続を確認してください。

/etc/rc.conf の編集

/etc/rc.conf に以下を追加します。
ipsec_enable="YES"
ipsec_file="/etc/ipsec.conf"

gif_interfaces="gif0"
gifconfig_gif0="a.b.c.d  e.f.g.h"

network_interfaces="lo0 vr0 dc0 gif0" 
# gif0 を追加

ifconfig_gif0="192.168.0.1 192.168.1.1 netmask 255.255.255.255"

static_routes="vpn"
route_vpn="-net 192.168.1.0/24 192.168.1.1"

racoonのinstall

暗号鍵を交換するdaemon である racoon をinstallします。
#
# cd /usr/ports/security/racoon
# make install
2002.1.31現在の stable の ports は
PORTVERSION=    20011215a
MASTER_SITES=   ftp://ftp.kame.net/pub/kame/misc/
でした。

racoonを使用する場合、/etc/ipsec.confから

add a.b.c.d   e.f.g.h esp       9991 -E simple "258mojiInainoMojiretsu1"
add e.f.g.h   a.b.c.d esp       9992 -E simple "258mojiInainoMojiretsu2"
を取り除きます。

racoonの設定

racoonの設定は /usr/local/etc/racoon/racoon.conf で行ないます。
今回は事前共有鍵を用います。
racoon.confはBOX1,BOX2で共通です。

# cd /usr/local/etc/racoon
# vi racoon.conf
path include "/usr/local/etc/racoon" ;
path pre_shared_key "/usr/local/etc/racoon/psk.txt" ;
path certificate "/usr/local/etc/cert" ;

#log debug;
log notify;

remote anonymous
{
        exchange_mode main, aggressive;
        doi ipsec_doi;
        situation identity_only;
        nonce_size 16;
        lifetime time 1 hour;   # sec,min,hour
        initial_contact on;
        support_mip6 on;
        proposal_check obey;    # obey, strict or claim

        proposal {
                encryption_algorithm 3des;
                hash_algorithm sha1;
                authentication_method pre_shared_key ;
                dh_group 2 ;
        }
}


sainfo anonymous
{
        pfs_group 2;
        lifetime time 1 hour;
        encryption_algorithm 3des ;
        authentication_algorithm hmac_sha1;
        compression_algorithm deflate ;
}

共有する鍵を /usr/local/etc/racoon/psk.txt に置きます。

# vi /usr/local/etc/racoon/psk.txt
# BOX1では
e.f.g.h     hogehoge
#
# BOX2では
a.b.c.d     hogehoge
# chmod 400  /usr/local/etc/racoon/psk.txt


[ Nob's Home | FreeBSD ]

nob@makioka.y-min.or.jp