我无法为 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.h
IPV6_DONTFRAG
linux/in6.h
linux/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_DISCOVER
int on = IPV6_PMTUDISC_DO
setsockopt
IPV6_RECVPATHMTU
但我没有得到PACKET TOO BIG
答复或ancillary data
与cmsg_level == IPPROTO_IPV6
cmsg_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
对我不起作用,但setsockopt
对IPV6_MTU_DISCOVER
自己的界面起作用。eth1 接口 MTU 为 1500(默认),如果sendto
要发送更大尺寸的数据包,errno
设置为EMSGSIZE
. 同样在一段时间后,我收到PACKET TOO BIG
了这些不发送来自自己的内核/操作系统的消息的消息。
我真正的问题是,我没有PACKET TOO BIG
从运行在 GNS3 上的虚拟路由器(Cisco c3660)收到(在 VirtualBox 4.2.6 上运行的 Ubuntu 12.10)消息。