Linux自带的pfkey可以使用两种方式操作搭建VPN环境:一种是使用setkey手动设置SA,这种方式不常用,SA中的SPI,加密密钥、认证密钥等都是要手动填的,比如md5需要128bit,3des 192bits,可想而知有多大不便;另一种由racoon自动协商生成IPsec SA,但它在协商之前需要setkey先设置SP。
搭建环境:CentOS release 5.4,内核2.6.18-164.el5xen
拓扑如下:
在每一个VPN网关需要增加默认路由,否则无法使用ping触发协商。
一、使用setkey手动设置方式。
如果之前使用过openswan klips,需要:1,执行service ipsec stop;2,进入/lib/modules/uname -r
/kernel/net/key/,执行insmod af_key.ko;3,加载内核模块xfrmuser af_key esp4 ah4 ipcomp xfrm4_tunnel。如果之前没有用过klips,那么直接执行service ipsec restart即可。可以执行如下命令看是否正确加载netkey:
[root@localhost ipsec]# ipsec version
Linux Openswan U2.4.7/K2.6.18-164.el5xen (netkey)
See `ipsec --copyright’ for copyright information.
在确保Linux是使用netkey的情况下,就可以往下继续操作了。先配置VPN1,VPN2只要把配置里的IP换个位置就可以了,加黑的表示执行的命令行。
[root@localhost racoon]# cd /etc/racoon/
[root@localhost racoon]# vim setkey.conf
[root@localhost racoon]# cat setkey.conf
#!/usr/sbin/setkey-f
#flushSAD and SPD
flush;
spdflush;
add 192.168.95.162 192.168.95.230 esp 0x201 -m tunnel -E 3des-cbc 0x7aeaca3f87d060a12f4a4487d5a5c3355920fae70a96c831-A hmac-md5 0x16b844ab6136507eadc4765d782af8d0;
add 192.168.95.230 192.168.95.162 esp 0x301 -m tunnel -E 3des-cbc0x7aeaca3f87d060a12f4a4487d5a5c3355920fae70a96c831 -A hmac-md50x16b844ab6136507eadc4765d782af8d0;
spdadd 40.0.0.0/24 50.0.0.0/24 any -P out ipsecesp/tunnel/192.168.95.162-192.168.95.230/use;
spdadd 50.0.0.0/24 40.0.0.0/24 any -P in ipsecesp/tunnel/192.168.95.230-192.168.95.162/use;
[root@localhost racoon]# setkey -f/etc/racoon/setkey.conf
此时VPN1网关的SA、SP已经添加完毕,下面使用一个应用程序倾泻出已添加完的SA。
[root@localhost racoon]# cat dump.c (相关代码可参考UNPV13e,也可从网上直接下载源码)
因为dump.c要使用UNPV13e里的一些库和封装的函数,所以这个.c文件无法直接使用简单的gcc命令执行。我在源码目录已经编译好了dump可执行程序。
[root@localhost racoon]# ./dump (如果使用klips是无法倾泻的,因为klips没有实现倾泻安全联盟相关代码)
SADB_DUMP:10
Sendingdump message:
SADBMessage Dump, errno 0, satype Unspecified, seq 0, pid 27408
Messagesreturned:
SADBMessage Dump, errno 0, satype IPsec ESP, seq 1, pid 27408
SA: SPI=16973824 Replay Window=0 State=Mature
Authentication Algorithm: HMAC-MD5
Encryption Algorithm: 3DES-CBC
Hard lifetime:
0 allocations, 0 bytes0 addtime, 0 usetime
Soft lifetime:
0 allocations, 0 bytes0 addtime, 0 usetime
Current lifetime:
0 allocations, 0 bytes
added at Tue Jan 29 11:11:58 2013, never used
Source address: 192.168.95.230/32
Dest address: 192.168.95.162/32
Proxy address: 0.0.0.0 (IP proto 255)
Authentication key,128 bits: 0x16b844ab6136507eadc4765d782af8d0
Encryption key, 192bits: 0x7aeaca3f87d060a12f4a4487d5a5c3355920fae70a96c831
[unknown extension 19]
SADBMessage Dump, errno 0, satype IPsec ESP, seq 0, pid 27408
SA: SPI=16908288 Replay Window=0 State=Mature
Authentication Algorithm: HMAC-MD5
Encryption Algorithm: 3DES-CBC
Hard lifetime:
0 allocations, 0 bytes0 addtime, 0 usetime
Soft lifetime:
0 allocations, 0 bytes0 addtime, 0 usetime
Current lifetime:
0 allocations, 0 bytes
added at Tue Jan 29 11:11:58 2013, never used
Source address: 192.168.95.162/32
Dest address: 192.168.95.230/32
Proxy address: 0.0.0.0 (IP proto 255)
Authentication key,128 bits: 0x16b844ab6136507eadc4765d782af8d0
Encryption key, 192bits: 0x7aeaca3f87d060a12f4a4487d5a5c3355920fae70a96c831
[unknown extension 19]
可以很清楚的看到,两个方向都包含对应的SA,只不过它的SPI显示的不太好看,可以用以下命令查看SPI,即我们在 setkey.conf里指定的。
[root@localhost racoon]# setkey -D
192.168.95.230192.168.95.162
esp mode=tunnel spi=769(0x00000301) reqid=0(0x00000000)
E: 3des-cbc 7aeaca3f 87d060a1 2f4a4487 d5a5c335 5920fae70a96c831
A: hmac-md5 16b844ab 6136507e adc4765d 782af8d0
seq=0x00000000 replay=0flags=0x00000000 state=mature
created: Jan 29 11:11:58 2013 current: Jan 29 11:18:14 2013
diff: 376(s) hard: 0(s) soft: 0(s)
last: hard: 0(s) soft: 0(s)
current: 0(bytes) hard: 0(bytes) soft: 0(bytes)
allocated: 0 hard: 0 soft: 0
sadb_seq=1 pid=27417 refcnt=0
192.168.95.162192.168.95.230
esp mode=tunnel spi=513(0x00000201) reqid=0(0x00000000)
E: 3des-cbc 7aeaca3f 87d060a1 2f4a4487 d5a5c335 5920fae70a96c831
A: hmac-md5 16b844ab 6136507e adc4765d 782af8d0
seq=0x00000000 replay=0flags=0x00000000 state=mature
created: Jan 29 11:11:58 2013 current: Jan 29 11:18:14 2013
diff: 376(s) hard: 0(s) soft: 0(s)
last: hard: 0(s) soft: 0(s)
current: 0(bytes) hard: 0(bytes) soft: 0(bytes)
allocated: 0 hard: 0 soft: 0
sadb_seq=0 pid=27417 refcnt=0
二、使用racoon自动协调方式。
也仅配置VPN1。VPN2类似。
[root@localhost racoon]# cd /etc/racoon/
[root@localhost racoon]# vim setkey.conf
[root@localhost racoon]# cat setkey.conf
#!/usr/sbin/setkey-f
#flushSAD and SPD
flush;
spdflush
spdadd 40.0.0.0/24 50.0.0.0/24 any -P out ipsecesp/tunnel/192.168.95.162-192.168.95.230/use;
spdadd 50.0.0.0/24 40.0.0.0/24 any -P in ipsecesp/tunnel/192.168.95.230-192.168.95.162/use;
[root@localhost racoon]# vim racoon.conf
[root@localhost racoon]# cat racoon.conf
RacoonIKE daemon configuration file.
See’man racoon.conf’ for a description of the format and entries.
pathinclude “/etc/racoon”;
pathpre_shared_key “/etc/racoon/psk”;
#pathcertificate “/etc/racoon/certs”;
remote192.168.95.230 {
exchange_mode main;
lifetime time 2 hour;
proposal {
encryption_algorithm 3des;
hash_algorithm sha1;
authentication_methodpre_shared_key;
dh_group 2;
}
}
sainfoanonymous
{
pfs_group modp768;
lifetime time 1 hour ;
encryption_algorithm 3des, blowfish448, rijndael ;
authentication_algorithm hmac_sha1,hmac_md5 ;
compression_algorithm deflate ;
}
[root@localhost racoon]# cat psk
192.168.95.2300x123456
[root@localhost racoon]# chmod 700 psk
[root@localhost racoon]# ls -alh psk
-rwx------1 root root 24 01-28 17:27 psk
注意psk文件的属性,一定不能被除所有者以外的人可读(我把它设置为700),否则racoon协商时会报以下错误:
/etc/racoon/pskhas weak file permission
在配置完VPN1和VPN2后,执行:
[root@localhost racoon]# setkey -f/etc/racoon/setkey.conf
[root@localhost racoon]# racoon -d -F -f/etc/racoon/racoon.conf (-d表示打开详细的调试信息,-F表示前台运行,这样可以直接看到调试信息了)
Foregroundmode.
2013-01-2911:27:54: INFO: @(#)ipsec-tools 0.6.5 (http://ipsec-tools.sourceforge.net)
2013-01-2911:27:54: INFO: @(#)This product linked OpenSSL 0.9.8e-fips-rhel5 01 Jul 2008(http://www.openssl.org/)
2013-01-2911:27:54: INFO: 127.0.0.1[500] used as isakmp port (fd=7)
2013-01-2911:27:54: INFO: 127.0.0.1[500] used for NAT-T
2013-01-2911:27:54: INFO: 192.168.95.230[500] used as isakmp port (fd=8)
2013-01-2911:27:54: INFO: 192.168.95.230[500] used for NAT-T
2013-01-2911:27:54: INFO: 50.0.0.1[500] used as isakmp port (fd=9)
2013-01-2911:27:54: INFO: 50.0.0.1[500] used for NAT-T
2013-01-2911:27:54: INFO: ::1[500] used as isakmp port (fd=10)
2013-01-2911:27:54: INFO: fe80::20c:29ff:fe38:49f8%eth0[500] used as isakmp port (fd=11)
2013-01-2911:27:54: INFO: fe80::20c:29ff:fe38:4902%eth1[500] used as isakmp port (fd=12)
此时两边都是就绪状态,这时可以通过从一方ping对端保护子网地址,就可以触发协商,这个类似于cisco方式。
附几个常用的命令:
setkey使用
setkey –D:查看SAD信息
setkey –DP:查看SPD信息
setkey –F:清除SAD信息
setkey –FP:清除SPD信息
racoon
racoon –F :任务前台运行
racoon –d: debug
racoon和setkey源码在ipsec-toos包中。
常遇错误
1,/etc/racoon/pskhas weak file permission
这是因为psk这个文件的安全性要求很高,不能不能被除所有者以外的人可读(这个可以从源码的注释中看到)。
2,pfkey X_SPDDUMP failed: No such file ordirectory
没有使用setkey设置SP。
3,ping的时候无法触发racoon协调,增加默认路由。