4

我无法为 IPv6/ICMPv6 数据包设置“不分段”标志。我正在做 PMTUD,我想强制路由器丢弃比 MTU 更大的数据包。使用带有 IPV6_MTU_DISCOVER 的 setsockopt 不起作用。

int on = IPV6_PMTUDISC_DO; // tried also IPV6_PMTUDISC_PROBE
setsockopt(socket, IPPROTO_IPV6, IPV6_MTU_DISCOVER, &on, sizeof(on));

结果: 线鲨

我也不能按照Unix-Linux Addison-Wesley - Stevens2003 - Unix Network ProgrammingIPV6_DONTFRAG中的描述使用 setosckopt,因为我包含了标头并在. 包含在我的源代码中会导致这些重新定义错误。netinet/in6.hIPV6_DONTFRAGlinux/in6.hlinux/in6.h

In file included from mypmtud.cc:30:0: /usr/include/linux/in6.h:30:8: error: redefinition of ‘struct in6_addr’ In file included from /usr/include/netdb.h:28:0,
                 from mypmtud.cc:23: /usr/include/netinet/in.h:198:8: error: previous definition of ‘struct in6_addr’ In file included from mypmtud.cc:30:0: /usr/include/linux/in6.h:46:8: error: redefinition of ‘struct sockaddr_in6’ In file included from /usr/include/netdb.h:28:0,
                 from mypmtud.cc:23: /usr/include/netinet/in.h:239:8: error: previous definition of ‘struct sockaddr_in6’ In file included from mypmtud.cc:30:0: /usr/include/linux/in6.h:54:8: error: redefinition of ‘struct ipv6_mreq’ In file included from /usr/include/netdb.h:28:0,
                 from mypmtud.cc:23: /usr/include/netinet/in.h:275:8: error: previous definition of ‘struct ipv6_mreq’ make: *** [mypmtud] Error 1

环境:VirtualBox 4.26 上的 Ubuntu 12.10 和用于虚拟网络的 GNS3。虚拟 Cisco C3660 路由器只有基本配置:ip、ipv6 地址、不关闭和设置 mtu。

编辑: 我需要 IPv6 堆栈/OS 内核来丢弃大于链路 MTU 的数据包或发出“此数据包需要分段”的信号。我怎样才能实现这种行为?

我尝试setsockopt使用IPV6_DONTFRAG(在我的代码中定义#define IPV6_DONTFRAG 62)、setsockopt使用和使用。IPV6_MTU_DISCOVERint on = IPV6_PMTUDISC_DOsetsockoptIPV6_RECVPATHMTU

但我没有得到PACKET TOO BIG答复或ancillary datacmsg_level == IPPROTO_IPV6cmsg_type == IPV6_PATHMTU.

我的部分代码:

/** sending ICMP packet*/
 if (((length = sendto(mysocket, packet, lengthBuff, 0, result->ai_addr, result->ai_addrlen)) < 0) && (errno == EMSGSIZE)){

     // works for IPv4, doesn't work with IPv6
     cout << "changing maxBuff and lengthBuff size" << endl;

        maxBuff = lengthBuff;
        lengthBuff = (minBuff + maxBuff) / 2;

        if (packet) {
            delete(packet);
            packet = NULL;
        }


    } else if (length < 0){

     cerr << "Error: sending data." << endl;

            freeaddrinfo(result);
            close(mysocket);

            if (packet) {
                delete(packet);
                packet = NULL;
            }

            exit(1);
    } else if(((recvmsg(mysocket, &msg, 0)) != -1) && (errno != EINTR)) {

        // reading ancillary dada as described in  *Unix-Linux Addison-Wesley - Stevens2003 - Unix Network Programming, page 736*
        cmsgh = CMSG_FIRSTHDR(&msg);    

        if(cmsgh != NULL) {
            cout << "getting msg " << endl;
            cout << "msg len " << msg.msg_controllen << endl;


            if(cmsgh->cmsg_level == IPPROTO_ICMPV6 && cmsgh->cmsg_type == IPV6_PATHMTU)
            {
                cout << "CMSGHEADER - GOOD" << endl;
                //mtustruct = CMSG_DATA(&msg); 

            maxBuff = lengthBuff;
            lengthBuff = (minBuff + maxBuff) / 2;

            if (packet) {
                delete(packet);
                packet = NULL;
            }

            }
            else{

                cout << "different ancillary data. " << endl;
                cout << " level " << cmsgh->cmsg_level << " type " << cmsgh->cmsg_type << endl;
            }
        }

    } else {

        cout << "no ERROR with sendto and no RESCVMSG" << endl;
    } 

    /** receiving ICMP data */
    tv.tv_sec = 3;
    tv.tv_usec = 0;

    int retval; // select

        FD_ZERO(&mySet);
        FD_SET(mysocket, &mySet);

        retval = select(mysocket + 1, &mySet, NULL, NULL, &tv);

        if (retval == -1) {
            cerr << "select failed" << endl;
            //break;
            exit(1);
        } else if (retval) {
            if ((length = recvfrom(mysocket, buffer, MAX, 0, result->ai_addr, &(result->ai_addrlen))) == -1) {
                cerr << "Error: receiving data." << endl;
            } else {
                icmpRec = (struct icmp6_hdr*) buffer;

                if((icmpRec->icmp6_type == ICMP6_PACKET_TOO_BIG)) {


                    cout << "next hop MTU: " << ntohl(icmpRec->icmp6_mtu) << endl;
                    maxBuff = ntohl(icmpRec->icmp6_mtu);                       

                } else if ((icmpRec->icmp6_type == ICMP6_ECHO_REPLY) && (ntohs(icmpRec->icmp6_id) == pid) && (ntohs(icmpRec->icmp6_seq) == (seq - 1))) {
                    cout << "code " << ntohs(icmpRec->icmp6_code) << endl;
                    cout << "ICMP ECHO REPLY" << endl;
                    minBuff = lengthBuff;

                }
            }
        }

EDIT2: 我意识到,setsockopt定义IPV6_DONTFRAG对我不起作用,但setsockoptIPV6_MTU_DISCOVER自己的界面起作用。eth1 接口 MTU 为 1500(默认),如果sendto要发送更大尺寸的数据包,errno设置为EMSGSIZE. 同样在一段时间后,我收到PACKET TOO BIG了这些不发送来自自己的内核/操作系统的消息的消息。

我真正的问题是,我没有PACKET TOO BIG从运行在 GNS3 上的虚拟路由器(Cisco c3660)收到(在 VirtualBox 4.2.6 上运行的 Ubuntu 12.10)消息。

4

1 回答 1

5

我想强制路由器丢弃比 MTU 更大的数据包

在 IPv6 中,大于 MTU 的数据包将始终被丢弃。与 IPv4 不同,IPv6 路由器不会对数据包进行分段。相反,源应该执行 PMTU 并且:

  • 让传输层协议产生足够大小的数据报
  • 在本地对数据包进行分段并附加分段扩展

Linux 确实完全支持IPV6_DONTFRAG(我认为它是在 2.6.35 中添加的),尽管它只影响本地行为。

于 2013-01-06T19:02:24.887 回答