Linux系统IPsec转控分离时UDP报文的处理

项目中需要将IPsec的控制面和转发面进行分离,也就是说IKE协商在一个vm上进行,然后协商成功的child sa通过ip xfrm下发到另外的vm上用来加解密流量。实际使用的时候发现,如果是不穿越nat的时候,是可行的,因为这个时候走的esp或者ah协议。但是如果要穿越nat的话,报文经过udp封装之后就不能正常加解密了,转念一想,这样是正常的,因为vm上并没有打开对应的udp端口,报文在进入协议栈的时候就被丢掉了,linux系统会返回给发包方一个端口不可达的错误消息。

为了正确处理udp封装的ipsec报文,必须要在linux正确打开对应端口,一般是4500。

Perl 语言版:

http://techblog.newsnow.co.uk/2011/11/simple-udp-esp-encapsulation-nat-t-for.html

设置IP_XFRM_POLICY部分的内容没有办法正确执行,可能是版本太老的原因。

Python语言版

实际测试发现,不配置IP_XFRM_POLICY也是可以正常加解封装UDP封装的IPsec报文的。 socket必须是global变量。

def init_socket(port):
    global udp4500
    udp4500 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
    udp4500.setsockopt(socket.IPPROTO_IP, socket.SO_REUSEADDR, 1)
    udp4500.setsockopt(socket.IPPROTO_UDP, 100, 2)

    # IP_IPSEC_POLICY = 17
    # IPSEC_DIR_INBOUND = 0
    # IPSEC_DIR_OUTBOUND = 1
    #
    # policy1 = struct.pack("384xH828xLLBBBB", 2, 0, 0, IPSEC_DIR_OUTBOUND, 0, 0, 0)
    # policy2 = struct.pack("384xH828xLLBBBB", 2, 0, 0, IPSEC_DIR_INBOUND, 0, 0, 0)
    # s.setsockopt(socket.IPPROTO_IP, IP_IPSEC_POLICY, policy1)
    # s.setsockopt(socket.IPPROTO_IP, IP_IPSEC_POLICY, policy2)
    udp4500.bind(("0.0.0.0", port))

C语言版:

http://blog.csdn.net/xingyeping/article/details/51353448

可以正常执行,代码如下:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 


int set_ipsec_policy(int fd)
{
    struct xfrm_userpolicy_info policy;
    u_int sol, ipsec_policy;
    sol = IPPROTO_IP;
    ipsec_policy = IP_XFRM_POLICY;
    memset(&policy, 0, sizeof(policy));
    policy.action = XFRM_POLICY_ALLOW;
    policy.sel.family = AF_INET;
    policy.dir = XFRM_POLICY_OUT;

    if (setsockopt(fd, sol, ipsec_policy, &policy, sizeof(policy)) < 0)
    {

      printf("unable to set IPSEC_POLICY on socket: %s\n",strerror(errno));
      return -1;
    }

        policy.dir = XFRM_POLICY_IN;
    if (setsockopt(fd, sol, ipsec_policy, &policy, sizeof(policy)) < 0)
    {

      printf("unable to set IPSEC_POLICY on socket: %s\n",strerror(errno));
      return -1;
    }
    return 0;


}

int main(int argc, char *argv[])
{
    int sockfd = -1;

    struct sockaddr_in host_addr;
    if((sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))<0)
    {

        printf("socket() error!\n");

        exit(1);
    }
    memset(&host_addr, 0, sizeof(host_addr));
    host_addr.sin_family = AF_INET;
    host_addr.sin_port = htons(4500);
    host_addr.sin_addr.s_addr = htonl(INADDR_ANY);

    const int on = 1;
    if(setsockopt(sockfd, IPPROTO_IP, SO_REUSEADDR, &on, sizeof(on))<0)
    {

        printf("setsockopt() error!\n");

        exit(0);

    }
    int encap = 2;

    if(setsockopt(sockfd, IPPROTO_UDP, 100, &encap, sizeof(encap))<0)
    {

        printf("setsockopt() udp error!\n");

        exit(0);

    }

    if (bind(sockfd, (struct sockaddr*)&host_addr, sizeof(host_addr)) < 0)
    {

      printf("unable to bind socket: %s\n", strerror(errno));

      close(sockfd);
      return -1;
    }
    set_ipsec_policy(sockfd);
    printf("bind ..\n");
    while(1)
    {

        sleep(1);
    }
    return 0;
}

你可能感兴趣的:(Linux系统IPsec转控分离时UDP报文的处理)