如何通过RAW套接字发送修改后的IPv6数据包? [英] How to send modified IPv6 packet through RAW socket?

查看:531
本文介绍了如何通过RAW套接字发送修改后的IPv6数据包?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过C Linux中的RAW套接字发送自定义IPv6标头.
我已经使用IP_HDRINCL套接字选项成功实现了IPv4,但是IPv6没有等效项.
我在此处找到了解决方法,建议使用socket(AF_INET6, SOCK_RAW, IPPROTO_RAW)具有相同的解决方法与启用IP_HDRINCL套接字选项相同.

套接字已成功创建,并且在将sendto函数与修改后的标头一起使用之前,不会出现任何错误.

I'm trying to send a custom IPv6 header through a RAW socket in C Linux.
I already succeded in IPv4 using the IP_HDRINCL socket option, however, there's not an equivalent for IPv6.
I found a workaround here suggesting to use socket(AF_INET6, SOCK_RAW, IPPROTO_RAW) to have the same effect as enabling the IP_HDRINCL socket option.

The socket is created successfully and I'm not getting any error until I use the sendto function with my modified header.

我这样设置套接字:

static int socketFd = 0;
static struct sockaddr_in6 remote;

int main()
{
    socketFd = socket (PF_INET6, SOCK_RAW, IPPROTO_RAW);

    if (socketFd < 0)
    {
        printf ("An error ocurred while creating the socket.\n");
        exit (2);
    }

    remote.sin6_family = AF_INET6;
    remote.sin6_port = htons (25000);

    if (inet_pton (AF_INET6, "fd00:c0de::70d6:4ab9:115d:8cda", &(remote.sin6_addr)) != 1)
    {
        close (socketFd);
        printf ("Unable to parse IPv6 address.\n");
        exit (2);
    }

 /*More code */
  ...

  return 0;
}

然后,我有了此回调函数,该函数应该发送我的自定义IPv6数据包,但sendto无法返回EINVAL.

And then, I have this callback function that should send my custom IPv6 packets but sendto fails returning EINVAL.

static void sendPacket ()
{
    char buffer[BUFSIZ];
    const size_t len = sizeof(struct ip6_hdr) + sizeof(struct UDP_hdr);
    struct ip6_hdr *ip6 = (struct ip6_hdr*) (buffer);
    struct UDP_hdr *udp = (struct UDP_hdr *) (buffer + sizeof(struct ip6_hdr));

    memset (buffer, 0, BUFSIZ);

    ip6->ip6_ctlun.ip6_un2_vfc = 0x60;
    ip6->ip6_dst = remote.sin6_addr;
    ip6->ip6_flow = 60;
    ip6->ip6_hops = 64;
    ip6->ip6_nxt = 17;
    ip6->ip6_plen = sizeof(struct UDP_hdr);

    if (inet_pton (AF_INET6, "fd00:c0de::62a4:4cff:1234:5678", &(ip6->ip6_src)) != 1)
    {
        printf ("Error while parsing spoofed IPv6 address.\n");
        return;
    }

    // Fabricate the UDP header. Source port number, redundant
    udp->uh_sport = htons (21000);
    // Destination port number
    udp->uh_dport = remote.sin6_port;
    udp->uh_ulen = htons (sizeof(struct UDP_hdr));

    if (sendto (socketFd, buffer, len, 0, (struct sockaddr *) &remote, sizeof(remote)) < 0)
    {
        perror ("sendto");
        printf ("Error while sending packet.\n");
    }
}

我已经调试了程序,并且ip6_hdr结构中的所有值似乎都是正确的,而且UDP_hdr结构中的值也正确.我想念什么吗?

I've debugged the program and all the values in the ip6_hdr struct seem to be correct and also the ones in the struct UDP_hdr. Am I missing something?

推荐答案

好,所以我找到了解决方案.对于任何对此感到困惑的人,我解决它的方法是使用Pcap库(紧随示例).但是对于必须使用AF_PACKETS的用户,我在此中找到了一个使用RAW套接字完成此操作的非常完整的示例.链接.

Ok, so I found a solution. To anyone struggling with this, the way I solved it was using the Pcap library (followed this example). However for those that must use AF_PACKETS, I found a very complete example which uses RAW sockets to accomplish this in this link.

这篇关于如何通过RAW套接字发送修改后的IPv6数据包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆