如何通过RAW套接字发送修改后的IPv6数据包?

Pep*_*dou 5 c linux ipv6 raw-sockets sendto

我正在尝试通过 C Linux 中的 RAW 套接字发送自定义 IPv6 标头。
我已经使用 IP_HDRINCL 套接字选项在 IPv4 中取得了成功,但是,IPv6 没有等效的选项。
在这里找到了一个解决方法,建议使用socket(AF_INET6, SOCK_RAW, IPPROTO_RAW)与启用 IP_HDRINCL 套接字选项具有相同的效果。套接字已成功创建,并且在使用带有修改后的标头的函数

之前我不会收到任何错误。sendto

我这样设置套接字:

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;
}
Run Code Online (Sandbox Code Playgroud)

然后,我有一个回调函数,它应该发送我的自定义 IPv6 数据包,但sendto无法返回 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");
    }
}
Run Code Online (Sandbox Code Playgroud)

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

Pep*_*dou 3

好的,所以我找到了解决方案。对于任何为此苦苦挣扎的人,我解决这个问题的方法是使用 Pcap 库(按照这个例子)。然而,对于那些必须使用 AF_PACKETS 的人,我在此链接中找到了一个非常完整的示例,它使用 RAW 套接字来完成此操作。